-
Notifications
You must be signed in to change notification settings - Fork 0
3 Serialization
Requests are serialized with an IApiRequestSerializer. They take an IApiRequest and convert it to the type needed to communicate with an API. When dealing with HTTP, that's an HttpRequestMessage. There are two levels where you can choose the serializer, one as a DefaultRequestSerlializer on your implementation of the IApiClient, the other is to set the serializer against the IApiRequest. If you give a serializer to an IApiRequest directly, it will override the DefaultRequestSerlializer specified on the client.
A few serializers are included for you out-the-box:
Does nothing whatsoever with your request
Serializes objects into an application/json request with camel casing.
Serializes objects to a URL encoded form request. Use the HttpFormNameAttribute to specify how property names should be converted to the form encoding. Alternatively, you can pass a string dictionary as the payload.
Serializes objects into a text/plain request by just calling ToString on your object.
You are welcome to implement your own serialization by implementing IApiRequestSerializer. The HttpRequestMessage is available when the IApiRequest you receive in Serialize is of type HttpApiRequestBase.
Responses are deserialized by an IApiResponseDeserializer. They take the API response and convert it to some usable POCO IApiResponse. These are assigned to the matched ApiResponseVariation (more on those in following pages - essentially they describe an expected response format from the API server). You can set a default in your client by assigning to DefaultResponeDeserlializer.
The following deserializers are provided out-the-box:
Deserializes an application/json response into the expected type.
Deserializes a byte array response.
Deserializes the response you get from a .NET API when you have validation issues in your model. Returns a DotNetValidationProblem.
Does nothing with the response.
Maintains the incoming stream from the API for the caller to process themselves.
Converts a text/plain response to a string return.
To implement your own HTTP deserializing, inherit from HttpApiResponseDeserializerBase and implement the DeserializeAsync method. You can then assign or deserializer to the client or the response variation.