Example #1
0
        public async Task <T> Get <T>(string url, TimeSpan timeout, bool usingNetworkStackCache = false) where T : new()
        {
            if (!usingNetworkStackCache)
            {
                MakeQueryUnique(ref url);
            }

            using (var handler = new HttpClientHandler())
            {
                using (var httpClient = new HttpClient(handler))
                {
                    httpClient.Timeout = timeout;

                    var request = new HttpRequestMessage(HttpMethod.Get, url);
                    if (handler.SupportsTransferEncodingChunked())
                    {
                        request.Headers.TransferEncodingChunked = true;
                    }

                    var response = await httpClient.SendAsync(request);

                    var stringResponse = await response.Content.ReadAsStringAsync();

                    return(_deserializer.Deserialize <T>(stringResponse));
                }
            }
        }
Example #2
0
        public static async Task <T> postTable <T>(this T entidad) where T : IEntidadBase
        {
            if (string.IsNullOrEmpty(entidad.nombreTabla))
            {
                entidad.nombreTabla = entidad.GetType().Name.eliminarCaracteresEspeciales().ToLower();
            }

            if (string.IsNullOrEmpty(entidad.PartitionKey))
            {
                entidad.PartitionKey = entidad.GetType().FullName.ToLower();
            }

            if (string.IsNullOrEmpty(entidad.RowKey))
            {
                string row = string.Format("{0}{1}{2}tick{3}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year, DateTime.Now.Ticks);
                entidad.RowKey = entidad.GetType().Namespace.ToLower() + row;
            }


            try
            {
                T      valorRetorno;
                string json = JsonConvert.SerializeObject(entidad, Formatting.Indented,
                                                          new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });

                HttpClientHandler handler  = new HttpClientHandler();
                var httpClient             = new HttpClient(handler);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Path_Servicio.obtenerUrlServicio() + "table");
                request.Content = new StringContent(json);
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Hefesoft.Standard.Static.Variables_Globales.Bearer);

                MediaTypeHeaderValue contentType = request.Content.Headers.ContentType;
                contentType.MediaType = "application/json";

                request.Content.Headers.ContentType = contentType;

                if (handler.SupportsTransferEncodingChunked())
                {
                    request.Headers.TransferEncodingChunked = true;
                }
                HttpResponseMessage response = await httpClient.SendAsync(request);

                var resultadoString = response.Content.ReadAsStringAsync().Result;

                valorRetorno = JsonConvert.DeserializeObject <T>(resultadoString);

                return(valorRetorno);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #3
0
        private HttpClient CreateClient()
        {
            var handler = new HttpClientHandler();

            if (handler.SupportsAutomaticDecompression)
            {
                handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            }

            var client = new HttpClient(handler, true);

            client.BaseAddress = new Uri(_baseUrl, UriKind.Absolute);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/json"));
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/x-json"));
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/javascript"));

            if (handler.SupportsTransferEncodingChunked())
            {
                client.DefaultRequestHeaders.TransferEncodingChunked = true;
            }

            if (_defaults != null)
            {
                ProductInfoHeaderValue productHeader = null;
                if (!string.IsNullOrEmpty(_defaults.UserAgent) && ProductInfoHeaderValue.TryParse(_defaults.UserAgent, out productHeader))
                {
                    client.DefaultRequestHeaders.UserAgent.Clear();
                    client.DefaultRequestHeaders.UserAgent.Add(productHeader);
                }

                foreach (var kvp in _defaults.DefaultHeaders)
                {
                    client.DefaultRequestHeaders.Add(kvp.Key, kvp.Value);
                }

                if (!string.IsNullOrEmpty(_defaults.AuthToken) && !string.IsNullOrEmpty(_defaults.AuthScheme))
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(_defaults.AuthScheme, _defaults.AuthToken);
                }
            }

            return(client);
        }
        public async Task <TokenResponseModel> sigIn(Usuario entidad)
        {
            try
            {
                string json = JsonConvert.SerializeObject(entidad, Formatting.Indented,
                                                          new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });

                HttpClientHandler handler  = new HttpClientHandler();
                var httpClient             = new HttpClient(handler);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Path_Servicio.obtenerUrl() + "token");

                //request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Hefesoft.Standard.Static.Variables_Globales.Bearer);
                request.Content = new StringContent("grant_type=password&username="******"&password="******"application/x-www-form-urlencoded");
                MediaTypeHeaderValue contentType = request.Content.Headers.ContentType;
                contentType.MediaType = "application/json";



                request.Content.Headers.ContentType = contentType;

                if (handler.SupportsTransferEncodingChunked())
                {
                    request.Headers.TransferEncodingChunked = true;
                }
                HttpResponseMessage response = await httpClient.SendAsync(request);

                var resultadoString = response.Content.ReadAsStringAsync().Result;

                TokenResponseModel tokenResponse = (TokenResponseModel)JsonConvert.DeserializeObject(resultadoString, typeof(TokenResponseModel));
                Hefesoft.Standard.Static.Variables_Globales.Bearer = tokenResponse.AccessToken;
                return(tokenResponse);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #5
