예제 #1
0
        /// <summary>
        /// Generic Method to perform an HTTP POST Request with return content
        /// </summary>
        /// <typeparam name="T">The Return Type Model definition</typeparam>
        /// <param name="Url">The API Url</param>
        /// <param name="content">The body content to post</param>
        public async Task <ApiHttpResponse <T> > PostDataAsync <T>(string Url, object content, string jwt = null)
        {
            // Get a new http client object from the factory
            var httpClient = _httpClientFactory.CreateClient();

            // Add Token to Header if present
            if (!string.IsNullOrWhiteSpace(jwt))
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwt);
            }

            // Make the Post request
            var postContent = _jsonService.ToJsonStringContent(content);
            var response    = await httpClient.PostAsync(new Uri(Url), postContent);

            string jsonString = await response.Content.ReadAsStringAsync();


            // Create the return object
            var apiResponse = new ApiHttpResponse <T>()
            {
                StatusCode = response.StatusCode, ReasonPhrase = response.ReasonPhrase, ApiData = default
            };

            if (response.IsSuccessStatusCode)
            {
                if (!string.IsNullOrWhiteSpace(jsonString))
                {
                    apiResponse.ApiData = _jsonService.FromJson <T>(jsonString);
                }
            }
            else
            {
                apiResponse.ErrorApiResponse = _jsonService.FromJson <ApiResponse>(jsonString);
            }

            return(apiResponse);
        }