Skip to content

Instantly share code, notes, and snippets.

@JeffJacobson
Last active December 10, 2015 06:08
Show Gist options
  • Save JeffJacobson/4392702 to your computer and use it in GitHub Desktop.
Save JeffJacobson/4392702 to your computer and use it in GitHub Desktop.
Demonstrates how to detect a WSDOT Traveler API REST Request URL and append an access code from web.config. Designed for use with a Proxy page, as described at http://help.arcgis.com/en/webapi/javascript/arcgis/help/jshelp_start.htm#jshelp/ags_proxy.htm.
/// <summary>
/// Appends an AccessCode parameter to the input URI if it is a WSDOT Traveler Info REST API URI and does not already have an AccessCode parameter.
/// </summary>
/// <param name="uri">The URI passed to the proxy. Note that this string will be modified if an access code is determined to be necessary.</param>
/// <example>
/// <code><![CDATA[public void ProcessRequest (HttpContext context) {
/// HttpResponse response = context.Response;
/// // Get the URL requested by the client (take the entire querystring at once to handle the case of the URL itself containing querystring parameters)
/// string uri = context.Request.Url.Query.Substring(1);
/// AddTravelerApiAccessCodeIfNecessary(ref uri);
/// // Do the rest of your stuff...
/// }
/// ]]></code>
/// </example>
private static void AddTravelerApiAccessCodeIfNecessary(ref string uri)
{
const string urlPattern = @"(?i)wsdot\.wa\.gov\/Traffic\/api\/(\w+)\/\1REST\.svc";
const string accessCodePattern = @"AccessCode=([^\s&])+";
if (Regex.IsMatch(uri, urlPattern)) // If this is a traveler API URL...
{
if (!Regex.IsMatch(uri, accessCodePattern, RegexOptions.None)) // If the URL does not already have an AccessCode parameter...
{
string code = ConfigurationManager.AppSettings["wsdotTrafficApiAccessCode"];
char separator = uri.Contains("?") ? '&' : '?';
uri = string.Format("{0}{1}AccessCode={2}", uri, separator, code);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment