Exemple #1
0
        public async Task <HTTPResult <T> > HttpCallAsync <T>(NetworkCredential cred, string action, HttpMethod method, string content = null, CancellationTokenSource tokenSource = default)
        {
            HttpClientHandler authtHandler = new HttpClientHandler {
                Credentials = cred ?? CredentialCache.DefaultCredentials
            };

            try
            {
                using (HttpClient client = new HttpClient(authtHandler))
                {
                    client.Timeout = TimeSpan.FromMinutes(10);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    var requestMessage = new HttpRequestMessage
                    {
                        RequestUri = new Uri(_baseUri, action),
                        Method     = method
                    };
                    if ((method == HttpMethod.Post || method == HttpMethod.Put) && !string.IsNullOrWhiteSpace(content))
                    {
                        requestMessage.Content = new StringContent(content, System.Text.Encoding.UTF8, "application/json");
                    }

                    CancellationToken token = tokenSource == null ? default : tokenSource.Token;
                                              using (HttpResponseMessage message = await client.SendAsync(requestMessage, token))
                                              {
                                                  var status = new HTTPCode {
                                                      statusCode = (int)message.StatusCode, reason = message.ReasonPhrase
                                                  };
                                                  if (message.IsSuccessStatusCode)
                                                  {
                                                      using (var mc = message.Content)
                                                      {
                                                          return(new HTTPResult <T>(status, await mc.ReadAsAsync <T>(token)));

                                                          //  return Tuple.Create(await mc.ReadAsAsync<T>(token), status);
                                                      }
                                                  }
                                                  return(new HTTPResult <T>(status, default(T))); // Tuple.Create(default(T), status);
                                              }
                }
            }
            catch (Exception ex)
            {
                return(new HTTPResult <T>(new HTTPCode {
                    statusCode = 400, reason = "Exception"
                }, default(T)));                                                                                //Tuple.Create(default(T), new HTTPCode { statusCode = 400, reason = "Exception" });
            }
        }
Exemple #2
0
 public HTTPResult(HTTPCode status, Task <T> task)
 {
     this.status = status;
     this.task   = task;
 }
Exemple #3
0
 public HTTPResult(HTTPCode code, T payload)
 {
     Payload = payload;
     Result  = code;
 }