-
Notifications
You must be signed in to change notification settings - Fork 3
/
petstore.cs
308 lines (266 loc) · 13.1 KB
/
petstore.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//nSwaggerVersion:0.0.6
// This file was automatically generated by nSwagger. Changes made to this file will be lost if nSwagger is run again. See https://github.com/rmaclean/nswagger for more information.
// This file was last generated at: 2016-04-08T12:38:30.1859435Z
namespace nSwagger
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
public class SwaggerPetstore
{
private readonly string url;
private readonly ISwaggerHTTPClient httpClient;
public SwaggerPetstore(string url = null, ISwaggerHTTPClient httpClient = null)
{
if (!string.IsNullOrWhiteSpace(url))
{
this.url = url;
}
else
{
this.url = "http://petstore.swagger.io";
}
if (httpClient == null)
{
this.httpClient = new SwaggerHTTPClient();
}
else
{
this.httpClient = httpClient;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("", "RECS0001:Class is declared partial but has only one part", Justification = "This is partial to allow the file to extended in a seperate file if needed. Changes to this file would be lost when the code is regenerated and so supporting a seperate file for this is ideal.")]
public partial class Pet
{
public long id
{
get;
set;
}
public string name
{
get;
set;
}
public string tag
{
get;
set;
}
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("", "RECS0001:Class is declared partial but has only one part", Justification = "This is partial to allow the file to extended in a seperate file if needed. Changes to this file would be lost when the code is regenerated and so supporting a seperate file for this is ideal.")]
public partial class Error
{
public int code
{
get;
set;
}
public string message
{
get;
set;
}
}
//<summary>
// finds pets in the system
//</summary>
//<returns>
// pet response
//</returns>
public async Task<APIResponse<Pet[]>> GetpetsAsync()
{
{
var response = await httpClient.GetAsync(new Uri(url + "/pets", UriKind.Absolute), new SwaggerHTTPClientOptions(TimeSpan.FromSeconds(30)));
if (response == null)
{
return new APIResponse<Pet[]>(false);
}
switch ((int)response.StatusCode)
{
case 200:
{
var data = JsonConvert.DeserializeObject<Pet[]>(await response.Content.ReadAsStringAsync());
return new APIResponse<Pet[]>(data, response.StatusCode);
}
case 200:
{
var data = JsonConvert.DeserializeObject<Pet[]>(await response.Content.ReadAsStringAsync());
return new APIResponse<Pet[]>(data, response.StatusCode);
}
default:
{
return new APIResponse<Pet[]>(response.StatusCode);
}
}
}
}
} public class SwaggerHTTPClientOptions
{
public TimeSpan Timeout { get; }
public SwaggerHTTPClientOptions(TimeSpan timeout)
{
Timeout = timeout;
}
}
public interface ISwaggerHTTPClient
{
Task<HttpResponseMessage> PutAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, HttpContent content = null, string token = null);
Task<HttpResponseMessage> PostAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, HttpContent content = null, string token = null);
Task<HttpResponseMessage> HeadAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, string token = null);
Task<HttpResponseMessage> OptionsAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, string token = null);
Task<HttpResponseMessage> PatchAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, HttpContent content, string token = null);
Task<HttpResponseMessage> DeleteAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, string token = null);
Task<HttpResponseMessage> GetAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, string token = null);
}
class SwaggerHTTPClient : ISwaggerHTTPClient
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "This is done interntionally as each place that calls this will dispose it")]
private static HttpClient CreateClient()
{
var cookieJar = new CookieContainer();
var httpHandler = new HttpClientHandler
{
CookieContainer = cookieJar,
AllowAutoRedirect = true,
UseCookies = true
};
var client = new HttpClient(httpHandler, true);
client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue
{
NoCache = true,
NoStore = true,
Private = true,
ProxyRevalidate = true,
MustRevalidate = true
};
return client;
}
public async Task<HttpResponseMessage> PutAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, HttpContent content = null, string token = null) => await HTTPCallAsync("put", uri, httpOptions, content, token);
public async Task<HttpResponseMessage> PostAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, HttpContent content = null, string token = null) => await HTTPCallAsync("post", uri, httpOptions, content, token);
private static async Task<HttpResponseMessage> HTTPCallAsync(string method, Uri uri, SwaggerHTTPClientOptions options, HttpContent content = null, string token = null)
{
using (var client = CreateClient())
{
using (var cancellationTokenSource = new CancellationTokenSource(options.Timeout))
{
var errorMessage = string.Empty;
try
{
if (content != null)
{
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
}
if (!string.IsNullOrWhiteSpace(token))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", token);
}
var response = default(HttpResponseMessage);
switch (method.ToUpperInvariant())
{
case "DELETE":
{
response = await client.DeleteAsync(uri, cancellationTokenSource.Token);
break;
}
case "POST":
{
response = await client.PostAsync(uri, content, cancellationTokenSource.Token);
break;
}
case "PUT":
{
response = await client.PutAsync(uri, content, cancellationTokenSource.Token);
break;
}
case "GET":
{
response = await client.GetAsync(uri, HttpCompletionOption.ResponseContentRead, cancellationTokenSource.Token);
break;
}
case "HEAD":
{
response = await client.SendAsync(new HttpRequestMessage
{
Method = new HttpMethod(method),
RequestUri = uri
}, HttpCompletionOption.ResponseHeadersRead, cancellationTokenSource.Token);
break;
}
case "OPTIONS":
{
response = await client.SendAsync(new HttpRequestMessage
{
Method = new HttpMethod(method),
RequestUri = uri
}, HttpCompletionOption.ResponseContentRead, cancellationTokenSource.Token);
break;
}
case "PATCH":
{
response = await client.SendAsync(new HttpRequestMessage
{
Method = new HttpMethod(method),
RequestUri = uri,
Content = content
}, HttpCompletionOption.ResponseContentRead, cancellationTokenSource.Token);
break;
}
}
#if DEBUG
Debug.WriteLine($"HTTP {method} to {uri} returned {response.StatusCode} with content {await response.Content?.ReadAsStringAsync()}");
#endif
return response;
}
catch (FileNotFoundException) { errorMessage = $"HTTP {method} exception - file not found exception"; /* this can happen if WP cannot resolve the server */ }
catch (WebException) { errorMessage = $"HTTP {method} exception - web exception"; }
catch (HttpRequestException) { errorMessage = $"HTTP {method} exception - http exception"; }
catch (TaskCanceledException) { errorMessage = $"HTTP {method} exception - task cancelled exception"; }
catch (UnauthorizedAccessException) { errorMessage = $"HTTP {method} exception - unauth exception"; }
#if DEBUG
Debug.WriteLine(errorMessage);
#endif
}
}
return null;
}
public async Task<HttpResponseMessage> HeadAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, string token = null) => await HTTPCallAsync("head", uri, httpOptions, token: token);
public async Task<HttpResponseMessage> OptionsAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, string token = null) => await HTTPCallAsync("options", uri, httpOptions, token: token);
public async Task<HttpResponseMessage> PatchAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, HttpContent content, string token = null) => await HTTPCallAsync("patch", uri, httpOptions, content, token: token);
public async Task<HttpResponseMessage> DeleteAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, string token = null) => await HTTPCallAsync("delete", uri, httpOptions, token: token);
public async Task<HttpResponseMessage> GetAsync(Uri uri, SwaggerHTTPClientOptions httpOptions, string token = null) => await HTTPCallAsync("get", uri, httpOptions, token: token);
}
public class APIResponse<T>
{
public APIResponse(dynamic data, HttpStatusCode statusCode) : this(statusCode)
{
Data = data;
}
public APIResponse(T successData, HttpStatusCode statusCode) : this(statusCode)
{
SuccessData = successData;
SuccessDataAvailable = true;
}
public bool Success { get; }
public APIResponse(bool success)
{
Success = success;
}
public APIResponse(HttpStatusCode statusCode) : this((int)statusCode >= 200 && (int)statusCode <= 299)
{
HTTPStatusCode = statusCode;
}
public dynamic Data { get; }
public T SuccessData { get; }
public HttpStatusCode? HTTPStatusCode { get; }
public bool SuccessDataAvailable { get; }
}
}