GetResourceUri() 공개 메소드

public GetResourceUri ( string baseUrl ) : Uri
baseUrl string
리턴 System.Uri
예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="restRequest"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task <HttpResponseMessage> GetHttpResponseMessage <T>(RestRequest restRequest, CancellationToken cancellationToken = default(CancellationToken))
        {
            //RWM If we've specified a DateFormat for the Client, but not not the Request, pass it down.
            if (!string.IsNullOrWhiteSpace(DateFormat) && string.IsNullOrWhiteSpace(restRequest.DateFormat))
            {
                restRequest.DateFormat = DateFormat;
            }

            //RWM: If we've specified JsonSerializerSettings for the Client, but not not the Request, pass it down.
            if (JsonSerializerSettings != null && restRequest.JsonSerializerSettings == null)
            {
                restRequest.JsonSerializerSettings = JsonSerializerSettings;
            }

            //RWM: We've moved this call to inside the HttpHandler setter... let's see if that solves our Mono problems.
            //ConfigureHandler(HttpHandler);
            _client = new HttpClient(HttpHandler);

            if (string.IsNullOrWhiteSpace(UserAgent))
            {
                SetUserAgent <T>();
            }
            _client.DefaultRequestHeaders.Add("user-agent", UserAgent);

            var message = new HttpRequestMessage(restRequest.Method, restRequest.GetResourceUri(BaseUrl));

            //RWM: Add the global headers for all requests.
            foreach (var header in Headers)
            {
                message.Headers.Add(header.Key, header.Value);
            }

            //RWM: Add request-specific headers.
            foreach (var header in restRequest.Headers)
            {
                message.Headers.Add(header.Key, header.Value.ToString());
            }

            //RWM: Not sure if this is sufficient, or if HEAD supports a body, will need to check into the RFC.
            if (restRequest.Method != HttpMethod.Get && restRequest.Method != HttpMethod.Head && restRequest.Method != HttpMethod.Trace)
            {
                //RWM: This feels hacky. May need some tweaking.
                if (restRequest.ContentType == ContentTypes.ByteArray)
                {
                    //RWM: A fix for an issue uncovered by @scottisafool.
                    if (restRequest.Parameters.Count > 0)
                    {
                        message.Content = new ByteArrayContent(restRequest.Parameters[0].GetEncodedValue() as byte[]);
                    }
                }
                else
                {
                    var contentString = new StringContent(restRequest.GetRequestBody(), Encoding.UTF8, restRequest.GetContentType());
                    message.Content = contentString;
                }
            }

            return(await _client.SendAsync(message, cancellationToken).ConfigureAwait(false));
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="restRequest"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        private async Task <HttpResponseMessage> GetHttpResponseMessage <T>([NotNull] RestRequest restRequest, CancellationToken cancellationToken = default(CancellationToken))
        {
            //RWM If we've specified a DateFormat for the Client, but not not the Request, pass it down.
            if (!string.IsNullOrWhiteSpace(DateFormat) && string.IsNullOrWhiteSpace(restRequest.DateFormat))
            {
                restRequest.DateFormat = DateFormat;
            }

            //RWM: If we've specified JsonSerializerSettings for the Client, but not not the Request, pass it down.
            if (JsonSerializerSettings != null && restRequest.JsonSerializerSettings == null)
            {
                restRequest.JsonSerializerSettings = JsonSerializerSettings;
            }

            if (string.IsNullOrWhiteSpace(UserAgent))
            {
                SetUserAgent <T>();
            }

            //RWM: We likely only need to set this once.
            if (!_client.DefaultRequestHeaders.UserAgent.Any())
            {
                _client.DefaultRequestHeaders.Add("user-agent", UserAgent);
            }

            var message = new HttpRequestMessage(restRequest.Method, restRequest.GetResourceUri(BaseUrl));

            //RWM: Add the global headers for all requests.
            foreach (var header in Headers)
            {
                message.Headers.TryAddWithoutValidation(header.Key, header.Value);
                //message.Headers.Add(header.Key, header.Value);
            }

            //RWM: Add request-specific headers.
            foreach (var header in restRequest.Headers)
            {
                message.Headers.TryAddWithoutValidation(header.Key, header.Value.ToString());
                //message.Headers.Add(header.Key, header.Value.ToString());
            }

            //RWM: Not sure if this is sufficient, or if HEAD supports a body, will need to check into the RFC.
            if (restRequest.Method != HttpMethod.Get && restRequest.Method != HttpMethod.Head && restRequest.Method != HttpMethod.Trace)
            {
                //RWM: This feels hacky. May need some tweaking.
                if (restRequest.ContentType == ContentTypes.ByteArray)
                {
                    //RWM: A fix for an issue uncovered by @scottisafool.
                    if (restRequest.Parameters.Count > 0)
                    {
                        message.Content = new ByteArrayContent(restRequest.Parameters[0].GetEncodedValue() as byte[]);
                    }
                }
                //RWM: This may not be the best place to keep this... might be better to refactor RestRequest.GetRequestBody to return a HttpContent object instead.
                else if (restRequest.ContentType == ContentTypes.MultiPartFormData)
                {
                    var content = new MultipartFormDataContent();

                    foreach (var p in restRequest.Parameters)
                    {
                        if (p is FileParameter)
                        {
                            var fileParameter = p as FileParameter;
                            if (string.IsNullOrEmpty(fileParameter.Filename))
                            {
                                content.Add(new StreamContent(fileParameter.Value as Stream), fileParameter.Key);
                            }
                            else
                            {
                                content.Add(new StreamContent(fileParameter.Value as Stream), fileParameter.Key, fileParameter.Filename);
                            }
                        }
                        else if (p.Encoding == ParameterEncoding.ByteArray)
                        {
                            content.Add(new ByteArrayContent(p.GetEncodedValue() as byte[]));
                        }
                        else
                        {
                            content.Add(new StringContent(p.GetEncodedValue().ToString()), p.Key);
                        }
                    }

                    message.Content = content;
                }
                else
                {
                    var contentString = new StringContent(restRequest.GetRequestBody(), Encoding.UTF8, restRequest.GetContentType());
                    message.Content = contentString;
                }
            }

            return(await _client.SendAsync(message, cancellationToken).ConfigureAwait(false));
        }