Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api-gateway): add support for custom serializer #568

Merged
merged 5 commits into from
Jul 30, 2021

Conversation

michaelbrewer
Copy link
Contributor

@michaelbrewer michaelbrewer commented Jul 27, 2021

Issue #, if available:

Description of changes:

Add support a custom serializer to allow for:

  • Output to xml
  • Support for serializing Enum or Sets and other complex types
  • Support alternative json libraries like ujson or orjson

Example usage:

import json
from enum import Enum
from json import JSONEncoder
from typing import Dict

class CustomEncoder(JSONEncoder):
    """Your customer json encoder"""
    def default(self, obj):
        if isinstance(obj, Enum):
            return obj.value
        try:
            iterable = iter(obj)
        except TypeError:
            pass
        else:
            return sorted(iterable)
        return JSONEncoder.default(self, obj)

def custom_serializer(obj) -> str:
    """Your custom serializer - can even output xml 😨"""
    return json.dumps(obj, cls=CustomEncoder)

# Assigning your custom serializer
app = ApiGatewayResolver(serializer=custom_serializer)

class Color(Enum):
    RED = 1
    BLUE = 2

@app.get("/colors")
def get_color() -> Dict:
    return {
        "color": Color.RED,
        "variations": {"light", "dark"},
    }

Checklist

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 27, 2021
@boring-cyborg boring-cyborg bot added the tests label Jul 27, 2021
@michaelbrewer michaelbrewer changed the title feat(event-handler): allow for a custom serializer feat(event-handler): api gateway handler allow for a custom serializer Jul 27, 2021
@michaelbrewer michaelbrewer changed the title feat(event-handler): api gateway handler allow for a custom serializer feat(event-handler): api gateway handler add support for a custom serializer Jul 27, 2021
@codecov-commenter
Copy link

codecov-commenter commented Jul 27, 2021

Codecov Report

Merging #568 (f5f4123) into develop (1135314) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #568   +/-   ##
========================================
  Coverage    99.86%   99.86%           
========================================
  Files          113      113           
  Lines         4485     4489    +4     
  Branches       243      244    +1     
========================================
+ Hits          4479     4483    +4     
  Misses           3        3           
  Partials         3        3           
Impacted Files Coverage Δ
aws_lambda_powertools/event_handler/api_gateway.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1135314...f5f4123. Read the comment docs.

@michaelbrewer
Copy link
Contributor Author

@marcioemiranda @heitorlessa my initial PR is up

@michaelbrewer michaelbrewer marked this pull request as draft July 27, 2021 17:01
@michaelbrewer michaelbrewer marked this pull request as ready for review July 28, 2021 19:41
@heitorlessa
Copy link
Contributor

Thanks a lot Mike, including the illustrious XML example haha

Q: Do we need a deserializer for event source data classes too?

@michaelbrewer
Copy link
Contributor Author

Thanks a lot Mike, including the illustrious XML example haha

i should try it out :)

Q: Do we need a deserializer for event source data classes too?

Like we did for idempotent utility?

@heitorlessa
Copy link
Contributor

heitorlessa commented Jul 30, 2021

Like we did for Logger: https://awslabs.github.io/aws-lambda-powertools-python/latest/core/logger/#bring-your-own-json-serializer

It can be done in a separate PR. This PR already addresses the serialization ask, deserializing would be a sensible ask by anyone looking to understand why orjson hasn't optimized their high traffic API end-to-end (json_body uses json.loads)

Note: AFAIK XML wouldn't work here because this feature requires API GW Proxy Resource.

@heitorlessa heitorlessa added the feature New feature or functionality label Jul 30, 2021
@heitorlessa heitorlessa added this to the 1.19.0 milestone Jul 30, 2021
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

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

Syntactic sugar to ease maintenance later

aws_lambda_powertools/event_handler/api_gateway.py Outdated Show resolved Hide resolved
Co-authored-by: Heitor Lessa <heitor.lessa@hotmail.com>
@michaelbrewer
Copy link
Contributor Author

Like we did for Logger: https://awslabs.github.io/aws-lambda-powertools-python/latest/core/logger/#bring-your-own-json-serializer

It can be done in a separate PR. This PR already addresses the serialization ask, deserializing would be a sensible ask by anyone looking to understand why orjson hasn't optimized their high traffic API end-to-end (json_body uses json.loads)

Note: AFAIK XML wouldn't work here because this feature requires API GW Proxy Resource.

In the BaseProxyEvent.json_body method?

@heitorlessa
Copy link
Contributor

In the BaseProxyEvent.json_body method?

Yep. Any places where we'd deserialize, and one would want to have orjson or something else with their defaults in place... since we're allowing them for serialization.

I'm merging this as is now, thanks a lot!!!!!!

@heitorlessa heitorlessa changed the title feat(event-handler): api gateway handler add support for a custom serializer feat(api-gateway): add support for custom serializer Jul 30, 2021
@heitorlessa heitorlessa merged commit 8fcb23b into aws-powertools:develop Jul 30, 2021
@marcioemiranda
Copy link

Thanks a lot, this looks good. Any idea when this feature would be released?

@michaelbrewer michaelbrewer deleted the api-gw-custom-serializer branch July 31, 2021 14:58
@heitorlessa
Copy link
Contributor

heitorlessa commented Jul 31, 2021 via email

michaelbrewer added a commit to gyft/aws-lambda-powertools-python that referenced this pull request Aug 3, 2021
)

Co-authored-by: Heitor Lessa <heitor.lessa@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants