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

What if there are arbitrary items in POST Data?(question) #46

Open
mqnowa opened this issue Nov 30, 2024 · 1 comment
Open

What if there are arbitrary items in POST Data?(question) #46

mqnowa opened this issue Nov 30, 2024 · 1 comment
Labels
question Further information is requested

Comments

@mqnowa
Copy link

mqnowa commented Nov 30, 2024

What if there are arbitrary items in POST Data?
In Python there is no “undefined” in javascript. So, if I want to manipulate the existence of a property, I have no choice but to pass a dict to POST.
But I want to use dataclass so that I can write programs with no documentation and only intellisense.
Is there a special variable or other solution that would prevent me from including that property in the data I post?

Translated with DeepL.com (free version)

@dataclass
class PostBody():
    required_property: str
    optional_property: str = UNDEFINED

class MyClient(RequestsClient):
    def __init__(): ...

    @post("/api/path")
    def post_api_path(self, body: PostBody) -> ResponceDataclass:
        pass

client = MyClient()
res = client.post_api_path(PostBody("foooo"))
# Posted Body: {"required_property": "foooo"}
@Tishka17
Copy link
Member

One of the options is using TypedDict, it is like dict, but IDE will recognize supported keys.

But probably you are looking for skipping keys with default values during serialization.
Here is such an option: https://adaptix.readthedocs.io/en/latest/loading-and-dumping/extended-usage.html#omit-default

To apply that you need to override _init_request_body_factory in your Client class. It whould be something like:

class MyClient(RequestsClient):
    ...

    def _init_request_body_factory(self) -> Retort:
        return Retort(
            recipe=[
                name_mapping(PostBody, omit_default=True),
            ],
        )

@Tishka17 Tishka17 added the question Further information is requested label Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants