Skip to content

Commit

Permalink
fix: Update readme with details on supply a custom HttpClient instance (
Browse files Browse the repository at this point in the history
#1362)

Co-authored-by: Chris Howarth <christopher.howarth@autocab.com>
Co-authored-by: Glenn <5834289+glennawatson@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 12, 2023
1 parent 098c4e3 commit 25c4840
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ services
* [Headers inheritance](#headers-inheritance)
* [Default Interface Methods](#default-interface-methods)
* [Using HttpClientFactory](#using-httpclientfactory)
* [Providing a custom HttpClient](#providing-a-custom-httpclient)
* [Handling exceptions](#handling-exceptions)
* [When returning Task&lt;IApiResponse&gt;, Task&lt;IApiResponse&lt;T&gt;&gt;, or Task&lt;ApiResponse&lt;T&gt;&gt;](#when-returning-taskiapiresponse-taskiapiresponset-or-taskapiresponset)
* [When returning Task&lt;T&gt;](#when-returning-taskt)
Expand Down Expand Up @@ -1155,6 +1156,48 @@ public class HomeController : Controller
}
```

### Providing a custom HttpClient

You can supply a custom `HttpClient` instance by simply passing it as a parameter to the `RestService.For<T>` method:

```csharp
RestService.For<ISomeApi>(new HttpClient()
{
BaseAddress = new Uri("https://www.someapi.com/api/")
});
```

However, when supplying a custom `HttpClient` instance the following `RefitSettings` properties will not work:

* `AuthorizationHeaderValueGetter`
* `AuthorizationHeaderValueWithParamGetter`
* `HttpMessageHandlerFactory`

If you still want to be able to configure the `HtttpClient` instance that `Refit` provides while still making use of the above settings, simply expose the `HttpClient` on the API interface:

```csharp
interface ISomeApi
{
// This will automagically be populated by Refit if the property exists
HttpClient Client { get; }

[Headers("Authorization: Bearer")]
[Get("/endpoint")]
Task<string> SomeApiEndpoint();
}
```

Then, after creating the REST service, you can set any `HttpClient` property you want, e.g. `Timeout`:

```csharp
SomeApi = RestService.For<ISomeApi>("https://www.someapi.com/api/", new RefitSettings()
{
AuthorizationHeaderValueGetter = () => GetTokenAsync()
});

SomeApi.Client.Timeout = timeout;
```

### Handling exceptions
Refit has different exception handling behavior depending on if your Refit interface methods return `Task<T>` or if they return `Task<IApiResponse>`, `Task<IApiResponse<T>>`, or `Task<ApiResponse<T>>`.

Expand Down

0 comments on commit 25c4840

Please sign in to comment.