Exemplo n.º 1
0
        internal async Task <T> GetAsync <T>(string uri, int id)
        {
            try
            {
                uri = string.IsNullOrEmpty(uri) ? $"{id}" : $"{uri}/{id}";
                var response = await ClientContext.GetAsync(uri);

                if (response.IsSuccessStatusCode)
                {
                    return(ConvertResponseToObject <T>(response));
                }
            }
            catch (AggregateException ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }
            catch (WebException ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }
            catch (Exception ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }

            return(default);
Exemplo n.º 2
0
        internal async Task <T> GetAsync <T>(string uri)
        {
            try
            {
                var response = await ClientContext.GetAsync(uri);

                if (response.IsSuccessStatusCode)
                {
                    return(ConvertResponseToObject <T>(response));
                }
            }
            catch (AggregateException ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }
            catch (WebException ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }
            catch (Exception ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }

            return(default(T));
        }
Exemplo n.º 3
0
        internal async Task <T> Delete <T>(string uri, int id)
        {
            try
            {
                var response = await ClientContext.DeleteAsync(uri + "/" + id);

                if (response.IsSuccessStatusCode)
                {
                    return(ConvertResponseToObject <T>(response));
                }
                AnotherResponse(response);
            }
            catch (AggregateException ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }
            catch (WebException ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }
            catch (Exception ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }

            return(default(T));
        }
Exemplo n.º 4
0
        internal async Task <T> PutAsync <T>(string uri, object id, T content)
        {
            try
            {
                var objJson  = JsonConvert.SerializeObject(content);
                var response = await ClientContext.PutAsJsonAsync <T>(uri + "/" + id, content);

                if (response.IsSuccessStatusCode)
                {
                    return(ConvertResponseToObject <T>(response));
                }
                AnotherResponse(response);
            }
            catch (AggregateException ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }
            catch (WebException ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }
            catch (Exception ex)
            {
                ResourcesBase.ShowMessageError(ex.Message);
            }

            return(default(T));
        }
Exemplo n.º 5
0
        private void AnotherResponse(HttpResponseMessage response)
        {
            string message = response.Content.ReadAsStringAsync().Result;

            switch (response.StatusCode)
            {
            //case HttpStatusCode.Continue:
            //    break;
            //case HttpStatusCode.SwitchingProtocols:
            //    break;
            //case HttpStatusCode.OK:
            //    break;
            //case HttpStatusCode.Created:
            //    break;
            //case HttpStatusCode.Accepted:
            //    break;
            //case HttpStatusCode.NonAuthoritativeInformation:
            //    break;
            //case HttpStatusCode.NoContent:
            //    break;
            //case HttpStatusCode.ResetContent:
            //    break;
            //case HttpStatusCode.PartialContent:
            //    break;
            //case HttpStatusCode.MultipleChoices:
            //    break;

            //case HttpStatusCode.MovedPermanently:
            //    break;

            case HttpStatusCode.Found:
                ResourcesBase.ShowMessageError("Data Tidak Ditemukan");
                break;

            case HttpStatusCode.SeeOther:
                break;

            case HttpStatusCode.NotModified:
                ResourcesBase.ShowMessageError("Data Tidak Tersimpan");
                break;

            //case HttpStatusCode.UseProxy:
            //    break;
            //case HttpStatusCode.Unused:
            //    break;
            //case HttpStatusCode.TemporaryRedirect:
            //    break;
            case HttpStatusCode.BadRequest:

                ResourcesBase.ShowMessageError(message);

                break;

            case HttpStatusCode.Unauthorized:
                ResourcesBase.ShowMessageError("Anda Tidak Memiliki Access");
                break;

            //case HttpStatusCode.PaymentRequired:
            //    break;
            //case HttpStatusCode.Forbidden:
            //    break;
            case HttpStatusCode.NotFound:
                ResourcesBase.ShowMessageError("Data Request Tidak Ditemukan");
                break;

            //case HttpStatusCode.MethodNotAllowed:
            //    break;
            //case HttpStatusCode.NotAcceptable:
            //    break;
            //case HttpStatusCode.ProxyAuthenticationRequired:
            //    break;
            case HttpStatusCode.RequestTimeout:
                ResourcesBase.ShowMessageError("Waktu Request Terlalu Lama");
                break;

            //case HttpStatusCode.Conflict:
            //    break;
            //case HttpStatusCode.Gone:
            //    break;
            //case HttpStatusCode.LengthRequired:
            //    break;
            //case HttpStatusCode.PreconditionFailed:
            //    break;
            //case HttpStatusCode.RequestEntityTooLarge:
            //    break;
            //case HttpStatusCode.RequestUriTooLong:
            //    break;
            //case HttpStatusCode.UnsupportedMediaType:
            //    break;
            //case HttpStatusCode.RequestedRangeNotSatisfiable:
            //    break;
            //case HttpStatusCode.ExpectationFailed:
            //    break;
            //case HttpStatusCode.UpgradeRequired:
            //    break;
            //case HttpStatusCode.InternalServerError:
            //    break;
            //case HttpStatusCode.NotImplemented:
            //    break;
            //case HttpStatusCode.BadGateway:
            //    break;
            //case HttpStatusCode.ServiceUnavailable:
            //    break;
            //case HttpStatusCode.GatewayTimeout:
            //    break;
            //case HttpStatusCode.HttpVersionNotSupported:
            //    break;
            default:
                message = response.Content.ReadAsStringAsync().Result;
                ResourcesBase.ShowMessageError(message);
                break;
            }
        }