예제 #1
0
        public void Carrega()
        {
            // Load
            Loading = true;

            // Busca Dados Corrida pesquisada
            var dadosCorridaPesquisa = GlobalVariablesManager.GetApplicationCurrentProperty(GlobalVariablesManager.VariaveisGlobais.DadosCorridaPesquisada) as ChamadoEncontradoItemViewModel;

            if (dadosCorridaPesquisa != null)
            {
                chamadoItem = dadosCorridaPesquisa;
            }
        }
예제 #2
0
        public static async Task <T> RequestAsync(string URL, RequestType requestType = RequestType.Get, string requestBody = null, int triesNumber = 0, string contentType = "application/json")
        {
            T tReturn = null;

            for (int i = 0; i <= triesNumber; i++)
            {
                try
                {
                    using (HttpClient client = new HttpClient())
                    {
                        HttpResponseMessage response = new HttpResponseMessage();

                        client.BaseAddress = new Uri(BaseUrl);

                        if (!string.IsNullOrEmpty(Token))
                        {
                            client.DefaultRequestHeaders.Add("Authorization", (string)GlobalVariablesManager.GetApplicationCurrentProperty("token"));
                        }

                        HttpContent httpContent = new StringContent(requestBody, Encoding.UTF8, contentType);

                        switch (requestType)
                        {
                        case RequestType.Get:
                            response = await client.GetAsync(URL);

                            break;

                        case RequestType.Post:
                            response = await client.PostAsync(URL, httpContent);

                            break;

                        case RequestType.Put:
                            response = await client.PutAsync(URL, httpContent);

                            break;

                        case RequestType.Delete:
                            response = await client.DeleteAsync(URL);

                            break;

                        default:
                            break;
                        }

                        if (response.IsSuccessStatusCode)
                        {
                            string responseString = await response.Content.ReadAsStringAsync();

                            var objetoRetorno = JsonHelper <T> .JsonToObject(responseString);


                            tReturn = objetoRetorno;
                        }
                        else
                        {
                            if (response.StatusCode == System.Net.HttpStatusCode.RequestTimeout)
                            {
                                if (i == triesNumber)
                                {
                                    throw new Exception("Tempo limite atingido");
                                }
                            }
                            else if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
                            {
                                throw new Exception("Serviço não encontrado");
                            }
                            else if (response.StatusCode == System.Net.HttpStatusCode.InternalServerError)
                            {
                                if (i == triesNumber)
                                {
                                    throw new Exception("Serviço indisponível no momento");
                                }
                            }
                            else
                            {
                                throw new Exception("Falha no acesso!");
                            }
                        }
                    }
                }
                catch (JsonException x)
                {
                    throw x;
                }
                catch (Exception x)
                {
                    throw x;
                }
            }

            return(tReturn);
        }