コード例 #1
0
 public RestResponse(ErrorResponseModel error)
 {
     this.Success = false;
     this.IsException = false;
     this.Data = null;
     this.Error = error;
 }
コード例 #2
0
 public RestResponse(Exception exception)
 {
     this.Success = false;
     this.IsException = true;
     this.Data = exception.Message;
     this.Error = null;
 }
コード例 #3
0
 public RestResponse(string data)
 {
     this.Success = true;
     this.IsException = false;
     this.Data = data;
     this.Error = null;
 }
コード例 #4
0
        private static async Task <RestResponse> Send(CancellationToken ct, string method, string url, string json, Guid appKey, Guid token, int timeoutInMinutes)
        {
            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = new TimeSpan(0, timeoutInMinutes, 0); // 1 minute timeout
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                httpClient.DefaultRequestHeaders.Add("user-agent", UserAgent());

                if (appKey != Guid.Empty)
                {
                    httpClient.DefaultRequestHeaders.Add("appkey", appKey.ToString());
                }
                if (token != Guid.Empty)
                {
                    httpClient.DefaultRequestHeaders.Add("token", token.ToString());
                }

                Coordinate location = UserLocation();
                if (location != null)
                {
                    httpClient.DefaultRequestHeaders.Add("latitude", location.Latitude.ToString(CultureInfo.InvariantCulture));
                    httpClient.DefaultRequestHeaders.Add("longitude", location.Longitude.ToString(CultureInfo.InvariantCulture));
                }

                HttpResponseMessage response = new HttpResponseMessage();

                try
                {
                    if (method == "POST")
                    {
                        response = await httpClient.PostAsync(url, new StringContent(json, Encoding.UTF8, "application/json"), ct);
                    }
                    else if (method == "PUT")
                    {
                        response = await httpClient.PutAsync(url, new StringContent(json, Encoding.UTF8, "application/json"), ct);
                    }
                    else if (method == "PATCH")
                    {
                        response = await httpClient.PatchAsync(url, new StringContent(json, Encoding.UTF8, "application/json"), ct);
                    }

                    if (response.IsSuccessStatusCode)
                    {
                        try
                        {
                            var data = response.Content.ReadAsStringAsync().Result;

                            return(new RestResponse(data));
                        }
                        catch (Exception)
                        {
                            throw new Exception(AppResources.ApiErrorPopupMessageUnknownError);
                        }
                    }
                    else
                    {
                        string errorResponseString = response.Content.ReadAsStringAsync().Result;

                        Debug.WriteLine(errorResponseString);

                        if (!String.IsNullOrEmpty(errorResponseString))
                        {
                            ErrorResponseModel errorResponse = null;
                            try
                            {
                                errorResponse = JsonConvert.DeserializeObject <ErrorResponseModel>(errorResponseString);

                                return(new RestResponse(errorResponse));
                            }
                            catch (Exception) { }
                        }

                        if (response.StatusCode == HttpStatusCode.NotFound)
                        {
                            return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageNotFound)));
                        }
                        else if (response.StatusCode == HttpStatusCode.BadRequest)
                        {
                            return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageBadRequest)));
                        }
                        else if (response.StatusCode == HttpStatusCode.InternalServerError)
                        {
                            return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageInternalServerError)));
                        }
                        else
                        {
                            return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageUnknownError)));
                        }
                    }
                }
                catch (TaskCanceledException)
                {
                    if (ct.IsCancellationRequested)
                    {
                        return(new RestResponse(new Exception(AppResources.ApiErrorTaskCancelled)));
                    }

                    return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageTimeout)));
                }
                catch (Exception)
                {
                    return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageUnknownError)));
                }
            }
        }
コード例 #5
0
        public static async Task <RestResponse> Get(CancellationToken ct, string url)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                var handler = new HttpClientHandler();
                if (handler.SupportsAutomaticDecompression)
                {
                    handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                }
                using (HttpClient httpClient = new HttpClient(handler))
                {
                    httpClient.Timeout = new TimeSpan(0, 1, 0); // 1 minute timeout
                    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpResponseMessage response = new HttpResponseMessage();

                    try
                    {
                        response = await httpClient.GetAsync(url, ct);

                        if (response.IsSuccessStatusCode)
                        {
                            try
                            {
                                var data = response.Content.ReadAsStringAsync().Result;

                                return(new RestResponse(data));
                            }
                            catch (Exception)
                            {
                                throw new Exception(AppResources.ApiErrorPopupMessageUnknownError);
                            }
                        }
                        else
                        {
                            string errorResponseString = response.Content.ReadAsStringAsync().Result;

                            Debug.WriteLine(errorResponseString);

                            if (!String.IsNullOrEmpty(errorResponseString))
                            {
                                ErrorResponseModel errorResponse = null;
                                try
                                {
                                    errorResponse = JsonConvert.DeserializeObject <ErrorResponseModel>(errorResponseString);

                                    return(new RestResponse(errorResponse));
                                }
                                catch (Exception) { }
                            }

                            if (response.StatusCode == HttpStatusCode.NotFound)
                            {
                                return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageNotFound)));
                            }
                            else if (response.StatusCode == HttpStatusCode.BadRequest)
                            {
                                return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageBadRequest)));
                            }
                            else if (response.StatusCode == HttpStatusCode.InternalServerError)
                            {
                                return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageInternalServerError)));
                            }
                            else
                            {
                                return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageUnknownError)));
                            }
                        }
                    }
                    catch (TaskCanceledException)
                    {
                        if (ct.IsCancellationRequested)
                        {
                            return(new RestResponse(new Exception(AppResources.ApiErrorTaskCancelled)));
                        }

                        return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageTimeout)));
                    }
                    catch (Exception)
                    {
                        return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageUnknownError)));
                    }
                }
            }
            else
            {
                return(new RestResponse(new Exception(AppResources.ApiErrorPopupMessageNoNetwork)));
            }
        }