Skip to content

Advanced usage

Anthony Steele edited this page Mar 4, 2015 · 1 revision

The Api wrapper advanced features are useful for are cases when you need to do non-default things with the 7digital Api wrapper.

For instrumenting, intercepting and caching the Api wrapper

UsingClient: Supply a custom IHttpClient for the request, typically wrapping the original one in order to intercept requests and responses for purposes such as logging or metrics.

UsingCache: allows you to integrate a cache where responses will be written to after a successful request, and later read from, for performance. See the page on Response caching for details.

For special http requests

The following methods are useful to and have the wrapper send a http request that it doesn't normally do. They are very useful in a few cases:

WithMethod: Manually specify the http method for the request.

WithPayload: Used to specify a request body as json, xml or form-url encoded. Useful when you need a post body.

WithAccept: specify the Accept header to send. We supply some default values:

  • AcceptFormat.XmlOnly – a header of Accept: application/xml will be sent.
  • AcceptFormat.JsonPreferred. – a header of Accept: application/json, application/xml;q=0.5 will be sent, which will prefer json, but accept xml as a second choice if json is not available.

The default value if this method is not called is the same as AcceptFormat.XmlOnly. The Json support is still a work in progress, so is not recommended for production use.

You can also specify any accept header value by using .WithAccept(new AcceptFormat("any accept format header goes here");

Response - Ending with Response() instead of Please() will issue the request and return the Response object as a Task<Response>. You can inspect the http status code, response headers and response body text. It will not be deserialised into an object. You are then responsible for deserialising this as you see fit.

ResponseAs is like Response(), but ending with ResponseAs<TR> will attempt to deserialise the response as the the supplied type TR, and returns an awaitable Task<TR>. The type TR does not need to be the same as the endpoint class T, you can supply any type here, but you are then responsible for ensuring that it is a match to the response.

UsingBaseUri - instead of using the configured base uri for the api, this redirects the request to another uri that you supply. This is useful for directing some kinds of requests to a stub or test endpoint. You are then responsible for supplying a correct base uri.

Clone this wiki locally