예제 #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));
        }
예제 #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));
        }
예제 #3
0
        public async Task <OUT> PatchAsync <IN, OUT>(string url, IN input, Dictionary <string, string> queryParams = null, bool silent = false)
        {
            var http = CreateHttpClient();
            var uri  = AppendQueryParam(url, queryParams);

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

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

                    SuccessfulStatusCodeResult?.Invoke(response.StatusCode, uri, content, body);
                    return(TypeConvert <OUT>(body));
                }
                else
                {
                    if (!silent)
                    {
                        UnsuccessfulStatusCodeResult?.Invoke(response.StatusCode, uri, content, await response.Content.ReadAsStringAsync());
                    }
                }
            }
            catch (Exception e)
            {
                if (!silent)
                {
                    ErrorResult?.Invoke(e.GetBaseException(), uri);
                }
                Log.Error(e, "Error while PatchAsync.");
            }
            finally
            {
                http.Dispose();
            }

            return(default(OUT));
        }
예제 #4
0
        public async Task <T> DeleteAsync <T>(string url, Dictionary <string, string> queryParams = null, bool silent = false)
        {
            var http = CreateHttpClient();
            var uri  = AppendQueryParam(url, queryParams);

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

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

                    SuccessfulStatusCodeResult?.Invoke(response.StatusCode, uri, null, body);
                    return(TypeConvert <T>(body));
                }
                else
                {
                    if (!silent)
                    {
                        UnsuccessfulStatusCodeResult?.Invoke(response.StatusCode, uri, null, await response.Content.ReadAsStringAsync());
                    }
                }
            }
            catch (Exception e)
            {
                if (!silent)
                {
                    ErrorResult?.Invoke(e.GetBaseException(), uri);
                }
                Log.Error(e, "Error while DeleteAsync.");
            }
            finally
            {
                http.Dispose();
            }

            return(default(T));
        }
예제 #5
0
        public async Task <OUT> PostAsync <OUT>(string url, string path, List <KeyValuePair <string, string> > values = null, Dictionary <string, string> queryParams = null, bool silent = false)
        {
            var http = CreateHttpClient();
            var uri  = AppendQueryParam(url, queryParams);

            try
            {
                using (var content = new MultipartFormDataContent())
                {
                    if (values != null)
                    {
                        foreach (var keyValuePair in values)
                        {
                            content.Add(new StringContent(keyValuePair.Value), $"\"{keyValuePair.Key}\"");
                        }
                    }

                    if (!string.IsNullOrEmpty(path))
                    {
                        var fileName  = Path.GetFileName(path);
                        var extension = Path.GetExtension(fileName).Replace(".", "").ToLower();
                        var data      = new StreamContent(File.OpenRead(path));
                        data.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
                        {
                            Name     = "\"paperImage\"",
                            FileName = $"\"{fileName}\""
                        };
                        data.Headers.ContentType = new MediaTypeHeaderValue($"image/{extension}");
                        content.Add(data);
                    }

                    var response = await http.PostAsync(uri, content);

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

                        SuccessfulStatusCodeResult?.Invoke(response.StatusCode, uri, null, body);
                        return(TypeConvert <OUT>(body));
                    }
                    else
                    {
                        if (!silent)
                        {
                            UnsuccessfulStatusCodeResult?.Invoke(response.StatusCode, uri, null, await response.Content.ReadAsStringAsync());
                        }
                    }
                }
            }
            catch (Exception e)
            {
                if (!silent)
                {
                    ErrorResult?.Invoke(e.GetBaseException(), uri);
                }
                Log.Error(e, "Error while PostAsync.");
            }
            finally
            {
                http.Dispose();
            }

            return(default(OUT));
        }