Esempio n. 1
0
        public static ITithelyRestResponse <S> ToTithelyResponse <S>(this IRestResponse <S> restResponse) where S : new()
        {
            var response = new TithelyRestResponse <S>();

            response.StatusCode   = restResponse.StatusCode;
            response.JsonResponse = restResponse.Content;

            if ((int)restResponse.StatusCode >= 300)
            {
                response.ErrorMessage = restResponse.ErrorMessage;
            }
            else
            {
                if (restResponse.Content.Contains("status"))
                {
                    var data   = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(restResponse.Content);
                    var status = data.status.ToString();

                    if (status == "fail")
                    {
                        response.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                }

                response.Result = restResponse.Data;
            }
            return(response);
        }
        public async Task <ITithelyRestResponse <T> > CreateWithHttp(string url = "")
        {
            using (var client = new HttpClient())
            {
                var urlData = new List <KeyValuePair <string, string> >();
                _parameters.ToList().ForEach(x =>
                {
                    urlData.Add(new KeyValuePair <string, string>(x.Key, x.Value));
                });

                var request = new HttpRequestMessage(HttpMethod.Post, url)
                {
                    Content = new FormUrlEncodedContent(urlData)
                };
                request.Headers.Authorization = new AuthenticationHeaderValue("Basic", CreateAuthValue());

                var res = await client.SendAsync(request);

                var response = new TithelyRestResponse <T>();

                response.StatusCode   = res.StatusCode;
                response.JsonResponse = await res.Content.ReadAsStringAsync();

                if (response.JsonResponse.Contains("status"))
                {
                    var data   = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(response.JsonResponse);
                    var status = data.status.ToString();

                    if (status == "fail")
                    {
                        response.StatusCode = System.Net.HttpStatusCode.BadRequest;
                    }
                    else
                    {
                        try
                        {
                            response.Result = JsonConvert.DeserializeObject <T>(response.JsonResponse);
                        }
                        catch (Exception e)
                        {
                            var t = e.Message;
                        }
                    }
                }

                return(response);
            }
        }
        private async Task <ITithelyRestResponse <S> > ConvertResponseAsync <S>(HttpResponseMessage response) where S : new()
        {
            var tithelyResponse = new TithelyRestResponse <S> {
                StatusCode   = response.StatusCode,
                JsonResponse = await response.Content.ReadAsStringAsync()
            };

            if (!string.IsNullOrEmpty(tithelyResponse.JsonResponse) && (int)response.StatusCode > 300)
            {
                var responseError = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(tithelyResponse.JsonResponse);
                tithelyResponse.ErrorMessage = responseError.error_message;
            }
            else
            {
                tithelyResponse.Result = JsonConvert.DeserializeObject <S>(tithelyResponse.JsonResponse);
            }
            return(tithelyResponse);
        }