Request Serializer#
- class blacksmith.service.http_body_serializer.AbstractHttpBodySerializer#
Request body serializer.
- abstract accept(content_type: str) bool #
Return true in case it can handle the request.
- abstract serialize(body: Union[Dict[str, Any], Sequence[Any]]) Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]] #
Serialize a python simple types to a python request body.
The body received here is the extracted object from the request model.
- abstract deserialize(body: bytes, encoding: Optional[str]) Optional[Any] #
Deserialize a raw http response body to a python simple types representation.
- class blacksmith.service.http_body_serializer.JsonRequestSerializer#
The default serializer that serialize to json
- accept(content_type: str) bool #
Return true in case it can handle the request.
- serialize(body: Union[Dict[str, Any], Sequence[Any]]) Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]] #
Serialize a python simple types to a python request body.
The body received here is the extracted object from the request model.
- deserialize(body: bytes, encoding: Optional[str]) Optional[Any] #
Deserialize a raw http response body to a python simple types representation.
- class blacksmith.service.http_body_serializer.UrlencodedRequestSerializer#
A serializer for application/x-www-form-urlencoded request.
- accept(content_type: str) bool #
Return true in case it can handle the request.
- serialize(body: Union[Dict[str, Any], Sequence[Any]]) Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]] #
Serialize a python simple types to a python request body.
The body received here is the extracted object from the request model.
- deserialize(body: bytes, encoding: Optional[str]) Optional[Any] #
Deserialize a raw http response body to a python simple types representation.
- class blacksmith.service.http_body_serializer.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)#
- default(o: Any) Any #
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- blacksmith.service.http_body_serializer.get_fields(model: pydantic.main.BaseModel) Mapping[str, pydantic.fields.FieldInfo] #
- blacksmith.service.http_body_serializer.get_location(field: Any) Literal['path', 'headers', 'querystring', 'body'] #
- blacksmith.service.http_body_serializer.get_value(v: Union[str, int, float, bool, pydantic.types.SecretStr, pydantic.types.SecretBytes]) Union[str, int, float, bool] #
- blacksmith.service.http_body_serializer.serialize_part(req: blacksmith.domain.model.params.Request, part: Dict[str, Any]) Dict[str, Union[str, int, float, bool]] #
- blacksmith.service.http_body_serializer.register_http_body_serializer(serializer: blacksmith.service.http_body_serializer.AbstractHttpBodySerializer) None #
Register a serializer to serialize some kind of request.
- blacksmith.service.http_body_serializer.unregister_http_body_serializer(serializer: blacksmith.service.http_body_serializer.AbstractHttpBodySerializer) None #
Unregister a serializer previously added.
Usefull for testing purpose.
- blacksmith.service.http_body_serializer.serialize_request_body(req: blacksmith.domain.model.params.Request, body: Dict[str, str], content_type: Optional[str] = None) Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]] #
Serialize the body of the request.
Note that the content_type is optional, but if it is set,
the request will contains
- blacksmith.service.http_body_serializer.serialize_request(method: Literal['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], url_pattern: str, request_model: blacksmith.domain.model.params.Request) blacksmith.domain.model.http.HTTPRequest #
Serialize
blacksmith.Request
subclasses toblacksmith.HTTPRequest
.While processing an http request, the request models are serialize to an intermediate object
blacksmith.HTTPRequest
, that will be passed over middleware and finally to the transport in order to build the final http request.Note that the body of the
blacksmith.HTTPRequest
is a string, here, serialized by a registered serializer.
- blacksmith.service.http_body_serializer.serialize_response(resp: blacksmith.domain.model.http.HTTPRawResponse) blacksmith.domain.model.http.HTTPResponse #
Deserialize an http response to the http intermediate representation that will become the pydantic based response. Basically it parse json bytes a a python structure. But this function is here to supports serializations format depending on the content-type.