| | 1 | | /* |
| | 2 | | * WeShare API |
| | 3 | | * |
| | 4 | | * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) |
| | 5 | | * |
| | 6 | | * The version of the OpenAPI document: 1.0 |
| | 7 | | * Generated by: https://github.com/openapitools/openapi-generator.git |
| | 8 | | */ |
| | 9 | |
|
| | 10 | |
|
| | 11 | | using System; |
| | 12 | | using System.Collections; |
| | 13 | | using System.Collections.Generic; |
| | 14 | | using System.Globalization; |
| | 15 | | using System.IO; |
| | 16 | | using System.Linq; |
| | 17 | | using System.Net; |
| | 18 | | using System.Reflection; |
| | 19 | | using System.Runtime.Serialization; |
| | 20 | | using System.Runtime.Serialization.Formatters; |
| | 21 | | using System.Text; |
| | 22 | | using System.Threading; |
| | 23 | | using System.Text.RegularExpressions; |
| | 24 | | using System.Threading.Tasks; |
| | 25 | | using Newtonsoft.Json; |
| | 26 | | using Newtonsoft.Json.Serialization; |
| | 27 | | using RestSharp; |
| | 28 | | using RestSharp.Serializers; |
| | 29 | | using RestSharpMethod = RestSharp.Method; |
| | 30 | | using Polly; |
| | 31 | |
|
| | 32 | | namespace Applications.WeShare.Swagger.Client |
| | 33 | | { |
| | 34 | | /// <summary> |
| | 35 | | /// Allows RestSharp to Serialize/Deserialize JSON using our custom logic, but only when ContentType is JSON. |
| | 36 | | /// </summary> |
| | 37 | | internal class CustomJsonCodec : IRestSerializer, ISerializer, IDeserializer |
| | 38 | | { |
| | 39 | | private readonly IReadableConfiguration _configuration; |
| 0 | 40 | | private static readonly string _contentType = "application/json"; |
| 75 | 41 | | private readonly JsonSerializerSettings _serializerSettings = new JsonSerializerSettings |
| 75 | 42 | | { |
| 75 | 43 | | // OpenAPI generated types generally hide default constructors. |
| 75 | 44 | | ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, |
| 75 | 45 | | ContractResolver = new DefaultContractResolver |
| 75 | 46 | | { |
| 75 | 47 | | NamingStrategy = new CamelCaseNamingStrategy |
| 75 | 48 | | { |
| 75 | 49 | | OverrideSpecifiedNames = false |
| 75 | 50 | | } |
| 75 | 51 | | } |
| 75 | 52 | | }; |
| | 53 | |
|
| 0 | 54 | | public CustomJsonCodec(IReadableConfiguration configuration) |
| 0 | 55 | | { |
| 0 | 56 | | _configuration = configuration; |
| 0 | 57 | | } |
| | 58 | |
|
| 75 | 59 | | public CustomJsonCodec(JsonSerializerSettings serializerSettings, IReadableConfiguration configuration) |
| 75 | 60 | | { |
| 75 | 61 | | _serializerSettings = serializerSettings; |
| 75 | 62 | | _configuration = configuration; |
| 75 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// Serialize the object into a JSON string. |
| | 67 | | /// </summary> |
| | 68 | | /// <param name="obj">Object to be serialized.</param> |
| | 69 | | /// <returns>A JSON string.</returns> |
| | 70 | | public string Serialize(object obj) |
| 7 | 71 | | { |
| 7 | 72 | | if (obj != null && obj is Applications.WeShare.Swagger.Model.AbstractOpenAPISchema) |
| 0 | 73 | | { |
| | 74 | | // the object to be serialized is an oneOf/anyOf schema |
| 0 | 75 | | return ((Applications.WeShare.Swagger.Model.AbstractOpenAPISchema)obj).ToJson(); |
| | 76 | | } |
| | 77 | | else |
| 7 | 78 | | { |
| 7 | 79 | | return JsonConvert.SerializeObject(obj, _serializerSettings); |
| | 80 | | } |
| 7 | 81 | | } |
| | 82 | |
|
| 7 | 83 | | public string Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value); |
| | 84 | |
|
| | 85 | | public T Deserialize<T>(RestResponse response) |
| 34 | 86 | | { |
| 34 | 87 | | var result = (T)Deserialize(response, typeof(T)); |
| 14 | 88 | | return result; |
| 14 | 89 | | } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Deserialize the JSON string into a proper object. |
| | 93 | | /// </summary> |
| | 94 | | /// <param name="response">The HTTP response.</param> |
| | 95 | | /// <param name="type">Object type.</param> |
| | 96 | | /// <returns>Object representation of the JSON string.</returns> |
| | 97 | | internal object Deserialize(RestResponse response, Type type) |
| 34 | 98 | | { |
| 34 | 99 | | if (type == typeof(byte[])) // return byte array |
| 0 | 100 | | { |
| 0 | 101 | | return response.RawBytes; |
| | 102 | | } |
| | 103 | |
|
| | 104 | | // TODO: ? if (type.IsAssignableFrom(typeof(Stream))) |
| 34 | 105 | | if (type == typeof(Stream)) |
| 0 | 106 | | { |
| 0 | 107 | | var bytes = response.RawBytes; |
| 0 | 108 | | if (response.Headers != null) |
| 0 | 109 | | { |
| 0 | 110 | | var filePath = string.IsNullOrEmpty(_configuration.TempFolderPath) |
| 0 | 111 | | ? Path.GetTempPath() |
| 0 | 112 | | : _configuration.TempFolderPath; |
| 0 | 113 | | var regex = new Regex(@"Content-Disposition=.*filename=['""]?([^'""\s]+)['""]?$"); |
| 0 | 114 | | foreach (var header in response.Headers) |
| 0 | 115 | | { |
| 0 | 116 | | var match = regex.Match(header.ToString()); |
| 0 | 117 | | if (match.Success) |
| 0 | 118 | | { |
| 0 | 119 | | string fileName = filePath + ClientUtils.SanitizeFilename(match.Groups[1].Value.Replace("\"" |
| 0 | 120 | | File.WriteAllBytes(fileName, bytes); |
| 0 | 121 | | return new FileStream(fileName, FileMode.Open); |
| | 122 | | } |
| 0 | 123 | | } |
| 0 | 124 | | } |
| 0 | 125 | | var stream = new MemoryStream(bytes); |
| 0 | 126 | | return stream; |
| | 127 | | } |
| | 128 | |
|
| 34 | 129 | | if (type.Name.StartsWith("System.Nullable`1[[System.DateTime")) // return a datetime object |
| 0 | 130 | | { |
| 0 | 131 | | return DateTime.Parse(response.Content, null, System.Globalization.DateTimeStyles.RoundtripKind); |
| | 132 | | } |
| | 133 | |
|
| 34 | 134 | | if (type == typeof(string) || type.Name.StartsWith("System.Nullable")) // return primitive type |
| 0 | 135 | | { |
| 0 | 136 | | return Convert.ChangeType(response.Content, type); |
| | 137 | | } |
| | 138 | |
|
| | 139 | | // at this point, it must be a model (json) |
| | 140 | | try |
| 34 | 141 | | { |
| 34 | 142 | | return JsonConvert.DeserializeObject(response.Content, type, _serializerSettings); |
| | 143 | | } |
| 20 | 144 | | catch (Exception e) |
| 20 | 145 | | { |
| 20 | 146 | | throw new ApiException(500, e.Message); |
| | 147 | | } |
| 14 | 148 | | } |
| | 149 | |
|
| 0 | 150 | | public ISerializer Serializer => this; |
| 34 | 151 | | public IDeserializer Deserializer => this; |
| | 152 | |
|
| 34 | 153 | | public string[] AcceptedContentTypes => RestSharp.Serializers.ContentType.JsonAccept; |
| | 154 | |
|
| 34 | 155 | | public SupportsContentType SupportsContentType => contentType => |
| 68 | 156 | | contentType.EndsWith("json", StringComparison.InvariantCultureIgnoreCase) || |
| 68 | 157 | | contentType.EndsWith("javascript", StringComparison.InvariantCultureIgnoreCase); |
| | 158 | |
|
| | 159 | | public string ContentType |
| | 160 | | { |
| 0 | 161 | | get { return _contentType; } |
| 0 | 162 | | set { throw new InvalidOperationException("Not allowed to set content type."); } |
| | 163 | | } |
| | 164 | |
|
| 68 | 165 | | public DataFormat DataFormat => DataFormat.Json; |
| | 166 | | } |
| | 167 | | /// <summary> |
| | 168 | | /// Provides a default implementation of an Api client (both synchronous and asynchronous implementations), |
| | 169 | | /// encapsulating general REST accessor use cases. |
| | 170 | | /// </summary> |
| | 171 | | public partial class ApiClient : ISynchronousClient, IAsynchronousClient |
| | 172 | | { |
| | 173 | | private readonly string _baseUrl; |
| | 174 | |
|
| | 175 | | /// <summary> |
| | 176 | | /// Specifies the settings on a <see cref="JsonSerializer" /> object. |
| | 177 | | /// These settings can be adjusted to accommodate custom serialization rules. |
| | 178 | | /// </summary> |
| 89 | 179 | | public JsonSerializerSettings SerializerSettings { get; set; } = new JsonSerializerSettings |
| 14 | 180 | | { |
| 14 | 181 | | // OpenAPI generated types generally hide default constructors. |
| 14 | 182 | | ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, |
| 14 | 183 | | ContractResolver = new DefaultContractResolver |
| 14 | 184 | | { |
| 14 | 185 | | NamingStrategy = new CamelCaseNamingStrategy |
| 14 | 186 | | { |
| 14 | 187 | | OverrideSpecifiedNames = false |
| 14 | 188 | | } |
| 14 | 189 | | } |
| 14 | 190 | | }; |
| | 191 | |
|
| | 192 | | /// <summary> |
| | 193 | | /// Allows for extending request processing for <see cref="ApiClient"/> generated code. |
| | 194 | | /// </summary> |
| | 195 | | /// <param name="request">The RestSharp request object</param> |
| | 196 | | partial void InterceptRequest(RestRequest request); |
| | 197 | |
|
| | 198 | | /// <summary> |
| | 199 | | /// Allows for extending response processing for <see cref="ApiClient"/> generated code. |
| | 200 | | /// </summary> |
| | 201 | | /// <param name="request">The RestSharp request object</param> |
| | 202 | | /// <param name="response">The RestSharp response object</param> |
| | 203 | | partial void InterceptResponse(RestRequest request, RestResponse response); |
| | 204 | |
|
| | 205 | | /// <summary> |
| | 206 | | /// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base ur |
| | 207 | | /// </summary> |
| 0 | 208 | | public ApiClient() |
| 0 | 209 | | { |
| 0 | 210 | | _baseUrl = Applications.WeShare.Swagger.Client.GlobalConfiguration.Instance.BasePath; |
| 0 | 211 | | } |
| | 212 | |
|
| | 213 | | /// <summary> |
| | 214 | | /// Initializes a new instance of the <see cref="ApiClient" /> |
| | 215 | | /// </summary> |
| | 216 | | /// <param name="basePath">The target service's base path in URL format.</param> |
| | 217 | | /// <exception cref="ArgumentException"></exception> |
| 14 | 218 | | public ApiClient(string basePath) |
| 14 | 219 | | { |
| 14 | 220 | | if (string.IsNullOrEmpty(basePath)) |
| 0 | 221 | | throw new ArgumentException("basePath cannot be empty"); |
| | 222 | |
|
| 14 | 223 | | _baseUrl = basePath; |
| 14 | 224 | | } |
| | 225 | |
|
| | 226 | | /// <summary> |
| | 227 | | /// Constructs the RestSharp version of an http method |
| | 228 | | /// </summary> |
| | 229 | | /// <param name="method">Swagger Client Custom HttpMethod</param> |
| | 230 | | /// <returns>RestSharp's HttpMethod instance.</returns> |
| | 231 | | /// <exception cref="ArgumentOutOfRangeException"></exception> |
| | 232 | | private RestSharpMethod Method(HttpMethod method) |
| 34 | 233 | | { |
| | 234 | | RestSharpMethod other; |
| 34 | 235 | | switch (method) |
| | 236 | | { |
| | 237 | | case HttpMethod.Get: |
| 25 | 238 | | other = RestSharpMethod.Get; |
| 25 | 239 | | break; |
| | 240 | | case HttpMethod.Post: |
| 7 | 241 | | other = RestSharpMethod.Post; |
| 7 | 242 | | break; |
| | 243 | | case HttpMethod.Put: |
| 0 | 244 | | other = RestSharpMethod.Put; |
| 0 | 245 | | break; |
| | 246 | | case HttpMethod.Delete: |
| 2 | 247 | | other = RestSharpMethod.Delete; |
| 2 | 248 | | break; |
| | 249 | | case HttpMethod.Head: |
| 0 | 250 | | other = RestSharpMethod.Head; |
| 0 | 251 | | break; |
| | 252 | | case HttpMethod.Options: |
| 0 | 253 | | other = RestSharpMethod.Options; |
| 0 | 254 | | break; |
| | 255 | | case HttpMethod.Patch: |
| 0 | 256 | | other = RestSharpMethod.Patch; |
| 0 | 257 | | break; |
| | 258 | | default: |
| 0 | 259 | | throw new ArgumentOutOfRangeException("method", method, null); |
| | 260 | | } |
| | 261 | |
|
| 34 | 262 | | return other; |
| 34 | 263 | | } |
| | 264 | |
|
| | 265 | | /// <summary> |
| | 266 | | /// Provides all logic for constructing a new RestSharp <see cref="RestRequest"/>. |
| | 267 | | /// At this point, all information for querying the service is known. Here, it is simply |
| | 268 | | /// mapped into the RestSharp request. |
| | 269 | | /// </summary> |
| | 270 | | /// <param name="method">The http verb.</param> |
| | 271 | | /// <param name="path">The target path (or resource).</param> |
| | 272 | | /// <param name="options">The additional request options.</param> |
| | 273 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 274 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 275 | | /// <returns>[private] A new RestRequest instance.</returns> |
| | 276 | | /// <exception cref="ArgumentNullException"></exception> |
| | 277 | | private RestRequest NewRequest( |
| | 278 | | HttpMethod method, |
| | 279 | | string path, |
| | 280 | | RequestOptions options, |
| | 281 | | IReadableConfiguration configuration) |
| 34 | 282 | | { |
| 34 | 283 | | if (path == null) throw new ArgumentNullException("path"); |
| 34 | 284 | | if (options == null) throw new ArgumentNullException("options"); |
| 34 | 285 | | if (configuration == null) throw new ArgumentNullException("configuration"); |
| | 286 | |
|
| 34 | 287 | | RestRequest request = new RestRequest(path, Method(method)); |
| | 288 | |
|
| 34 | 289 | | if (options.PathParameters != null) |
| 34 | 290 | | { |
| 150 | 291 | | foreach (var pathParam in options.PathParameters) |
| 24 | 292 | | { |
| 24 | 293 | | request.AddParameter(pathParam.Key, pathParam.Value, ParameterType.UrlSegment); |
| 24 | 294 | | } |
| 34 | 295 | | } |
| | 296 | |
|
| 34 | 297 | | if (options.QueryParameters != null) |
| 34 | 298 | | { |
| 102 | 299 | | foreach (var queryParam in options.QueryParameters) |
| 0 | 300 | | { |
| 0 | 301 | | foreach (var value in queryParam.Value) |
| 0 | 302 | | { |
| 0 | 303 | | request.AddQueryParameter(queryParam.Key, value); |
| 0 | 304 | | } |
| 0 | 305 | | } |
| 34 | 306 | | } |
| | 307 | |
|
| 34 | 308 | | if (configuration.DefaultHeaders != null) |
| 34 | 309 | | { |
| 102 | 310 | | foreach (var headerParam in configuration.DefaultHeaders) |
| 0 | 311 | | { |
| 0 | 312 | | request.AddHeader(headerParam.Key, headerParam.Value); |
| 0 | 313 | | } |
| 34 | 314 | | } |
| | 315 | |
|
| 34 | 316 | | if (options.HeaderParameters != null) |
| 34 | 317 | | { |
| 180 | 318 | | foreach (var headerParam in options.HeaderParameters) |
| 39 | 319 | | { |
| 195 | 320 | | foreach (var value in headerParam.Value) |
| 39 | 321 | | { |
| 39 | 322 | | request.AddHeader(headerParam.Key, value); |
| 39 | 323 | | } |
| 39 | 324 | | } |
| 34 | 325 | | } |
| | 326 | |
|
| 34 | 327 | | if (options.FormParameters != null) |
| 34 | 328 | | { |
| 102 | 329 | | foreach (var formParam in options.FormParameters) |
| 0 | 330 | | { |
| 0 | 331 | | request.AddParameter(formParam.Key, formParam.Value); |
| 0 | 332 | | } |
| 34 | 333 | | } |
| | 334 | |
|
| 34 | 335 | | if (options.Data != null) |
| 7 | 336 | | { |
| 7 | 337 | | if (options.Data is Stream stream) |
| 0 | 338 | | { |
| 0 | 339 | | var contentType = "application/octet-stream"; |
| 0 | 340 | | if (options.HeaderParameters != null) |
| 0 | 341 | | { |
| 0 | 342 | | var contentTypes = options.HeaderParameters["Content-Type"]; |
| 0 | 343 | | contentType = contentTypes[0]; |
| 0 | 344 | | } |
| | 345 | |
|
| 0 | 346 | | var bytes = ClientUtils.ReadAsBytes(stream); |
| 0 | 347 | | request.AddParameter(contentType, bytes, ParameterType.RequestBody); |
| 0 | 348 | | } |
| | 349 | | else |
| 7 | 350 | | { |
| 7 | 351 | | if (options.HeaderParameters != null) |
| 7 | 352 | | { |
| 7 | 353 | | var contentTypes = options.HeaderParameters["Content-Type"]; |
| 14 | 354 | | if (contentTypes == null || contentTypes.Any(header => header.Contains("application/json"))) |
| 7 | 355 | | { |
| 7 | 356 | | request.RequestFormat = DataFormat.Json; |
| 7 | 357 | | } |
| | 358 | | else |
| 0 | 359 | | { |
| | 360 | | // TODO: Generated client user should add additional handlers. RestSharp only supports XML a |
| 0 | 361 | | } |
| 7 | 362 | | } |
| | 363 | | else |
| 0 | 364 | | { |
| | 365 | | // Here, we'll assume JSON APIs are more common. XML can be forced by adding produces/consumes t |
| 0 | 366 | | request.RequestFormat = DataFormat.Json; |
| 0 | 367 | | } |
| | 368 | |
|
| 7 | 369 | | request.AddJsonBody(options.Data); |
| 7 | 370 | | } |
| 7 | 371 | | } |
| | 372 | |
|
| 34 | 373 | | if (options.FileParameters != null) |
| 34 | 374 | | { |
| 102 | 375 | | foreach (var fileParam in options.FileParameters) |
| 0 | 376 | | { |
| 0 | 377 | | foreach (var file in fileParam.Value) |
| 0 | 378 | | { |
| 0 | 379 | | var bytes = ClientUtils.ReadAsBytes(file); |
| 0 | 380 | | var fileStream = file as FileStream; |
| 0 | 381 | | if (fileStream != null) |
| 0 | 382 | | request.AddFile(fileParam.Key, bytes, System.IO.Path.GetFileName(fileStream.Name)); |
| | 383 | | else |
| 0 | 384 | | request.AddFile(fileParam.Key, bytes, "no_file_name_provided"); |
| 0 | 385 | | } |
| 0 | 386 | | } |
| 34 | 387 | | } |
| | 388 | |
|
| 34 | 389 | | return request; |
| 34 | 390 | | } |
| | 391 | |
|
| | 392 | | private ApiResponse<T> ToApiResponse<T>(RestResponse<T> response) |
| 34 | 393 | | { |
| 34 | 394 | | T result = response.Data; |
| 34 | 395 | | string rawContent = response.Content; |
| | 396 | |
|
| 34 | 397 | | var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>(), result, rawContent |
| 34 | 398 | | { |
| 34 | 399 | | ErrorText = response.ErrorMessage, |
| 34 | 400 | | Cookies = new List<Cookie>() |
| 34 | 401 | | }; |
| | 402 | |
|
| 34 | 403 | | if (response.Headers != null) |
| 34 | 404 | | { |
| 170 | 405 | | foreach (var responseHeader in response.Headers) |
| 34 | 406 | | { |
| 34 | 407 | | transformed.Headers.Add(responseHeader.Name, ClientUtils.ParameterToString(responseHeader.Value)); |
| 34 | 408 | | } |
| 34 | 409 | | } |
| | 410 | |
|
| 34 | 411 | | if (response.ContentHeaders != null) |
| 34 | 412 | | { |
| 238 | 413 | | foreach (var responseHeader in response.ContentHeaders) |
| 68 | 414 | | { |
| 68 | 415 | | transformed.Headers.Add(responseHeader.Name, ClientUtils.ParameterToString(responseHeader.Value)); |
| 68 | 416 | | } |
| 34 | 417 | | } |
| | 418 | |
|
| 34 | 419 | | if (response.Cookies != null) |
| 34 | 420 | | { |
| 102 | 421 | | foreach (var responseCookies in response.Cookies.Cast<Cookie>()) |
| 0 | 422 | | { |
| 0 | 423 | | transformed.Cookies.Add( |
| 0 | 424 | | new Cookie( |
| 0 | 425 | | responseCookies.Name, |
| 0 | 426 | | responseCookies.Value, |
| 0 | 427 | | responseCookies.Path, |
| 0 | 428 | | responseCookies.Domain) |
| 0 | 429 | | ); |
| 0 | 430 | | } |
| 34 | 431 | | } |
| | 432 | |
|
| 34 | 433 | | return transformed; |
| 34 | 434 | | } |
| | 435 | |
|
| | 436 | | private ApiResponse<T> Exec<T>(RestRequest req, RequestOptions options, IReadableConfiguration configuration) |
| 34 | 437 | | { |
| 34 | 438 | | var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; |
| | 439 | |
|
| 34 | 440 | | var cookies = new CookieContainer(); |
| | 441 | |
|
| 34 | 442 | | if (options.Cookies != null && options.Cookies.Count > 0) |
| 0 | 443 | | { |
| 0 | 444 | | foreach (var cookie in options.Cookies) |
| 0 | 445 | | { |
| 0 | 446 | | cookies.Add(new Cookie(cookie.Name, cookie.Value)); |
| 0 | 447 | | } |
| 0 | 448 | | } |
| | 449 | |
|
| 34 | 450 | | var clientOptions = new RestClientOptions(baseUrl) |
| 34 | 451 | | { |
| 34 | 452 | | ClientCertificates = configuration.ClientCertificates, |
| 34 | 453 | | CookieContainer = cookies, |
| 34 | 454 | | MaxTimeout = configuration.Timeout, |
| 34 | 455 | | Proxy = configuration.Proxy, |
| 34 | 456 | | UserAgent = configuration.UserAgent |
| 34 | 457 | | }; |
| | 458 | |
|
| 34 | 459 | | RestClient client = new RestClient(clientOptions) |
| 109 | 460 | | .UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); |
| | 461 | |
|
| | 462 | | InterceptRequest(req); |
| | 463 | |
|
| | 464 | | RestResponse<T> response; |
| 34 | 465 | | if (RetryConfiguration.RetryPolicy != null) |
| 0 | 466 | | { |
| 0 | 467 | | var policy = RetryConfiguration.RetryPolicy; |
| 0 | 468 | | var policyResult = policy.ExecuteAndCapture(() => client.Execute(req)); |
| 0 | 469 | | response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) |
| 0 | 470 | | { |
| 0 | 471 | | Request = req, |
| 0 | 472 | | ErrorException = policyResult.FinalException |
| 0 | 473 | | }; |
| 0 | 474 | | } |
| | 475 | | else |
| 34 | 476 | | { |
| 34 | 477 | | response = client.Execute<T>(req); |
| 34 | 478 | | } |
| | 479 | |
|
| | 480 | | // if the response type is oneOf/anyOf, call FromJSON to deserialize the data |
| 34 | 481 | | if (typeof(Applications.WeShare.Swagger.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) |
| 0 | 482 | | { |
| | 483 | | try |
| 0 | 484 | | { |
| 0 | 485 | | response.Data = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content }); |
| 0 | 486 | | } |
| 0 | 487 | | catch (Exception ex) |
| 0 | 488 | | { |
| 0 | 489 | | throw ex.InnerException != null ? ex.InnerException : ex; |
| | 490 | | } |
| 0 | 491 | | } |
| 34 | 492 | | else if (typeof(T).Name == "Stream") // for binary response |
| 0 | 493 | | { |
| 0 | 494 | | response.Data = (T)(object)new MemoryStream(response.RawBytes); |
| 0 | 495 | | } |
| 34 | 496 | | else if (typeof(T).Name == "Byte[]") // for byte response |
| 0 | 497 | | { |
| 0 | 498 | | response.Data = (T)(object)response.RawBytes; |
| 0 | 499 | | } |
| 34 | 500 | | else if (typeof(T).Name == "String") // for string response |
| 0 | 501 | | { |
| 0 | 502 | | response.Data = (T)(object)response.Content; |
| 0 | 503 | | } |
| | 504 | |
|
| | 505 | | InterceptResponse(req, response); |
| | 506 | |
|
| 34 | 507 | | var result = ToApiResponse(response); |
| 34 | 508 | | if (response.ErrorMessage != null) |
| 20 | 509 | | { |
| 20 | 510 | | result.ErrorText = response.ErrorMessage; |
| 20 | 511 | | } |
| | 512 | |
|
| 34 | 513 | | if (response.Cookies != null && response.Cookies.Count > 0) |
| 0 | 514 | | { |
| 0 | 515 | | if (result.Cookies == null) result.Cookies = new List<Cookie>(); |
| 0 | 516 | | foreach (var restResponseCookie in response.Cookies.Cast<Cookie>()) |
| 0 | 517 | | { |
| 0 | 518 | | var cookie = new Cookie( |
| 0 | 519 | | restResponseCookie.Name, |
| 0 | 520 | | restResponseCookie.Value, |
| 0 | 521 | | restResponseCookie.Path, |
| 0 | 522 | | restResponseCookie.Domain |
| 0 | 523 | | ) |
| 0 | 524 | | { |
| 0 | 525 | | Comment = restResponseCookie.Comment, |
| 0 | 526 | | CommentUri = restResponseCookie.CommentUri, |
| 0 | 527 | | Discard = restResponseCookie.Discard, |
| 0 | 528 | | Expired = restResponseCookie.Expired, |
| 0 | 529 | | Expires = restResponseCookie.Expires, |
| 0 | 530 | | HttpOnly = restResponseCookie.HttpOnly, |
| 0 | 531 | | Port = restResponseCookie.Port, |
| 0 | 532 | | Secure = restResponseCookie.Secure, |
| 0 | 533 | | Version = restResponseCookie.Version |
| 0 | 534 | | }; |
| | 535 | |
|
| 0 | 536 | | result.Cookies.Add(cookie); |
| 0 | 537 | | } |
| 0 | 538 | | } |
| 34 | 539 | | return result; |
| 34 | 540 | | } |
| | 541 | |
|
| | 542 | | private async Task<ApiResponse<T>> ExecAsync<T>(RestRequest req, RequestOptions options, IReadableConfiguration |
| 0 | 543 | | { |
| 0 | 544 | | var baseUrl = configuration.GetOperationServerUrl(options.Operation, options.OperationIndex) ?? _baseUrl; |
| | 545 | |
|
| 0 | 546 | | var clientOptions = new RestClientOptions(baseUrl) |
| 0 | 547 | | { |
| 0 | 548 | | ClientCertificates = configuration.ClientCertificates, |
| 0 | 549 | | MaxTimeout = configuration.Timeout, |
| 0 | 550 | | Proxy = configuration.Proxy, |
| 0 | 551 | | UserAgent = configuration.UserAgent |
| 0 | 552 | | }; |
| | 553 | |
|
| 0 | 554 | | RestClient client = new RestClient(clientOptions) |
| 0 | 555 | | .UseSerializer(() => new CustomJsonCodec(SerializerSettings, configuration)); |
| | 556 | |
|
| | 557 | | InterceptRequest(req); |
| | 558 | |
|
| | 559 | | RestResponse<T> response; |
| 0 | 560 | | if (RetryConfiguration.AsyncRetryPolicy != null) |
| 0 | 561 | | { |
| 0 | 562 | | var policy = RetryConfiguration.AsyncRetryPolicy; |
| 0 | 563 | | var policyResult = await policy.ExecuteAndCaptureAsync((ct) => client.ExecuteAsync(req, ct), cancellatio |
| 0 | 564 | | response = (policyResult.Outcome == OutcomeType.Successful) ? client.Deserialize<T>(policyResult.Result) |
| 0 | 565 | | { |
| 0 | 566 | | Request = req, |
| 0 | 567 | | ErrorException = policyResult.FinalException |
| 0 | 568 | | }; |
| 0 | 569 | | } |
| | 570 | | else |
| 0 | 571 | | { |
| 0 | 572 | | response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false); |
| 0 | 573 | | } |
| | 574 | |
|
| | 575 | | // if the response type is oneOf/anyOf, call FromJSON to deserialize the data |
| 0 | 576 | | if (typeof(Applications.WeShare.Swagger.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T))) |
| 0 | 577 | | { |
| 0 | 578 | | response.Data = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content }); |
| 0 | 579 | | } |
| 0 | 580 | | else if (typeof(T).Name == "Stream") // for binary response |
| 0 | 581 | | { |
| 0 | 582 | | response.Data = (T)(object)new MemoryStream(response.RawBytes); |
| 0 | 583 | | } |
| 0 | 584 | | else if (typeof(T).Name == "Byte[]") // for byte response |
| 0 | 585 | | { |
| 0 | 586 | | response.Data = (T)(object)response.RawBytes; |
| 0 | 587 | | } |
| | 588 | |
|
| | 589 | | InterceptResponse(req, response); |
| | 590 | |
|
| 0 | 591 | | var result = ToApiResponse(response); |
| 0 | 592 | | if (response.ErrorMessage != null) |
| 0 | 593 | | { |
| 0 | 594 | | result.ErrorText = response.ErrorMessage; |
| 0 | 595 | | } |
| | 596 | |
|
| 0 | 597 | | if (response.Cookies != null && response.Cookies.Count > 0) |
| 0 | 598 | | { |
| 0 | 599 | | if (result.Cookies == null) result.Cookies = new List<Cookie>(); |
| 0 | 600 | | foreach (var restResponseCookie in response.Cookies.Cast<Cookie>()) |
| 0 | 601 | | { |
| 0 | 602 | | var cookie = new Cookie( |
| 0 | 603 | | restResponseCookie.Name, |
| 0 | 604 | | restResponseCookie.Value, |
| 0 | 605 | | restResponseCookie.Path, |
| 0 | 606 | | restResponseCookie.Domain |
| 0 | 607 | | ) |
| 0 | 608 | | { |
| 0 | 609 | | Comment = restResponseCookie.Comment, |
| 0 | 610 | | CommentUri = restResponseCookie.CommentUri, |
| 0 | 611 | | Discard = restResponseCookie.Discard, |
| 0 | 612 | | Expired = restResponseCookie.Expired, |
| 0 | 613 | | Expires = restResponseCookie.Expires, |
| 0 | 614 | | HttpOnly = restResponseCookie.HttpOnly, |
| 0 | 615 | | Port = restResponseCookie.Port, |
| 0 | 616 | | Secure = restResponseCookie.Secure, |
| 0 | 617 | | Version = restResponseCookie.Version |
| 0 | 618 | | }; |
| | 619 | |
|
| 0 | 620 | | result.Cookies.Add(cookie); |
| 0 | 621 | | } |
| 0 | 622 | | } |
| 0 | 623 | | return result; |
| 0 | 624 | | } |
| | 625 | |
|
| | 626 | | #region IAsynchronousClient |
| | 627 | | /// <summary> |
| | 628 | | /// Make a HTTP GET request (async). |
| | 629 | | /// </summary> |
| | 630 | | /// <param name="path">The target path (or resource).</param> |
| | 631 | | /// <param name="options">The additional request options.</param> |
| | 632 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 633 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 634 | | /// <param name="cancellationToken">Token that enables callers to cancel the request.</param> |
| | 635 | | /// <returns>A Task containing ApiResponse</returns> |
| | 636 | | public Task<ApiResponse<T>> GetAsync<T>(string path, RequestOptions options, IReadableConfiguration configuratio |
| 0 | 637 | | { |
| 0 | 638 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 639 | | return ExecAsync<T>(NewRequest(HttpMethod.Get, path, options, config), options, config, cancellationToken); |
| 0 | 640 | | } |
| | 641 | |
|
| | 642 | | /// <summary> |
| | 643 | | /// Make a HTTP POST request (async). |
| | 644 | | /// </summary> |
| | 645 | | /// <param name="path">The target path (or resource).</param> |
| | 646 | | /// <param name="options">The additional request options.</param> |
| | 647 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 648 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 649 | | /// <param name="cancellationToken">Token that enables callers to cancel the request.</param> |
| | 650 | | /// <returns>A Task containing ApiResponse</returns> |
| | 651 | | public Task<ApiResponse<T>> PostAsync<T>(string path, RequestOptions options, IReadableConfiguration configurati |
| 0 | 652 | | { |
| 0 | 653 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 654 | | return ExecAsync<T>(NewRequest(HttpMethod.Post, path, options, config), options, config, cancellationToken); |
| 0 | 655 | | } |
| | 656 | |
|
| | 657 | | /// <summary> |
| | 658 | | /// Make a HTTP PUT request (async). |
| | 659 | | /// </summary> |
| | 660 | | /// <param name="path">The target path (or resource).</param> |
| | 661 | | /// <param name="options">The additional request options.</param> |
| | 662 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 663 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 664 | | /// <param name="cancellationToken">Token that enables callers to cancel the request.</param> |
| | 665 | | /// <returns>A Task containing ApiResponse</returns> |
| | 666 | | public Task<ApiResponse<T>> PutAsync<T>(string path, RequestOptions options, IReadableConfiguration configuratio |
| 0 | 667 | | { |
| 0 | 668 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 669 | | return ExecAsync<T>(NewRequest(HttpMethod.Put, path, options, config), options, config, cancellationToken); |
| 0 | 670 | | } |
| | 671 | |
|
| | 672 | | /// <summary> |
| | 673 | | /// Make a HTTP DELETE request (async). |
| | 674 | | /// </summary> |
| | 675 | | /// <param name="path">The target path (or resource).</param> |
| | 676 | | /// <param name="options">The additional request options.</param> |
| | 677 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 678 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 679 | | /// <param name="cancellationToken">Token that enables callers to cancel the request.</param> |
| | 680 | | /// <returns>A Task containing ApiResponse</returns> |
| | 681 | | public Task<ApiResponse<T>> DeleteAsync<T>(string path, RequestOptions options, IReadableConfiguration configura |
| 0 | 682 | | { |
| 0 | 683 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 684 | | return ExecAsync<T>(NewRequest(HttpMethod.Delete, path, options, config), options, config, cancellationToken |
| 0 | 685 | | } |
| | 686 | |
|
| | 687 | | /// <summary> |
| | 688 | | /// Make a HTTP HEAD request (async). |
| | 689 | | /// </summary> |
| | 690 | | /// <param name="path">The target path (or resource).</param> |
| | 691 | | /// <param name="options">The additional request options.</param> |
| | 692 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 693 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 694 | | /// <param name="cancellationToken">Token that enables callers to cancel the request.</param> |
| | 695 | | /// <returns>A Task containing ApiResponse</returns> |
| | 696 | | public Task<ApiResponse<T>> HeadAsync<T>(string path, RequestOptions options, IReadableConfiguration configurati |
| 0 | 697 | | { |
| 0 | 698 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 699 | | return ExecAsync<T>(NewRequest(HttpMethod.Head, path, options, config), options, config, cancellationToken); |
| 0 | 700 | | } |
| | 701 | |
|
| | 702 | | /// <summary> |
| | 703 | | /// Make a HTTP OPTION request (async). |
| | 704 | | /// </summary> |
| | 705 | | /// <param name="path">The target path (or resource).</param> |
| | 706 | | /// <param name="options">The additional request options.</param> |
| | 707 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 708 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 709 | | /// <param name="cancellationToken">Token that enables callers to cancel the request.</param> |
| | 710 | | /// <returns>A Task containing ApiResponse</returns> |
| | 711 | | public Task<ApiResponse<T>> OptionsAsync<T>(string path, RequestOptions options, IReadableConfiguration configur |
| 0 | 712 | | { |
| 0 | 713 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 714 | | return ExecAsync<T>(NewRequest(HttpMethod.Options, path, options, config), options, config, cancellationToke |
| 0 | 715 | | } |
| | 716 | |
|
| | 717 | | /// <summary> |
| | 718 | | /// Make a HTTP PATCH request (async). |
| | 719 | | /// </summary> |
| | 720 | | /// <param name="path">The target path (or resource).</param> |
| | 721 | | /// <param name="options">The additional request options.</param> |
| | 722 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 723 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 724 | | /// <param name="cancellationToken">Token that enables callers to cancel the request.</param> |
| | 725 | | /// <returns>A Task containing ApiResponse</returns> |
| | 726 | | public Task<ApiResponse<T>> PatchAsync<T>(string path, RequestOptions options, IReadableConfiguration configurat |
| 0 | 727 | | { |
| 0 | 728 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 729 | | return ExecAsync<T>(NewRequest(HttpMethod.Patch, path, options, config), options, config, cancellationToken) |
| 0 | 730 | | } |
| | 731 | | #endregion IAsynchronousClient |
| | 732 | |
|
| | 733 | | #region ISynchronousClient |
| | 734 | | /// <summary> |
| | 735 | | /// Make a HTTP GET request (synchronous). |
| | 736 | | /// </summary> |
| | 737 | | /// <param name="path">The target path (or resource).</param> |
| | 738 | | /// <param name="options">The additional request options.</param> |
| | 739 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 740 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 741 | | /// <returns>A Task containing ApiResponse</returns> |
| | 742 | | public ApiResponse<T> Get<T>(string path, RequestOptions options, IReadableConfiguration configuration = null) |
| 25 | 743 | | { |
| 25 | 744 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 25 | 745 | | return Exec<T>(NewRequest(HttpMethod.Get, path, options, config), options, config); |
| 25 | 746 | | } |
| | 747 | |
|
| | 748 | | /// <summary> |
| | 749 | | /// Make a HTTP POST request (synchronous). |
| | 750 | | /// </summary> |
| | 751 | | /// <param name="path">The target path (or resource).</param> |
| | 752 | | /// <param name="options">The additional request options.</param> |
| | 753 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 754 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 755 | | /// <returns>A Task containing ApiResponse</returns> |
| | 756 | | public ApiResponse<T> Post<T>(string path, RequestOptions options, IReadableConfiguration configuration = null) |
| 7 | 757 | | { |
| 7 | 758 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 7 | 759 | | return Exec<T>(NewRequest(HttpMethod.Post, path, options, config), options, config); |
| 7 | 760 | | } |
| | 761 | |
|
| | 762 | | /// <summary> |
| | 763 | | /// Make a HTTP PUT request (synchronous). |
| | 764 | | /// </summary> |
| | 765 | | /// <param name="path">The target path (or resource).</param> |
| | 766 | | /// <param name="options">The additional request options.</param> |
| | 767 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 768 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 769 | | /// <returns>A Task containing ApiResponse</returns> |
| | 770 | | public ApiResponse<T> Put<T>(string path, RequestOptions options, IReadableConfiguration configuration = null) |
| 0 | 771 | | { |
| 0 | 772 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 773 | | return Exec<T>(NewRequest(HttpMethod.Put, path, options, config), options, config); |
| 0 | 774 | | } |
| | 775 | |
|
| | 776 | | /// <summary> |
| | 777 | | /// Make a HTTP DELETE request (synchronous). |
| | 778 | | /// </summary> |
| | 779 | | /// <param name="path">The target path (or resource).</param> |
| | 780 | | /// <param name="options">The additional request options.</param> |
| | 781 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 782 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 783 | | /// <returns>A Task containing ApiResponse</returns> |
| | 784 | | public ApiResponse<T> Delete<T>(string path, RequestOptions options, IReadableConfiguration configuration = null |
| 2 | 785 | | { |
| 2 | 786 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 2 | 787 | | return Exec<T>(NewRequest(HttpMethod.Delete, path, options, config), options, config); |
| 2 | 788 | | } |
| | 789 | |
|
| | 790 | | /// <summary> |
| | 791 | | /// Make a HTTP HEAD request (synchronous). |
| | 792 | | /// </summary> |
| | 793 | | /// <param name="path">The target path (or resource).</param> |
| | 794 | | /// <param name="options">The additional request options.</param> |
| | 795 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 796 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 797 | | /// <returns>A Task containing ApiResponse</returns> |
| | 798 | | public ApiResponse<T> Head<T>(string path, RequestOptions options, IReadableConfiguration configuration = null) |
| 0 | 799 | | { |
| 0 | 800 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 801 | | return Exec<T>(NewRequest(HttpMethod.Head, path, options, config), options, config); |
| 0 | 802 | | } |
| | 803 | |
|
| | 804 | | /// <summary> |
| | 805 | | /// Make a HTTP OPTION request (synchronous). |
| | 806 | | /// </summary> |
| | 807 | | /// <param name="path">The target path (or resource).</param> |
| | 808 | | /// <param name="options">The additional request options.</param> |
| | 809 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 810 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 811 | | /// <returns>A Task containing ApiResponse</returns> |
| | 812 | | public ApiResponse<T> Options<T>(string path, RequestOptions options, IReadableConfiguration configuration = nul |
| 0 | 813 | | { |
| 0 | 814 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 815 | | return Exec<T>(NewRequest(HttpMethod.Options, path, options, config), options, config); |
| 0 | 816 | | } |
| | 817 | |
|
| | 818 | | /// <summary> |
| | 819 | | /// Make a HTTP PATCH request (synchronous). |
| | 820 | | /// </summary> |
| | 821 | | /// <param name="path">The target path (or resource).</param> |
| | 822 | | /// <param name="options">The additional request options.</param> |
| | 823 | | /// <param name="configuration">A per-request configuration object. It is assumed that any merge with |
| | 824 | | /// GlobalConfiguration has been done before calling this method.</param> |
| | 825 | | /// <returns>A Task containing ApiResponse</returns> |
| | 826 | | public ApiResponse<T> Patch<T>(string path, RequestOptions options, IReadableConfiguration configuration = null) |
| 0 | 827 | | { |
| 0 | 828 | | var config = configuration ?? GlobalConfiguration.Instance; |
| 0 | 829 | | return Exec<T>(NewRequest(HttpMethod.Patch, path, options, config), options, config); |
| 0 | 830 | | } |
| | 831 | | #endregion ISynchronousClient |
| | 832 | | } |
| | 833 | | } |