Add parameters to http event by svdgraaf · Pull Request #2056 · serverless/serverless · GitHub
Skip to content

Add parameters to http event#2056

Merged
pmuens merged 12 commits into
serverless:masterfrom
svdgraaf:feature/method-parameters
Oct 10, 2016
Merged

Add parameters to http event#2056
pmuens merged 12 commits into
serverless:masterfrom
svdgraaf:feature/method-parameters

Conversation

@svdgraaf

@svdgraaf svdgraaf commented Sep 7, 2016

Copy link
Copy Markdown
Contributor

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

flomotlik commented Sep 8, 2016

Copy link
Copy Markdown
Contributor

@svdgraaf

svdgraaf commented Sep 8, 2016

Copy link
Copy Markdown
Contributor Author

@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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@svdgraaf

svdgraaf commented Sep 26, 2016

Copy link
Copy Markdown
Contributor Author

@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

@pmuens pmuens left a comment

Copy link
Copy Markdown
Contributor

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

pmuens commented Sep 30, 2016

Copy link
Copy Markdown
Contributor

/cc @flomotlik

@pmuens

pmuens commented Oct 3, 2016

Copy link
Copy Markdown
Contributor

@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

svdgraaf commented Oct 4, 2016

Copy link
Copy Markdown
Contributor Author

@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

pmuens commented Oct 4, 2016

Copy link
Copy Markdown
Contributor

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

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

@svdgraaf

svdgraaf commented Oct 6, 2016

Copy link
Copy Markdown
Contributor Author

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

pmuens commented Oct 6, 2016

Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Contributor

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

@svdgraaf

svdgraaf commented Oct 7, 2016

Copy link
Copy Markdown
Contributor Author

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

@pmuens pmuens left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor Author

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

@svdgraaf

Copy link
Copy Markdown
Contributor Author

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

@pmuens

pmuens commented Oct 10, 2016

Copy link
Copy Markdown
Contributor

Cool stuff @svdgraaf 🌮 🎉 Thank you very much!

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

jch254 commented Nov 2, 2016

Copy link
Copy Markdown

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
Copy Markdown

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