Add API Gateway Accept header customization - #716
Conversation
jonathan343
left a comment
There was a problem hiding this comment.
I ran into this issue while doing my own testing with this client and was glad to see this PR existed!
Overall, the changes look good to me. I have a few suggestions. Let me know if you have any questions!
| self, context: RequestContext[Any, HTTPRequest] | ||
| ) -> HTTPRequest: | ||
| request = context.transport_request | ||
| request.fields.set_field(Field(name="Accept", values=["application/json"])) |
There was a problem hiding this comment.
There is an operation that has a member which targets the Accept header. Looks like it accepts a application/yaml value. If a customer provides this and we are unconditionally setting this for them which replaced their requested value. We should check if this headers is set, if it is we don't replace it. If it isn't set, then we set application/json.
Botocore handler for reference: https://github.com/boto/botocore/blob/develop/botocore/handlers.py#L719-L722
| class ApiGatewayAcceptHeaderInterceptor(Interceptor[Any, Any, HTTPRequest, None]): | ||
| """Sets the Accept header to application/json on API Gateway requests.""" | ||
|
|
||
| def modify_before_signing( |
There was a problem hiding this comment.
Just curious, do other SDKs also do this logic under modify_before_signing?
There was a problem hiding this comment.
Can this entire interceptor be written at the Java layer?
Although it's a small amount of code, I think service specific interceptors should live inside the service package. If a customer only uses s3, they shouldn't need to pull in code that doesn't apply to them.
Again, not a big deal now, but would be nice if we can.
Context
While generating an API Gateway client from its model locally and running integration tests, several operations failed to deserialize their responses. For example,
CreateRestApireturns acreatedDatemember modeled as a timestamp (epoch-seconds underrestJson1), but the response returned an ISO-8601 string, so timestamp deserialization failed:The root cause is API Gateway specific. Unless the client sends
Accept: application/json, API Gateway responds withapplication/hal+json, in which timestamps are serialized as ISO-8601 strings instead of the epoch-seconds that therestJson1protocol expects.This is a documented API Gateway client requirement, not general protocol behavior. The Smithy spec calls it out as a service-specific customization: Amazon API Gateway Customizations. Other Smithy SDKs already implement this same customization in their codegen (e.g. smithy-rs, smithy-go, smithy-kotlin). This change adds the equivalent to smithy-python.
Changes
smithy-aws-core): a newApiGatewayAcceptHeaderInterceptorthat setsAccept: application/jsonon every request inmodify_before_signing.codegen/aws/core): a newApiGatewayIntegration, registered with thePythonIntegrationSPI. When the service being generated is API Gateway, it emits a plugin that registers the interceptor on the client; for all other services it does nothing. It lives under a newcustomizations/apigateway/package, leaving room for the other service customizations the Smithy spec lists (e.g. Glacier, S3, Machine Learning).Generated clients for the API Gateway service now include an
accept_header.pyplugin that appends the interceptor.Testing
smithy-aws-core): verify the interceptor setsAccept: application/json, and that it overrides any pre-existingAcceptheader. Both pass.smithy-aws-corestill pass.client.pyregisters theaccept_header_pluginand that the plugin appendsApiGatewayAcceptHeaderInterceptor.Without the customization this raises
JSONTokenError: Expected token of type 'number' at path 'createdDate', but found: 'string'.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.