Request Serializer¶
- class blacksmith.service.http_body_serializer.AbstractHttpBodySerializer¶
Request body serializer.
- abstractmethod accept(content_type: str) bool¶
Return true in case it can handle the request.
- abstractmethod serialize(body: dict[str, Any] | Sequence[Any]) str | bytes | Iterable[bytes] | AsyncIterable[bytes] | Mapping[str, str | int | float | bool]¶
Serialize a python simple types to a python request body.
The body received here is the extracted object from the request model.
- abstractmethod deserialize(body: bytes, encoding: str | None) Any | None¶
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: dict[str, Any] | Sequence[Any]) str | bytes | Iterable[bytes] | AsyncIterable[bytes] | Mapping[str, str | int | float | bool]¶
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: str | None) Any | None¶
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: dict[str, Any] | Sequence[Any]) str | bytes | Iterable[bytes] | AsyncIterable[bytes] | Mapping[str, str | int | float | bool]¶
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: str | None) Any | None¶
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 super().default(o)
- blacksmith.service.http_body_serializer.get_fields(model: BaseModel) Mapping[str, FieldInfo]¶
- blacksmith.service.http_body_serializer.get_location(field: FieldInfo) Literal['path', 'headers', 'querystring', 'body', 'attachment']¶
- blacksmith.service.http_body_serializer.get_value(v: str | int | float | bool | SecretStr | SecretBytes | dict[str, str | int | float | bool] | list[str | int | float | bool], is_body: bool) str | int | float | bool¶
- blacksmith.service.http_body_serializer.serialize_part(req: Request, part: dict[str, Any], loc: Literal['path', 'headers', 'querystring', 'body', 'attachment']) dict[str, str | int | float | bool]¶
- blacksmith.service.http_body_serializer.register_http_body_serializer(serializer: AbstractHttpBodySerializer) None¶
Register a serializer to serialize some kind of request.
- blacksmith.service.http_body_serializer.unregister_http_body_serializer(serializer: AbstractHttpBodySerializer) None¶
Unregister a serializer previously added.
Usefull for testing purpose.
- blacksmith.service.http_body_serializer.serialize_request_body(req: Request, body: dict[str, str], content_type: str | None = None) str | bytes | Iterable[bytes] | AsyncIterable[bytes] | Mapping[str, str | int | float | bool]¶
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_attachment(req: Request, attachments: dict[str, Attachment], content_type: str | None = None) dict[str, tuple[str, bytes, str | None, Mapping[str, str]]] | None¶
- blacksmith.service.http_body_serializer.serialize_request(method: Literal['HEAD', 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'], url_pattern: str, request_model: Request) HTTPRequest¶
Serialize
blacksmith.Requestsubclasses 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.HTTPRequestis a string, here, serialized by a registered serializer.
- blacksmith.service.http_body_serializer.serialize_response(resp: HTTPRawResponse) 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.