Skip to content

Instantly share code, notes, and snippets.

@emrekizildas
Created December 7, 2019 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emrekizildas/1e3ed62c6b41a62d587d35ed01beeb90 to your computer and use it in GitHub Desktop.
Save emrekizildas/1e3ed62c6b41a62d587d35ed01beeb90 to your computer and use it in GitHub Desktop.
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