Skip to content

Conversation

@sambauers
Copy link
Contributor

@sambauers sambauers commented Jun 10, 2020

Allows the addition of request.template.<content-type> to serverless.yml under apiGatewayServiceProxies.

Changes the approach to passing basic Action and MessageBody values by passing them in the POST request payload instead of via querystrings. This means that the prior default RequestParameters have been replaced with a single default which sets the Content-Type to application/x-www-form-urlencoded.

An example config which satisfies the requirements for an SQS FIFO queue follows.

Given a request payload of:

{
  "event_type": "message",
  "event_id": "ABC123",
  "message": "Hello!"
}

Then the following config inside serverless.yml:

  apiGatewayServiceProxies:
    - sqs:
        path: /{version}/event/receiver
        method: post
        queueName:
          Fn::GetAtt:
            - eventQueue
            - "QueueName"
        request:
          template:
            application/json: |-
              #set ($body = $util.parseJson($input.body))
              Action=SendMessage##
              &MessageGroupId=$util.urlEncode($body.event_type)##
              &MessageDeduplicationId=$util.urlEncode($body.event_id)##
              &MessageAttribute.1.Name=$util.urlEncode("X-Custom-Signature")##
              &MessageAttribute.1.Value.DataType=String##
              &MessageAttribute.1.Value.StringValue=$util.urlEncode($input.params("X-Custom-Signature"))##
              &MessageBody=$util.urlEncode($input.body)

Will produce the following request body (substituting some values with tags):

Action=SendMessage&MessageGroupId=message&MessageDeduplicationId=ABC123&MessageAttribute.1.Name=X-Custom-Signature&MessageAttribute.1.Value.DataType=String&MessageAttribute.1.Value.StringValue=<related-header-value>&MessageBody=<url-encoded-request-payload>

It is usually possible to omit the url encoding of the $input.body as SQS does not seem to care too much about that, but I have not tested with any potentially problematic characters.

This PR is in response to #80

Allows the addition of `request.template.<content-type>` to `serverless.yml` under `apiGatewayServiceProxies`.

Changes the approach to passing basic `Action` and `MessageBody` values by passing them in the POST request payload instead of via querystrings. This means that the prior default `RequestParameters` have been replaced with a single default which sets the `Content-Type` to `application/x-www-form-urlencoded`.

An example config which satisfies the requirements for an SQS FIFO queue follows.

Given a request payload of:

```json
{
  "event_type": "message",
  "event_id": "ABC123",
  "message": "Hello!"
}
```

Then the following config inside `serverless.yml`:

```yaml
  apiGatewayServiceProxies:
    - sqs:
        path: /{version}/slack/receiver
        method: post
        queueName:
          Fn::GetAtt:
            - eventQueue
            - "QueueName"
        request:
          template:
            application/json: |-
              #set ($body = $util.parseJson($input.body))
              Action=SendMessage##
              &MessageGroupId=$util.urlEncode($body.event_type)##
              &MessageDeduplicationId=$util.urlEncode($body.event_id)##
              &MessageAttribute.1.Name=$util.urlEncode("X-Custom-Signature")##
              &MessageAttribute.1.Value.DataType=String##
              &MessageAttribute.1.Value.StringValue=$util.urlEncode($input.params("X-Custom-Signature"))##
              &MessageBody=$util.urlEncode($input.body)
```

Will produce the following request body (substituting some values with tags):

```
Action=SendMessage&MessageGroupId=message&MessageDeduplicationId=ABC123&MessageAttribute.1.Name=X-Custom-Signature&MessageAttribute.1.Value.DataType=String&MessageAttribute.1.Value.StringValue=<related-header-value>&MessageBody=<url-encoded-request-payload>
```

It is usually possible to omit the url encoding of the `$input.body` as SQS does not seem to care too much about that, but I have not tested with any potentially problematic characters.
@horike37
Copy link
Collaborator

horike37 commented Jul 1, 2020

@sambauers
Thank you for this PR 👍
The spec and implementation are almost good to me. But, I have one concern for the compatibility of previous versions.

Changes the approach to passing basic Action and MessageBody values by passing them in the POST request payload instead of via querystrings. This means that the prior default RequestParameters have been replaced with a single default which sets the Content-Type to application/x-www-form-urlencoded.

Does this mean to include a breaking change? If so, we would like to avoid that.

@sambauers
Copy link
Contributor Author

@horike37 I’ll see if I can make this backward compatible and update once I have a solution.

@horike37
Copy link
Collaborator

horike37 commented Jul 2, 2020

Thanks!

@sambauers
Copy link
Contributor Author

sambauers commented Jul 13, 2020

@horike37 I've attempted to cater for existing implementations by passing any querystring request params into the default request body. The limitation is that an implementation can not specify querystring request params and a custom request body. If both are present, the custom request body will always be used instead of the backward-compatible mapping. I've added two more tests to account for this.

Copy link
Collaborator

@horike37 horike37 left a comment

Choose a reason for hiding this comment

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

Thank you for this update 👍 LGTM.

@horike37 horike37 merged commit ff2ff8c into serverless-operations:master Aug 11, 2020
@horike37
Copy link
Collaborator

horike37 commented Aug 11, 2020

@sambauers
Hi. Unfortunately, this integration test was failed on top of master(This test only runs on master). These changes probably break this previous behavior.
https://travis-ci.org/github/serverless-operations/serverless-apigateway-service-proxy/jobs/717065155

Can you take a look? Otherwise, This PR will be reverted for fixing this failure.

@sambauers
Copy link
Contributor Author

@horike37 see #96 for the fix, with a more robust backward compatible logic.

@horike37
Copy link
Collaborator

🎉 This PR is included in version 1.11.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants