Created
December 7, 2019 10:59
-
-
Save emrekizildas/1e3ed62c6b41a62d587d35ed01beeb90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ExampleService | |
{ | |
public HttpClient Client { get; } | |
public ExampleService(HttpClient client) | |
{ | |
client.BaseAddress = new Uri("https://api.example.com/"); | |
client.DefaultRequestHeaders.Add("Accept","application/json"); | |
client.DefaultRequestHeaders.Add("User-Agent","HttpClientFactory-Sample"); | |
Client = client; | |
} | |
public async Task<IEnumerable<User>> GetAllUsers() | |
{ | |
var response = await Client.GetAsync( | |
"/users/getall"); | |
response.EnsureSuccessStatusCode(); | |
using var responseStream = await response.Content.ReadAsStreamAsync(); | |
return await JsonSerializer.DeserializeAsync | |
<IEnumerable<User>>(responseStream); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment