Skip to content

Instantly share code, notes, and snippets.

@rodion-m
Created January 23, 2022 16:25
Show Gist options
  • Save rodion-m/b0c3320252ff87dd87e0a30b02567435 to your computer and use it in GitHub Desktop.
Save rodion-m/b0c3320252ff87dd87e0a30b02567435 to your computer and use it in GitHub Desktop.
HTTP API Client example foe ASP.NET Core course
using System.Net.Http.Json;
public class ShopClient
{
private readonly string _host;
private readonly HttpClient _httpClient;
public ShopClient(string? host = null, HttpClient? httpClient = null)
{
_httpClient = httpClient ?? new HttpClient();
}
public async Task<IReadOnlyList<Order>> GetOrders()
{
var uri = $"{_host}/orders";
var response = await _httpClient.GetFromJsonAsync<IReadOnlyList<Order>>(uri);
return response!;
}
public async Task AddProduct(Order order)
{
var uri = $"{_host}/add_order";
await _httpClient.PostAsJsonAsync(uri, order);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment