Skip to content

Instantly share code, notes, and snippets.

@shawnoster
Created February 15, 2012 02:12
Show Gist options
  • Save shawnoster/1832552 to your computer and use it in GitHub Desktop.
Save shawnoster/1832552 to your computer and use it in GitHub Desktop.
Some basic REST end-point utilities
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