|
| 1 | +--- |
| 2 | +sidebar_position: 3 |
| 3 | +tags: [HTTP Client, API Client, Dependency Injection] |
| 4 | +--- |
| 5 | + |
| 6 | +# API Client Usage |
| 7 | + |
| 8 | +There are a number of ways to create an API client for accessing the WebTrends API. The approach you take will depend on how your app works. |
| 9 | + |
| 10 | +:::tip[Examples] |
| 11 | + |
| 12 | +Examples on this page are using the C# default constructors feature for brevity. The approaches listed here will work with typical constructors. |
| 13 | + |
| 14 | +::: |
| 15 | + |
| 16 | +## Using Dependency Injection (DI) |
| 17 | + |
| 18 | +You can use DI to inject a client into your app code. |
| 19 | + |
| 20 | +### Using the default client |
| 21 | + |
| 22 | +The default client is pre-configured to use the configured `WebTrendsSettings`. The client will be disposed when the owning scope is disposed. |
| 23 | + |
| 24 | +```csharp |
| 25 | +public class MyService(IWebTrendsApiClient client) |
| 26 | +{ |
| 27 | + public async Task DoSomething() |
| 28 | + { |
| 29 | + // Do something with client. |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Using the API client factory |
| 35 | + |
| 36 | +It is possible to inject the API client factory and settings instead. You need to manage the lifetime of the client itself. |
| 37 | + |
| 38 | +```csharp |
| 39 | +public class MyService(IWebTrendsApiClientFactory clientFactory, WebTrendsSettings settings) |
| 40 | +{ |
| 41 | + public async Task DoSomething() |
| 42 | + { |
| 43 | + using var client = clientFactory.CreateApiClient(settings); |
| 44 | + |
| 45 | + // Do something with client. |
| 46 | + } |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +If you are not using the pre-configured `WebTrendsSettings`, you can provided your own: |
| 51 | + |
| 52 | +```csharp |
| 53 | +public class MyService(IWebTrendsApiClientFactory clientFactory) |
| 54 | +{ |
| 55 | + public async Task DoSomething() |
| 56 | + { |
| 57 | + var settings = new WebTrendsSettings(); |
| 58 | + using var client = clientFactory.CreateApiClient(settings); |
| 59 | + |
| 60 | + // Do something with client. |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +### Manually creating a client |
| 66 | + |
| 67 | +You can manually create a client yourself, but you are responsible for managing its lifetime. The following are some examples. |
| 68 | + |
| 69 | +#### Creating an API client manually |
| 70 | + |
| 71 | +```csharp |
| 72 | +var httpClient = new HttpClient(); |
| 73 | +var settings = new WebTrendsSettings(); |
| 74 | + |
| 75 | +var apiClient = new WebTrendsApiClient(httpClient, settings); |
| 76 | +``` |
| 77 | + |
| 78 | +#### Creating an API client factory manually |
| 79 | + |
| 80 | +```csharp |
| 81 | +var httpClientFactory = new WebTrendsHttpClientFactory(); |
| 82 | +var apiClientFactory = new WebTrendsApiClientFactory(httpClientFactory); |
| 83 | +var settings = new WebTrendsSettings(); |
| 84 | + |
| 85 | +var apiClient = apiClientFactory.CreateApiClient(settings); |
| 86 | +``` |
| 87 | + |
| 88 | +### Managing the HTTP Client |
| 89 | + |
| 90 | +If you need to control how the `HttpClient` is created, you can implement your own implementaton of `IWebTrendsHttpClientFactory`: |
| 91 | + |
| 92 | +```csharp |
| 93 | +public class MyWebTrendsHttpClientFactory : IWebTrendsHttpClientFactory |
| 94 | +{ |
| 95 | + public HttpClient CreateClient(string name) |
| 96 | + { |
| 97 | + return new HttpClient(); |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +If you are using the standard approach to dependency injection, you can register your own implementation, which will be used instead of the default implementation: |
| 103 | + |
| 104 | +```csharp |
| 105 | +services.AddWebTrends(); |
| 106 | +services.AddScoped<IWebTrendsHttpClientFactory, MyWebTrendsHttpClientFactory>(); |
| 107 | +``` |
0 commit comments