-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPetStoreClient.cs
More file actions
240 lines (201 loc) · 6.79 KB
/
PetStoreClient.cs
File metadata and controls
240 lines (201 loc) · 6.79 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#nullable enable
// <auto-generated />
#pragma warning disable
using System.Text.Json;
using System.Text;
namespace PetStore;
public class ApiResponse
{
public int Code { get; set; }
public string? Type { get; set; }
public string? Message { get; set; }
}
public class Category
{
public long Id { get; set; }
public string? Name { get; set; }
}
public class Pet
{
public long Id { get; set; }
public Category? Category { get; set; }
public string Name { get; set; }
public List<string> PhotoUrls { get; set; }
public List<Tag>? Tags { get; set; }
public string? Status { get; set; }
}
public class Tag
{
public long Id { get; set; }
public string? Name { get; set; }
}
public class Order
{
public long Id { get; set; }
public long PetId { get; set; }
public int Quantity { get; set; }
public DateTime? ShipDate { get; set; }
public string? Status { get; set; }
public bool Complete { get; set; }
}
public class User
{
public long Id { get; set; }
public string? Username { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
public string? Phone { get; set; }
public int UserStatus { get; set; }
}
public class PetStoreClient
{
private readonly HttpClient _httpClient;
private readonly string _baseUrl;
public PetStoreClient(HttpClient httpClient, string baseUrl = "")
{
_httpClient = httpClient;
_baseUrl = baseUrl;
}
public async Task<ApiResponse?> UploadFile(int petId, object body)
{
var path = $"/pet/{petId}/uploadImage";
return await SendRequestAsync<ApiResponse>(path, HttpMethod.Post, body);
}
public async Task UpdatePet(Pet body)
{
var path = $"/pet";
await SendRequestAsync(path, HttpMethod.Put, body);
}
public async Task AddPet(Pet body)
{
var path = $"/pet";
await SendRequestAsync(path, HttpMethod.Post, body);
}
public async Task<List<Pet>?> FindPetsByStatus(object[]? status = null)
{
var queryParams = new List<string>();
if (status != null)
queryParams.Add($"status={status}");
var path = "/pet/findByStatus" + (queryParams.Any() ? "?" + string.Join("&", queryParams) : "");
return await SendRequestAsync<List<Pet>>(path, HttpMethod.Get);
}
public async Task<List<Pet>?> FindPetsByTags(object[]? tags = null)
{
var queryParams = new List<string>();
if (tags != null)
queryParams.Add($"tags={tags}");
var path = "/pet/findByTags" + (queryParams.Any() ? "?" + string.Join("&", queryParams) : "");
return await SendRequestAsync<List<Pet>>(path, HttpMethod.Get);
}
public async Task<Pet?> GetPetById(int petId)
{
var path = $"/pet/{petId}";
return await SendRequestAsync<Pet>(path, HttpMethod.Get);
}
public async Task UpdatePetWithForm(int petId, object body)
{
var path = $"/pet/{petId}";
await SendRequestAsync(path, HttpMethod.Post, body);
}
public async Task DeletePet(int petId)
{
var path = $"/pet/{petId}";
await SendRequestAsync(path, HttpMethod.Delete);
}
public async Task<object?> GetInventory()
{
var path = $"/store/inventory";
return await SendRequestAsync<object>(path, HttpMethod.Get);
}
public async Task<Order?> PlaceOrder(Order body)
{
var path = $"/store/order";
return await SendRequestAsync<Order>(path, HttpMethod.Post, body);
}
public async Task<Order?> GetOrderById(int orderId)
{
var path = $"/store/order/{orderId}";
return await SendRequestAsync<Order>(path, HttpMethod.Get);
}
public async Task DeleteOrder(int orderId)
{
var path = $"/store/order/{orderId}";
await SendRequestAsync(path, HttpMethod.Delete);
}
public async Task CreateUsersWithListInput(List<User> body)
{
var path = $"/user/createWithList";
await SendRequestAsync(path, HttpMethod.Post, body);
}
public async Task<User?> GetUserByName(string username)
{
var path = $"/user/{username}";
return await SendRequestAsync<User>(path, HttpMethod.Get);
}
public async Task UpdateUser(string username, User body)
{
var path = $"/user/{username}";
await SendRequestAsync(path, HttpMethod.Put, body);
}
public async Task DeleteUser(string username)
{
var path = $"/user/{username}";
await SendRequestAsync(path, HttpMethod.Delete);
}
public async Task<string?> LoginUser(string? username = null, string? password = null)
{
var queryParams = new List<string>();
if (username != null)
queryParams.Add($"username={username}");
if (password != null)
queryParams.Add($"password={password}");
var path = "/user/login" + (queryParams.Any() ? "?" + string.Join("&", queryParams) : "");
return await SendRequestAsync<string>(path, HttpMethod.Get);
}
public async Task LogoutUser()
{
var path = $"/user/logout";
await SendRequestAsync(path, HttpMethod.Get);
}
public async Task CreateUsersWithArrayInput(List<User> body)
{
var path = $"/user/createWithArray";
await SendRequestAsync(path, HttpMethod.Post, body);
}
public async Task CreateUser(User body)
{
var path = $"/user";
await SendRequestAsync(path, HttpMethod.Post, body);
}
private async Task<T?> SendRequestAsync<T>(string path, HttpMethod method, object? body = null)
{
var request = new HttpRequestMessage(method, _baseUrl + path);
if (body != null)
{
var json = JsonSerializer.Serialize(body);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
if (string.IsNullOrEmpty(responseContent))
return default;
return JsonSerializer.Deserialize<T>(responseContent, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
});
}
private async Task SendRequestAsync(string path, HttpMethod method, object? body = null)
{
var request = new HttpRequestMessage(method, _baseUrl + path);
if (body != null)
{
var json = JsonSerializer.Serialize(body);
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
}
var response = await _httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
}
}