Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private async Task Request(RequestType requestType, string url, byte[] content = null, string mediaType = "application/json")
- {
- await WaitForRateLimit(url);
- if (requestType == RequestType.GET || requestType == RequestType.DELETE)
- {
- HttpResponseMessage response;
- if (requestType == RequestType.GET) response = await httpClient.GetAsync(url);
- else response = await httpClient.DeleteAsync(url);
- using (response)
- {
- GetRateLimits(response.Headers, url);
- GetErrors(response);
- return;
- }
- }
- if (requestType == RequestType.POST || requestType == RequestType.PATCH)
- {
- using (ByteArrayContent byteContent = new ByteArrayContent(content))
- {
- byteContent.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
- byteContent.Headers.ContentLength = content.Length;
- HttpResponseMessage response;
- if (requestType == RequestType.POST) response = await httpClient.PostAsync(url, byteContent);
- else response = await httpClient.PatchAsync(url, byteContent);
- using (response)
- {
- GetRateLimits(response.Headers, url);
- GetErrors(response);
- return;
- }
- }
- }
- Console.WriteLine("Method not yet implemented in Request.");
- return;
- }
- private async Task<T> Request<T>(RequestType requestType, string url, byte[] content = null, string mediaType = "application/json")
- {
- await WaitForRateLimit(url);
- if (requestType == RequestType.GET || requestType == RequestType.DELETE)
- {
- HttpResponseMessage response;
- if (requestType == RequestType.GET) response = await httpClient.GetAsync(url);
- else response = await httpClient.DeleteAsync(url);
- using (response)
- {
- GetRateLimits(response.Headers, url);
- if (response.IsSuccessStatusCode)
- {
- byte[] data = await response.Content.ReadAsByteArrayAsync();
- return JsonSerializer.Deserialize<T>(data);
- }
- GetErrors(response);
- return default;
- }
- }
- if (requestType == RequestType.POST || requestType == RequestType.PATCH)
- {
- using (ByteArrayContent byteContent = new ByteArrayContent(content))
- {
- byteContent.Headers.ContentType = new MediaTypeHeaderValue(mediaType);
- byteContent.Headers.ContentLength = content.Length;
- HttpResponseMessage response;
- if (requestType == RequestType.POST) response = await httpClient.PostAsync(url, byteContent);
- else response = await httpClient.PatchAsync(url, byteContent);
- using (response)
- {
- GetRateLimits(response.Headers, url);
- if (response.IsSuccessStatusCode)
- {
- byte[] data = await response.Content.ReadAsByteArrayAsync();
- return JsonSerializer.Deserialize<T>(data);
- }
- GetErrors(response);
- return default;
- }
- }
- }
- Console.WriteLine("Method not yet implemented in Request.");
- return default;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement