Exemple #1
0
        private static async Task <TResult> SendRequest <TResult>(HttpClient httpClient, string method, string uri, JsonSerializerSettings serializerSettings, object data = default, CancellationToken token = default(CancellationToken), params KeyValuePair <string, string>[] headers)
        {
            HttpRequestMessage msg = new HttpRequestMessage
            {
                Method     = new HttpMethod(method),
                RequestUri = new Uri(uri, UriKind.Absolute)
            };

            headers.ToList().ForEach(h => msg.Headers.Add(h.Key, h.Value));

            if (data != null)
            {
                var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8);
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                msg.Content = content;
            }

            var response = await httpClient.SendAsync(msg, token);

            await RequestProvider.HandleResponse(response);

            string serialized = await response.Content.ReadAsStringAsync();

            TResult result = default;

            if (!string.IsNullOrEmpty(serialized))
            {
                result = JsonConvert.DeserializeObject <TResult>(serialized, serializerSettings);
            }

            return(result);
        }
Exemple #2
0
 public async Task <TResult> GetAsync <TResult>(string uri, string bearerToken = "", CancellationToken token = default(CancellationToken))
 {
     return(await RequestProvider.SendRequest <TResult>(
                this.GetHttpClientWithBearerToken(bearerToken),
                WebRequestMethods.Http.Get,
                uri,
                this.serializerSettings,
                token : token
                ));
 }
Exemple #3
0
 public async Task <TResult> DeleteAsync <TResult>(string uri, object data, string bearerToken = "", CancellationToken token = default(CancellationToken), params KeyValuePair <string, string>[] headers)
 => await RequestProvider.SendRequest <TResult>(
     this.GetHttpClientWithBearerToken(bearerToken),
     "DELETE",
     uri,
     this.serializerSettings,
     data,
     token,
     headers
     );