Example #1
0
        /// <summary>
        /// Create new instance of <see cref="HttpRestUri"/>
        /// </summary>
        /// <param name="restUri">Rest uri.</param>
        public HttpRestUri(Uri restUri)
        {
            if (restUri == null)
            {
                throw new ArgumentNullException(nameof(restUri));
            }

            this.immutableBaseUri = restUri;
            this.queryParameter   = new HttpQueryParameter("");
            this.segments         = new List <string>();
        }
Example #2
0
        /// <summary>
        /// Add query or update existing values.
        /// </summary>
        /// <param name="query">Query.</param>
        public void AddQuery(string query)
        {
            HttpQueryParameter param = new HttpQueryParameter(query);

            foreach (KeyValuePair <string, object> pair in param)
            {
                // if query exist, update it.
                if (this.queryParameter.Contains(pair))
                {
                    this.queryParameter[pair.Key] = pair.Value;
                }
                else
                {
                    this.queryParameter.Add(pair.Key, pair.Value);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Create uri.
        /// </summary>
        /// <returns></returns>
        private Uri GetUri()
        {
            UriBuilder         uriBuilder = new UriBuilder(this.immutableBaseUri);
            HttpQueryParameter immutableQueryParameter = new HttpQueryParameter(uriBuilder.Query);

            if (this.queryParameter.Count > 0)
            {
                foreach (KeyValuePair <string, object> pair in this.queryParameter)
                {
                    if (immutableQueryParameter.ContainsKey(pair.Key))
                    {
                        // dont touch it, it should remain 'immutable'.
                        continue;
                    }

                    immutableQueryParameter.Add(
                        pair.Key,
                        pair.Value);
                }
            }

            uriBuilder.Query = immutableQueryParameter.ToQueryString();
            List <string> immutableSegments = new List <string>();

            if (this.immutableBaseUri.Segments.Length > 0)
            {
                foreach (string segment in this.immutableBaseUri.Segments)
                {
                    immutableSegments.Add(segment.TrimEnd('/'));
                }
            }

            if (this.segments.Count > 0)
            {
                immutableSegments.AddRange(this.segments);
            }

            uriBuilder.Path = string.Join("/", immutableSegments).TrimStart('/');

            return(uriBuilder.Uri);
        }