Beispiel #1
0
        /// <summary>
        /// Async HTTP Request with validation
        /// </summary>
        /// <param name="method">HTTP Method</param>
        /// <param name="uri">Relative or explicit URI</param>
        /// <param name="data">HTTP Body as JSON</param>
        /// <param name="parameters">URL Request parameters</param>
        /// <param name="Headers">Request headers</param>
        /// <returns>ApiResponse containing HTTP request body as JSON</returns>
        public async Task <ApiResponse> PerformRequestAsync(ApiResourceVerb method, string uri, string data = "", string parameters = "", Headers headers = null)
        {
            var response = await SendRequestAsync(method, uri, data, parameters, headers);

            await ValidateResponse(response);

            return(new ApiResponse(await response.Content.ReadAsStringAsync(), response));
        }
Beispiel #2
0
        /// <summary>
        /// Basic async HTTP Request method. Does not validate response
        /// </summary>
        /// <param name="method">HTTP Method</param>
        /// <param name="uri">Relative or explicit URI</param>
        /// <param name="data">HTTP Body as JSON</param>
        /// <param name="parameters">URL Request parameters</param>
        /// <param name="headers">Request headers</param>
        /// <returns>Task with HTTP response</returns>
        public async Task <HttpResponseMessage> SendRequestAsync(ApiResourceVerb method, string uri, string data, string parameters, Headers headers)
        {
            headers = headers ?? new Headers();

            if (!string.IsNullOrWhiteSpace(parameters))
            {
                if (!parameters.StartsWith("?") && !uri.Contains('?'))
                {
                    parameters = parameters.Insert(0, "?");
                }

                else if (!parameters.StartsWith("?") && uri.Contains('?'))
                {
                    parameters = parameters.Insert(0, "&");
                }

                uri = string.Format("{0}{1}", uri, parameters);
            }

            HttpMethod httpMethod = HttpMethod.Get;

            switch (method)
            {
            case ApiResourceVerb.GET:
                httpMethod = HttpMethod.Get;
                break;

            case ApiResourceVerb.DELETE:
                httpMethod = HttpMethod.Delete;
                break;

            case ApiResourceVerb.PUT:
                httpMethod = HttpMethod.Put;
                break;

            case ApiResourceVerb.POST:
                httpMethod = HttpMethod.Post;
                break;
            }

            var request = new HttpRequestMessage(httpMethod, uri);

            if ((method == ApiResourceVerb.POST || method == ApiResourceVerb.PUT) && !string.IsNullOrEmpty(data))
            {
                request.Content = new StringContent(data, Encoding.UTF8, "application/json");
            }

            foreach (KeyValuePair <string, string> header in headers.Build())
            {
                request.Headers.Add(header.Key, header.Value);
            }

            return(await _httpClient.SendAsync(request));
        }
Beispiel #3
0
        /// <summary>
        /// Asynchronously gets metadata for given resource
        /// </summary>
        /// <param name="uri">Relative or explicit path to a resource/endpoint</param>
        /// <param name="verb">Metadata for given verb</param>
        /// <param name="Headers">Request headers</param>
        /// <returns>Task with metadata</returns>
        /// <exception cref="RequestException">HTTP status not successful</exception>
        public async Task <MetadataResponse> GetMetadataAsync(string uri, ApiResourceVerb verb, Headers headers = null)
        {
            var parameters = $"?uri={uri}&verb={verb}";

            parameters += "&$expand=ApplicableDomainValues,ExpandableFields,Description,TranslatedDescription,TranslatedName,MacroDefinitions,URIParameters";
            string      url      = "metadata";
            ApiResponse response = await _request.PerformRequestAsync(ApiResourceVerb.GET, url, parameters : parameters, headers : headers);

            MetadataWrapper metadataResponse = JsonConvert.DeserializeObject <MetadataWrapper>(response.Content);

            return(metadataResponse.Metadata);
        }