0
        public static async Task <T> getTableByPartitionAndRowKey <T>(this T entidad, string rowKey) where T : IEntidadBase
        {
            T valor_retorno = default(T);

            if (string.IsNullOrEmpty(entidad.nombreTabla))
            {
                entidad.nombreTabla = entidad.GetType().Name.eliminarCaracteresEspeciales().ToLower();
            }

            if (string.IsNullOrEmpty(entidad.PartitionKey))
            {
                entidad.PartitionKey = entidad.GetType().FullName.ToLower();
            }

            string json       = JsonConvert.SerializeObject(entidad);
            string parameters = string.Format("table/?nombreTabla={0}&partitionKey={1}&rowKey={2}", entidad.nombreTabla, entidad.PartitionKey, rowKey);

            HttpClientHandler handler  = new HttpClientHandler();
            var httpClient             = new HttpClient(handler);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Path_Servicio.obtenerUrlServicio() + parameters);

            if (handler.SupportsTransferEncodingChunked())
            {
                request.Headers.TransferEncodingChunked = true;
            }
            HttpResponseMessage response = await httpClient.SendAsync(request);

            try
            {
                var resultadoString = response.Content.ReadAsStringAsync().Result;
                valor_retorno = JsonConvert.DeserializeObject <T>(resultadoString);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(valor_retorno);
        }
        public async Task <string> registerPushService(PushAzureService push)
        {
            try
            {
                string json = JsonConvert.SerializeObject(push, Formatting.Indented,
                                                          new JsonSerializerSettings
                {
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                });

                HttpClientHandler handler  = new HttpClientHandler();
                var httpClient             = new HttpClient(handler);
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Path_Servicio.obtenerUrlServicio() + "Register");

                request.Headers.Authorization = new AuthenticationHeaderValue(Hefesoft.Standard.Static.Variables_Globales.Bearer);
                request.Content = new StringContent(json);
                MediaTypeHeaderValue contentType = request.Content.Headers.ContentType;
                contentType.MediaType = "application/json";



                request.Content.Headers.ContentType = contentType;

                if (handler.SupportsTransferEncodingChunked())
                {
                    request.Headers.TransferEncodingChunked = true;
                }
                HttpResponseMessage response = await httpClient.SendAsync(request);

                var resultadoString = response.Content.ReadAsStringAsync().Result;

                var idHub = JsonConvert.DeserializeObject <string>(resultadoString);
                return(idHub);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private static async Task <T> doGetParameters <T>(T valorRetorno, string parameters) where T : IEntidadBase
        {
            HttpClientHandler handler  = new HttpClientHandler();
            var httpClient             = new HttpClient(handler);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Path_Servicio.obtenerUrlServicio() + parameters);

            if (handler.SupportsTransferEncodingChunked())
            {
                request.Headers.TransferEncodingChunked = true;
            }
            HttpResponseMessage response = await httpClient.SendAsync(request);

            try
            {
                var resultadoString = response.Content.ReadAsStringAsync().Result;
                valorRetorno = JsonConvert.DeserializeObject <T>(resultadoString);
            }
            catch
            {
            }
            return(valorRetorno);
        }
        private static async Task <string> doPost(string json)
        {
            HttpClientHandler handler  = new HttpClientHandler();
            var httpClient             = new HttpClient(handler);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Path_Servicio.obtenerUrlServicio() + "blob");

            request.Content = new StringContent(json);
            MediaTypeHeaderValue contentType = request.Content.Headers.ContentType;

            contentType.MediaType = "application/json";

            request.Content.Headers.ContentType = contentType;

            if (handler.SupportsTransferEncodingChunked())
            {
                request.Headers.TransferEncodingChunked = true;
            }
            HttpResponseMessage response = await httpClient.SendAsync(request);

            var resultadoString = response.Content.ReadAsStringAsync().Result;

            return(resultadoString);
        }