Replies: 1 comment 6 replies
-
I might have to add a possible return value to the list:
If we weren't interested in OpenAPI compatibility this would be a breeze, but since we are we need to expose some metadata here statically. By that I mean, by analyzing the function return value signature we need to know the names of the headers we're returning and their types. Let's say we want to return two headers, One option would be making the users define a class and return an instance of that.
This works fine, but I just find it annoying a class needs to be defined for each combination of headers. So let's look at more... esoteric solutions. One solution would be to, instead of a class, we make you return a tuple of tuples. The inner tuples contain the name of the header as a string literal, and the type of the value.
This works fine (and is type-checked by Mypy 0.920+), except the type signature is very hairy. It can be helped by some aliasing, like Hynek suggested.
And I guess a third alternative would be our old friend
It's also a little more DRY. |
Beta Was this translation helpful? Give feedback.
-
A very simple spec for return values from attrsapi handlers.
I'm avoiding the discussion of content types for now, since that discussion will be more complex and can be dealt with later.
Here's what an attrsapi handler may return:
starlette.responses.Response
,flask.Response
, etc). This is an escape hatch if a user needs to return something that's not supported. This cannot be statically parsed so it won't show up in the OpenAPI schemastr
- will be passed to the framework response class as text, and probably UTF8 encoded by the framework (I think they all do that)bytes
- will be passed to the framework directlyNone
, or no annotation - no content is returned, status code 200str | bytes | attrs | dataclass
. The first type argument (the int literal) will be used as the status code, and the other processed as usualBeta Was this translation helpful? Give feedback.
All reactions