Exemplo n.º 1
0
        public static async Task <RESTServiceResponse <T> > GetRequest <T>(string token, string url, HttpVerbs method = HttpVerbs.GET, NameValueCollection getParams = null, object postObject = null, string contentType = "application/json")
        {
            try
            {
                #region IsConnected

                bool IsConnected = Plugin.Connectivity.CrossConnectivity.Current.IsConnected;
                if (!IsConnected)
                {
                    return(new RESTServiceResponse <T>(false, "Vous n'êtes pas connéctés !"));
                }
                #endregion


                using (var client = new HttpClient())
                {
                    //setup client
                    Uri uri = new Uri(url);
                    #region Setting Attachements

                    if (method == HttpVerbs.GET && getParams != null)
                    {
                        uri = uri.AttachParameters(getParams);
                    }

                    #endregion
                    client.BaseAddress = uri;
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

                    //make request
                    HttpResponseMessage response = new HttpResponseMessage();
                    switch (method)
                    {
                    case HttpVerbs.GET:
                        try
                        {
                            response = await client.GetAsync(uri);
                        }
                        catch (Exception)
                        {
                        }
                        break;

                    case HttpVerbs.POST:
                        try
                        {
                            var content = new StringContent(JsonConvert.SerializeObject(postObject).ToString(), Encoding.UTF8, "application/json");
                            response = await client.PostAsync(uri, content);
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(ex.Message);
                        }
                        break;

                    default:
                        break;
                    }

                    var stringResponseJson = await response.Content?.ReadAsStringAsync();

                    RESTServiceResponse <T> result = JsonConvert.DeserializeObject <RESTServiceResponse <T> >(stringResponseJson);

                    return(result);
                }
            }
            catch (Exception ex)
            {
                return(new RESTServiceResponse <T>(false, ex.Message));
            }
        }
Exemplo n.º 2
0
        public static async Task <RESTServiceResponse <T> > GetRequest <T>(string token, string url, HttpVerbs method = HttpVerbs.GET, NameValueCollection getParams = null, object postObject = null, string contentType = "application/json")
        {
            try
            {
                #region IsConnected

                bool IsConnected = Plugin.Connectivity.CrossConnectivity.Current.IsConnected;
                if (!IsConnected)
                {
                    return(new RESTServiceResponse <T>(false, "Vous n'êtes pas connéctés !"));
                }
                #endregion


                using (var client = new HttpClient())
                {
                    //setup client
                    Uri uri = new Uri(url);
                    #region Setting Attachements

                    if (method == HttpVerbs.GET && getParams != null)
                    {
                        uri = uri.AttachParameters(getParams);
                    }

                    #endregion
                    client.BaseAddress = uri;
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                    client.Timeout = TimeSpan.FromSeconds(30);

                    //make request
                    HttpResponseMessage response = new HttpResponseMessage();
                    switch (method)
                    {
                    case HttpVerbs.GET:
                        try
                        {
                            response = await client.GetAsync(uri);
                        }
                        catch (Exception ex)
                        {
                            return(new RESTServiceResponse <T>(false, WinxoPriceUpdate.Assets.Strings.ErreurMessage));
                        }
                        break;

                    case HttpVerbs.POST:
                        try
                        {
                            var content = new StringContent(JsonConvert.SerializeObject(postObject).ToString(), Encoding.UTF8, contentType);
                            response = await client.PostAsync(uri, content);
                        }
                        catch (Exception ex)
                        {
                            return(new RESTServiceResponse <T>(false, WinxoPriceUpdate.Assets.Strings.ErreurMessage));
                        }
                        break;

                    default:
                        break;
                    }

                    var stringResponseJson = await response.Content?.ReadAsStringAsync();

                    /*if (!string.IsNullOrEmpty(datetimeformat) && !string.IsNullOrWhiteSpace(datetimeformat))
                     * {
                     *  var dateTimeConverter = new IsoDateTimeConverter { DateTimeFormat = datetimeformat };
                     *  RESTServiceResponse<T> result = JsonConvert.DeserializeObject<RESTServiceResponse<T>>(stringResponseJson, dateTimeConverter);
                     *  return result;
                     * }
                     * else
                     * {*/
                    RESTServiceResponse <T> result = JsonConvert.DeserializeObject <RESTServiceResponse <T> >(stringResponseJson);
                    return(result);
                    //}
                }
            }
            catch (Exception ex)
            {
                //return new RESTServiceResponse<T>(false, ex.Message);
                return(new RESTServiceResponse <T>(false, WinxoPriceUpdate.Assets.Strings.ErreurMessage));
            }
        }