python-lapidary / lapidary

Write Web API clients using annotations in python
https://lapidary.dev
MIT License
11 stars 0 forks source link

Add defaut mapping to RequstBody #44

Open rafalkrupinski opened 8 months ago

rafalkrupinski commented 8 months ago

The typical usage of RequestBody annotation is:

body: Annotated(RequestBodyType, RequestBody({'application/json': RequestBodyType}))

with a single key-value pair, where

These alternative shortcut forms could be allowed, to better support human authors:

body: Annotated(RequestBodyType, RequestBody())  # only accept 'application/json' and map the body to ResultType

body: Annotated(RequestBodyType, RequestBody('application/json'))  # only accept the given media type, and map to ResultType
rafalkrupinski commented 8 months ago

Same for maps in Responses/code maps:

Responses({
            '200': {
                'application/json': ResultType,
            }
        })
# same as 
Responses({
            '200': ResultType,
            }
        })

Not adding option of omitting the response code, to encourage declaring errors.

rafalkrupinski commented 1 month ago

Lapidary shouldn't read type hints, only WebArg annotations.

Annotated[ResultType, Responses({'200': ResultType})] # OK - matches content-type: application/json
Annotated(RequestBodyType, Body(RequestBodyType) # OK - sends content-type: application/json