public async Task <string> UploadFileAsync <TRequest>(
            string uri,
            TRequest data,
            IFormFile file,
            IReadOnlyCollection <KeyValuePair <string, string> > headers = null,
            HttpQuerySetting setting = null) where TRequest : class
        {
            CheckDisposed();

            using var requestContent = new MultipartFormDataContent();
            var streamContent = MapToStreamContent(file);

            requestContent.Add(streamContent, "file", file.FileName);

            var jsonContent = CreateJsonContent(data);

            requestContent.Add(jsonContent, "data");

            using var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri)
                  {
                      Content = requestContent
                  };
            var result = await SendAsync(requestMessage, headers, setting).ConfigureAwait(false);

            return(result);
        }
        public async Task <string> DeleteAsync(
            string uri,
            IReadOnlyCollection <KeyValuePair <string, string> > headers = null,
            HttpQuerySetting setting = null)
        {
            CheckDisposed();

            using var requestMessage = new HttpRequestMessage(HttpMethod.Delete, uri);
            return(await SendAsync(requestMessage, headers, setting).ConfigureAwait(false));
        }
        public async Task <string> PostAsync <TRequest>(
            string uri,
            TRequest data,
            IReadOnlyCollection <KeyValuePair <string, string> > headers = null,
            HttpQuerySetting setting = null)
            where TRequest : class
        {
            CheckDisposed();

            using var requestMessage = new HttpRequestMessage(HttpMethod.Post, uri);
            requestMessage.Content   = CreateJsonContent(data);
            var result = await SendAsync(requestMessage, headers, setting).ConfigureAwait(false);

            return(result);
        }
Ejemplo n.º 4
0
        protected async Task PostAsync(
            string uri,
            IReadOnlyCollection <KeyValuePair <string, string> > queryHeaders = null,
            HttpQuerySetting setting = null)
        {
            var fullUri = uriCreator.Create(GetApiEndpoint(), uri);

            try
            {
                await httpRequestExecutor.PostAsync(fullUri, AddDefaultHeaders(queryHeaders), setting ?? DefaultHttpQuerySetting()).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        private async Task <string> SendAsync(
            HttpRequestMessage requestMessage,
            IReadOnlyCollection <KeyValuePair <string, string> > headers = null,
            HttpQuerySetting setting = null)
        {
            AddHeaders(requestMessage, headers);

            if (setting == null)
            {
                setting = new HttpQuerySetting();
            }

            using var cts      = new CancellationTokenSource(setting.Timeout);
            using var response = await httpClient.SendAsync(requestMessage, cts.Token).ConfigureAwait(false);

            var result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            return(result);
        }
Ejemplo n.º 6
0
        protected async Task <string> SendFileAsync(
            string uri,
            IFormFile file,
            IReadOnlyCollection <KeyValuePair <string, string> > queryHeaders = null,
            HttpQuerySetting setting = null)
        {
            var fullUri = uriCreator.Create(GetApiEndpoint(), uri);

            try
            {
                var result = await httpRequestExecutor.UploadFileAsync(fullUri, file, AddDefaultHeaders(queryHeaders), setting ?? DefaultHttpQuerySetting()).ConfigureAwait(false);

                return(result);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 7
0
        protected async Task <byte[]> PostFileAsync <TRequest>(
            string uri,
            TRequest data,
            IReadOnlyCollection <KeyValuePair <string, string> > queryHeaders = null,
            HttpQuerySetting setting = null) where TRequest : class
        {
            var fullUri = uriCreator.Create(GetApiEndpoint(), uri);

            try
            {
                var result = await httpRequestExecutor.SendFileAsync(fullUri, HttpMethod.Post, data, AddDefaultHeaders(queryHeaders), setting ?? DefaultHttpQuerySetting()).ConfigureAwait(false);

                return(result);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 8
0
        protected async Task <byte[]> GetFileAsync(
            string uri,
            object queryParams = null,
            IReadOnlyCollection <KeyValuePair <string, string> > queryHeaders = null,
            HttpQuerySetting setting = null)
        {
            var fullUri = uriCreator.Create(GetApiEndpoint(), uri, queryParams);

            try
            {
                var result = await httpRequestExecutor.SendFileAsync <object>(fullUri, HttpMethod.Get, null,
                                                                              AddDefaultHeaders(queryHeaders), setting ?? DefaultHttpQuerySetting()).ConfigureAwait(false);

                return(result);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 9
0
        protected async Task <TResponse> GetAsync <TResponse>(
            string uri,
            object queryParams = null,
            IReadOnlyCollection <KeyValuePair <string, string> > queryHeaders = null,
            HttpQuerySetting setting = null)
        {
            var fullUri = uriCreator.Create(GetApiEndpoint(), uri, queryParams);

            try
            {
                var response = await httpRequestExecutor.GetAsync(fullUri, AddDefaultHeaders(queryHeaders), setting ?? DefaultHttpQuerySetting()).ConfigureAwait(false);

                var result = responseParser.Parse <TResponse>(response);

                return(result);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
Ejemplo n.º 10
0
        protected async Task DeleteByRequestAsync <TRequest>(
            string uri,
            object queryParams,
            TRequest data,
            IReadOnlyCollection <KeyValuePair <string, string> > queryHeaders = null,
            HttpQuerySetting setting = null)
            where TRequest : class
        {
            var fullUri = uriCreator.Create(GetApiEndpoint(), uri, queryParams);

            try
            {
                await httpRequestExecutor
                .DeleteAsync(fullUri, data, AddDefaultHeaders(queryHeaders), setting ?? DefaultHttpQuerySetting())
                .ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public async Task <byte[]> SendFileAsync <TRequest>(
            string uri,
            HttpMethod httpMethod,
            TRequest data = null,
            IReadOnlyCollection <KeyValuePair <string, string> > headers = null,
            HttpQuerySetting setting = null) where TRequest : class
        {
            CheckDisposed();

            if (setting == null)
            {
                setting = new HttpQuerySetting();
            }

            using var cts            = new CancellationTokenSource(setting.Timeout);
            using var requestMessage = new HttpRequestMessage(httpMethod, uri)
                  {
                      Version = HttpVersion.Version11
                  };
            AddHeaders(requestMessage, headers);

            if (null != data)
            {
                requestMessage.Content = CreateJsonContent(data);
            }

            using var response = await httpClient.SendAsync(requestMessage, cts.Token).ConfigureAwait(false);

            response.EnsureSuccessStatusCode();
            using var result = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);

            var stream = new MemoryStream();
            await result.CopyToAsync(stream, cts.Token).ConfigureAwait(false);

            stream.Position = 0;

            var httpContentHeaders = response.Content.Headers;

            return(stream.ToArray());
        }