Ejemplo n.º 1
0
        public async Task <OUT> PostAsync <IN, OUT>(string url, IN input, Dictionary <string, string> queryParams = null)
        {
            var http = new HttpClient();
            var uri  = AppendQueryParam(url, queryParams);

            try
            {
                string body = string.Empty;

                var content  = SerializeObject(input);
                var response = await http.PostAsync(uri, new StringContent(string.IsNullOrEmpty(content) ? "{}" : content, Encoding.UTF8, "application/json"));

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

                    HttpStatusCodeResult?.Invoke(response.StatusCode, uri, content, body);
                    return(TypeConvert <OUT>(body));
                }
                else
                {
                    HttpStatusCodeResult?.Invoke(response.StatusCode, uri, content, body, false);
                }
            }
            catch (Exception e)
            {
                ErrorResult?.Invoke(e.GetBaseException(), uri);
            }
            finally
            {
                http.Dispose();
            }

            return(default(OUT));
        }
Ejemplo n.º 2
0
        public async Task <T> GetAsync <T>(string url, Dictionary <string, string> queryParams = null)
        {
            var http = new HttpClient();
            var uri  = AppendQueryParam(url, queryParams);

            try
            {
                var response = await http.GetAsync(uri);

                string body = string.Empty;

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

                    HttpStatusCodeResult?.Invoke(response.StatusCode, uri, null, body);
                    return(TypeConvert <T>(body));
                }
                else
                {
                    HttpStatusCodeResult?.Invoke(response.StatusCode, uri, null, body, false);
                }
            }
            catch (Exception e)
            {
                ErrorResult?.Invoke(e.GetBaseException(), uri);
            }
            finally
            {
                http.Dispose();
            }

            return(default(T));
        }