Created
January 23, 2022 16:25
-
-
Save rodion-m/b0c3320252ff87dd87e0a30b02567435 to your computer and use it in GitHub Desktop.
HTTP API Client example foe ASP.NET Core course
This file contains hidden or 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
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