PortableRest.RestRequest.GetResourceUri C# (CSharp) Method

GetResourceUri() public method

public GetResourceUri ( string baseUrl ) : Uri
baseUrl string
return System.Uri
        public Uri GetResourceUri(string baseUrl)
        {
            foreach (var segment in UrlSegments.Where(c => !c.IsQueryString))
            {
                Resource = Resource.Replace("{" + segment.Key + "}", Uri.EscapeUriString(segment.Value));
            }

            if (UrlSegments.Any(c => c.IsQueryString))
            {
                var queryString = UrlSegments.Where(c => c.IsQueryString)
                    .Aggregate(new StringBuilder(),
                        (current, next) =>
                            current.Append(string.Format("&{0}={1}", Uri.EscapeUriString(next.Key), Uri.EscapeDataString(next.Value))))
                    .ToString();

                Resource = string.Format(Resource.Contains("?") ? "{0}{1}" : "{0}?{1}", Resource, queryString);
            }

            Resource = CombineUriParts(baseUrl, Resource);

            return new Uri(Resource, UriKind.RelativeOrAbsolute);
        }

Usage Example

Ejemplo n.º 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));
        }
All Usage Examples Of PortableRest.RestRequest::GetResourceUri