public static async Task <HttpResponseMessage> Update <E>(E entity, string entityURL, int id) where E : class
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(UtilDAO.UrlApi());
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                string urlEndPoint = client.BaseAddress.AbsoluteUri + entityURL + "/" + id.ToString() + "/";

                HttpResponseMessage response = await client.PutAsJsonAsync(urlEndPoint, entity);

                return(response);
            }
        }
        public static async Task <HttpResponseMessage> Delete(int id, string entityURL)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(UtilDAO.UrlApi());
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                string urlEndPoint = client.BaseAddress.AbsoluteUri + entityURL + "/" + id.ToString();

                HttpResponseMessage response = await client.DeleteAsync(urlEndPoint);

                return(response);
            }
        }
        public static async Task <List <E> > GetAll <E>(string entityURL) where E : class
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(UtilDAO.UrlApi());
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                HttpResponseMessage response = await client.GetAsync("/" + entityURL);

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

                    return(Newtonsoft.Json.JsonConvert.DeserializeObject <List <E> >(json).ToList());
                }
                else
                {
                    throw new Exception(response.RequestMessage.ToString());
                }
            }
        }
예제 #4
0
        /// <summary>
        /// GetCliente - Buscar/Listar TODOS os Emitentes
        /// </summary>
        /// <returns></returns>
        public static async Task <Cliente> GetClienteByIdDAO(string nomeUsuario)
        {
            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(UtilDAO.UrlApi());
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    HttpResponseMessage response = await client.GetAsync("cliente/" + nomeUsuario);

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

                        return(Newtonsoft.Json.JsonConvert.DeserializeObject <Cliente>(json));
                    }
                    else
                    {
                        throw new Exception(response.RequestMessage.ToString());
                    }
                }
            }
            catch (JsonException ex)
            {
                throw new Exception("JsonException - Não foi possivel buscar o cliente. Erro: " + ex.Message);
            }
            catch (HttpRequestException ex)
            {
                throw new Exception("HttpRequestException - Não foi possivel buscar o cliente. Erro: " + ex.Message);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception - Não foi possivel buscar os Emitentes. Erro: " + ex.Message);
            }
        }