Created
February 15, 2012 02:12
-
-
Save shawnoster/1832552 to your computer and use it in GitHub Desktop.
Some basic REST end-point utilities
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
private static string UrlCombine(string baseUrl, string resource) | |
{ | |
if (baseUrl.Length == 0) | |
{ | |
return baseUrl; | |
} | |
if (resource.Length == 0) | |
{ | |
return resource; | |
} | |
baseUrl = baseUrl.TrimEnd(new char[] { '/', '\\' }); | |
resource = resource.TrimStart(new char[] { '/', '\\' }); | |
return String.Format("{0}/{1}", baseUrl, resource); | |
} | |
public string BuildResource(string resource, Dictionary<string, string> parameters) | |
{ | |
string url = UrlCombine(_client.BaseUrl, resource); | |
if (parameters != null) | |
{ | |
// add in the developer key, it won't hurt methods that don't require it and prevents | |
// having to scatter it across all calls that do. | |
parameters.Add("key", ConsumerKey); | |
StringBuilder apiParameters = new StringBuilder("?"); | |
foreach (KeyValuePair<string, string> pair in parameters) | |
{ | |
apiParameters.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0}={1}&", pair.Key, Uri.EscapeDataString(pair.Value)); | |
} | |
url += apiParameters.ToString(); | |
url = url.TrimEnd('&'); | |
} | |
return url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment