-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataServiceClient.cs
91 lines (81 loc) · 3.32 KB
/
DataServiceClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Text.Json;
namespace WinForms.Client
{
public static class DataServiceClient
{
static DataServiceClient()
{
string? url = System.Configuration.ConfigurationManager.AppSettings["baseUrl"];
if (string.IsNullOrEmpty(url))
throw new InvalidOperationException("The 'baseUrl' configuration setting is missing.");
baseUrl = url;
}
static string baseUrl;
static HttpClient CreateClient()
{
return new HttpClient();
}
public static async Task<DataFetchResult?> GetOrderItemsAsync(int skip, int take, string sortField, bool sortAscending)
{
using var client = CreateClient();
var response = await client.GetAsync(
$"{baseUrl}/data/OrderItems?skip={skip}&take={take}&sortField={sortField}&sortAscending={sortAscending}");
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
var dataFetchResult = JsonSerializer.Deserialize<DataFetchResult>(responseBody, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
return dataFetchResult;
}
static OrderItem? AsOrderItem(this string responseBody)
{
return JsonSerializer.Deserialize<OrderItem>(responseBody, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
}
public static async Task<OrderItem?> GetOrderItemAsync(int id)
{
using var client = CreateClient();
var response = await client.GetAsync($"{baseUrl}/data/OrderItem/{id}");
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
return responseBody.AsOrderItem();
}
public static async Task<OrderItem?> CreateOrderItemAsync(OrderItem orderItem)
{
using var client = CreateClient();
var response = await client.PostAsync($"{baseUrl}/data/OrderItem",
new StringContent(JsonSerializer.Serialize(orderItem), Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
return responseBody.AsOrderItem();
}
public static async Task UpdateOrderItemAsync(OrderItem orderItem)
{
using var client = CreateClient();
var response = await client.PutAsync($"{baseUrl}/data/OrderItem/{orderItem.Id}",
new StringContent(JsonSerializer.Serialize(orderItem), Encoding.UTF8, "application/json"));
response.EnsureSuccessStatusCode();
}
public static async Task<bool> DeleteOrderItemAsync(int id)
{
try
{
using var client = CreateClient();
var response = await client.DeleteAsync($"{baseUrl}/data/OrderItem/{id}");
response.EnsureSuccessStatusCode();
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return false;
}
}
}
}