Created
June 7, 2018 11:05
-
-
Save davidvesely/eb5db05c12ebcda4035e8c3f5fd806d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usage: | |
var queryParams = new QueryParameters(); | |
queryParams.Add("public_id", messageId); | |
queryParams.Add("username", this.operatorConfig.Username); | |
queryParams.Add("checksum", this.GetChecksum(messageId)); | |
var builder = new UriBuilder(this.serviceUri) | |
{ | |
Query = queryParams.ToString() | |
}; | |
public class QueryParameters | |
{ | |
private readonly Dictionary<string, string> @params; | |
public QueryParameters() | |
{ | |
this.@params = new Dictionary<string, string>(); | |
} | |
public QueryParameters(string queryString) | |
{ | |
var queryStringCollection = HttpUtility.ParseQueryString(queryString); | |
this.@params = queryStringCollection.Keys | |
.Cast<string>() | |
.ToDictionary(k => k, v => queryStringCollection[v]); | |
} | |
public void Add(string key, string val) | |
{ | |
if (this.@params.ContainsKey(key)) | |
{ | |
throw new InvalidOperationException(string.Format("The key {0} already exists.", key)); | |
} | |
this.@params.Add(key, val); | |
} | |
public void Add<T>(string key, T val) | |
{ | |
this.Add(key, val.ToString()); | |
} | |
public string this[string key] | |
{ | |
get | |
{ | |
if (this.@params.ContainsKey(key)) | |
{ | |
return this.@params[key]; | |
} | |
else | |
{ | |
return null; | |
} | |
} | |
set | |
{ | |
this.Add(key, value); | |
} | |
} | |
public override string ToString() | |
{ | |
var sb = new StringBuilder(); | |
foreach (var kvp in this.@params) | |
{ | |
if (sb.Length > 0) | |
{ | |
sb.Append("&"); | |
} | |
sb.AppendFormat( | |
"{0}={1}", | |
HttpUtility.UrlEncode(kvp.Key), | |
HttpUtility.UrlEncode(kvp.Value)); | |
} | |
return sb.ToString(); | |
} | |
public bool Contains(string key) | |
{ | |
return this.@params.ContainsKey(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment