Skip to content

4 Response Variations

TimCodes.NET edited this page Dec 1, 2023 · 1 revision

Response variations are required when a single API endpoint can respond with different formats/models in different scenarios. An example of this may be a .NET API that sends a different model for a validation error.

Each IApiRequest therefore has an ApiResponseVariationResolver that is responsible for selecting the correct ApiResponseVariation based on the message received from the API. A resolver has a DefaultResponseVariation which is the variation selected if no other variation is matched.

There are helper methods included on the base client class to assist with creating resolvers:

  • GetResolver<TContent>(IApiResponseDeserializer? deserializer = null)
  • GetUntypedResolver(IApiResponseDeserializer? deserializer = null)

And some to help create ApiResponseVariations:

  • GetResponseVariation<TContent>(IApiResponseDeserializer? deserializer = null)
  • GetUntypedResponseVariation(IApiResponseDeserializer? deserializer = null)

If you need to add further variations to a resolver, you can simply add to the Variations list, which is a list of functions taking the response from the API as a parameter and returning an ApiResponseVariation if matched, otherwise null.

See an example by looking at the source code of the HttpApiClientBase helper method GetResolverWithDotNetBadRequestHandling<TContent>(IApiResponseDeserializer? deserializer = null).

Clone this wiki locally