Skip to content

Conversation

@svdgraaf
Copy link
Contributor

@svdgraaf svdgraaf commented Sep 7, 2016

In order to test your functions in the AWS console, or to generate a proper SDK, you need to specify parameters for your functions. This PR adds that option, like so:

functions:
  create:
    handler: posts.create
    events:
      - http:
          path: posts/create
          method: post
          parameters:
            method.request.querystring.url: true
            method.request.header.x-foo: false

They will then show up in the console, swagger, SDK's, etc, for example:
screenshot 2016-09-07 15 29 27

@flomotlik
Copy link
Contributor

flomotlik commented Sep 8, 2016

I like the feature a lot, but I'd do it a bit differently, so instead of:

- http:
       path: posts/create
        method: post
        parameters:
           method.request.querystring.url: true
           method.request.header.x-foo: false
           method.request.path.bar: false

We have

- http:
          path: posts/create/{bar}
          method: post
          request:
             headers:
               foo: false
             querystrings:
                url: true
             paths:
                bar: false

This brings it in line with the request property and easily allow users to include standard variables through `${self:custom.querystrings}.

Thoughts?

@svdgraaf
Copy link
Contributor Author

svdgraaf commented Sep 8, 2016

@flomotlik I used a 1 on 1 mapping of AWS, perhaps it's even better to move them under parameters, so that it's easier to add other variables to request later (perhaps for other providers)? Like so:

- http:
  path: posts/create
  method: post
  request:
    parameters:
      header:
        foo: false
      querystring:
        url: true
        foobar: false
      path:
        bar: false

@flomotlik
Copy link
Contributor

yup parameters makes sense. Only change I would do from your example is use plural for headers, querystrings, paths. Paths is strange though, but changing it to pathVariables is also weird.

@flomotlik
Copy link
Contributor

@svdgraaf as AWS have now released the new APIG implementation I think it makes sense to pick up this PR and implement/merge it. Let me know what the next steps are.

@svdgraaf
Copy link
Contributor Author

I'll look into it on monday, update from master and check for other loose ends

@svdgraaf
Copy link
Contributor Author

svdgraaf commented Sep 26, 2016

@flomotlik all good to go now 😄 The config is now plural as you requested:

- http:
    path: posts/create
    method: post
    request:
      parameters:
        headers:
          foo: false
        querystrings:
          url: true
          foobar: false
        paths:
          bar: false

@pmuens pmuens self-assigned this Sep 30, 2016
@pmuens pmuens added this to the v1.0-ideas milestone Sep 30, 2016
Copy link
Contributor

@pmuens pmuens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reviewed it today. Great stuff. Works out of the box 👍

Good job @svdgraaf (as always).

GTM from my side :shipit:

@pmuens
Copy link
Contributor

pmuens commented Sep 30, 2016

/cc @flomotlik

@pmuens
Copy link
Contributor

pmuens commented Oct 3, 2016

@svdgraaf one quick question. How do I test the paths parameter? If I set one up through the serverless.yml file it won't show up in the API Gateway console (it just says that I should add it in the resource path).

Here's the corresponding serverless.yml file:

- http:
    path: foo
    method: GET
    request:
      parameters:
        paths:
          bar: true

@svdgraaf
Copy link
Contributor Author

svdgraaf commented Oct 4, 2016

@pmuens I'm not sure actually, I only used the header and query parameters. I'll see if I can build an example (and document it).

@pmuens
Copy link
Contributor

pmuens commented Oct 4, 2016

@svdgraaf thank you very much! That would be great! 👍

Other than that the parameter support works really well! Good job!

@svdgraaf
Copy link
Contributor Author

svdgraaf commented Oct 6, 2016

Ok, I've put some time in it, and I know what the problem is.

The issue is that path parameters are actually not supported via the RequestParameters, or at least, it's very badly documented. For the path parameters to work, you need to add the parameter to the path, like so: /foo/{url}/, which will generate an endpoint path with url as path parameter.

However, I see that there currently is an issue with this PR, as that when you make such an endpoint, it will not set any other RequestParameters that you have defined (eg: the headers/params). I have to find some time to find out why.

@pmuens
Copy link
Contributor

pmuens commented Oct 6, 2016

@svdgraaf thanks for looking into it. Yes I was also struggling to find some resources on how to use path parameters.

Great that you're about to tackle it. Let me know if you need any help there. 👍

@flomotlik
Copy link
Contributor

Ok so we're waiting on this PR for now @svdgraaf correct?

@svdgraaf
Copy link
Contributor Author

svdgraaf commented Oct 7, 2016

@flomotlik yeah, let's not merge it just yet until I have time to work on this, probably this weekend or Monday.

Copy link
Contributor

@pmuens pmuens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting on more investigation regarding the paths parameter usage

@svdgraaf
Copy link
Contributor Author

Ok, got it, the problem is just that it's badly documented. I added another paragraph in the documentation with a sample: 1b1ce33

If you define a function with a paths request parameter, it just has to be added to the method path as well, like so:

# serverless.yml
functions:
  create:
    handler: posts.post_detail
    events:
      - http:
          path: posts/{id}
          method: get
          integration: lambda
          request:
            parameters:
              paths:
                id: true
              querystrings:
                bar: true

Which will end up in api gateway like so:

screenshot 2016-10-10 09 38 01

btw: you could leave the paths out of the request parameters, and the paths parameter would still show up on the test screen. The defined request parameters are mainly for testing and generating sdk's (eg: swagger), so it does makes sense somewhat (it's just very poorly documented).

@svdgraaf
Copy link
Contributor Author

something broke after the rebase with master, let me figure out why ;)

@svdgraaf
Copy link
Contributor Author

@pmuens can you check again if this is now satisfactory? :)

@pmuens
Copy link
Contributor

pmuens commented Oct 10, 2016

Cool stuff @svdgraaf 🌮 🎉 Thank you very much!

@pmuens pmuens merged commit 26b1ac9 into serverless:master Oct 10, 2016
@jch254
Copy link

jch254 commented Nov 2, 2016

I'm able to define a function as below and access the id path parameter in code using event.pathParameters.id.

functions:
  getItem:
    handler: handler.getItem
    memory: 512
    timeout: 60
    events:
      - http:
          method: get
          path: items/{id}
          cors: true

@marckaraujo
Copy link

@jch254 Your comment to get Parameters should be in Docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants