Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1.        private async Task Request(RequestType requestType, string url, byte[] content = null, string mediaType = "application/json")
  2.         {
  3.             await WaitForRateLimit(url);
  4.  
  5.             if (requestType == RequestType.GET || requestType == RequestType.DELETE)
  6.             {
  7.                 HttpResponseMessage response;
  8.                 if (requestType == RequestType.GET) response = await httpClient.GetAsync(url);
  9.                 else response = await httpClient.DeleteAsync(url);
  10.                 using (response)
  11.                 {
  12.                     GetRateLimits(response.Headers, url);
  13.                     GetErrors(response);
  14.                     return;
  15.                 }
  16.             }
  17.  
  18.             if (requestType == RequestType.POST || requestType == RequestType.PATCH)
  19.             {
  20.                 using (ByteArrayContent byteContent = new ByteArrayContent(content))
  21.                 {
  22.                     byteContent.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
  23.                     byteContent.Headers.ContentLength = content.Length;
  24.  
  25.                     HttpResponseMessage response;
  26.                     if (requestType == RequestType.POST) response = await httpClient.PostAsync(url, byteContent);
  27.                     else response = await httpClient.PatchAsync(url, byteContent);
  28.  
  29.                     using (response)
  30.                     {
  31.                         GetRateLimits(response.Headers, url);
  32.                         GetErrors(response);
  33.                         return;
  34.                     }
  35.                 }
  36.             }
  37.  
  38.             Console.WriteLine("Method not yet implemented in Request.");
  39.             return;
  40.         }
  41.  
  42.         private async Task<T> Request<T>(RequestType requestType, string url, byte[] content = null, string mediaType = "application/json")
  43.         {
  44.             await WaitForRateLimit(url);
  45.            
  46.             if (requestType == RequestType.GET || requestType == RequestType.DELETE)
  47.             {
  48.                 HttpResponseMessage response;
  49.                 if (requestType == RequestType.GET) response = await httpClient.GetAsync(url);
  50.                 else response = await httpClient.DeleteAsync(url);
  51.                 using (response)
  52.                 {
  53.                     GetRateLimits(response.Headers, url);
  54.  
  55.                     if (response.IsSuccessStatusCode)
  56.                     {
  57.                         byte[] data = await response.Content.ReadAsByteArrayAsync();
  58.                         return JsonSerializer.Deserialize<T>(data);
  59.                     }
  60.  
  61.                     GetErrors(response);
  62.                     return default;
  63.                 }
  64.             }
  65.  
  66.             if (requestType == RequestType.POST || requestType == RequestType.PATCH)
  67.             {
  68.                 using (ByteArrayContent byteContent = new ByteArrayContent(content))
  69.                 {
  70.                     byteContent.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
  71.                     byteContent.Headers.ContentLength = content.Length;
  72.  
  73.                     HttpResponseMessage response;
  74.                     if (requestType == RequestType.POST) response = await httpClient.PostAsync(url, byteContent);
  75.                     else response = await httpClient.PatchAsync(url, byteContent);
  76.  
  77.                     using (response)
  78.                     {
  79.                         GetRateLimits(response.Headers, url);
  80.                         if (response.IsSuccessStatusCode)
  81.                         {
  82.                             byte[] data = await response.Content.ReadAsByteArrayAsync();
  83.                             return JsonSerializer.Deserialize<T>(data);
  84.                         }
  85.  
  86.                         GetErrors(response);
  87.                         return default;
  88.                     }
  89.                 }
  90.             }
  91.  
  92.             Console.WriteLine("Method not yet implemented in Request.");
  93.             return default;
  94.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement