public
Last active

  • Download Gist
periodic.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
 
 
/*
Copyright (c) Microsoft
All rights reserved.
Licensed under the Microsoft Limited Public License (the “License”); you may not
use this file except in compliance with the License.
You may obtain a copy of the License at http://msdn.microsoft.com/en-us/cc300389.
 
 
 
 
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS
OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
win
See the Microsoft Limited Public License (Ms-LPL) for specific language governing
permissions and limitations under the License.
*/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
 
namespace TileServiceRole.Controllers
{
public class TilesController : ApiController
{
// /api/tiles/<tilename>
public HttpResponseMessage Get(String id)
{
// set cloud storage credentials
var client = new Microsoft.WindowsAzure.StorageClient.CloudBlobClient(
"http://AZURE_ACCOUNT_NAME.blob.core.windows.net",
new Microsoft.WindowsAzure.StorageCredentialsAccountAndKey(
"AZURE_ACCOUNT_NAME",
"AZURE_ACCOUNT_KEY"
)
);
 
// create HTTP response
var response = new HttpResponseMessage();
try
{
// get XML template from storage
var xml = client.GetBlobReference(id.Replace("_", "/")).DownloadText();
 
// format response
response.StatusCode = System.Net.HttpStatusCode.OK;
response.Content = new StringContent(xml);
response.Content.Headers.ContentType =
new System.Net.Http.Headers.MediaTypeHeaderValue("text/xml");
 
// set expires header to invalidate tile when content is obsolete
response.Content.Headers.Add("X-WNS-Expires", GetExpiryTime().ToString("R"));
}
catch (Exception e)
{
// send a 400 if there's a problem
response.StatusCode = System.Net.HttpStatusCode.BadRequest;
response.Content = new StringContent(e.Message + "\r\n" + e.StackTrace);
response.Content.Headers.ContentType =
new System.Net.Http.Headers.MediaTypeHeaderValue("text/plain");
}
 
// return response
return response;
}
 
private DateTime GetExpiryTime()
{
Int32 TimeZoneOffset = -4; // EDT offset from UTC
 
// get representation of local time for restaurant
var requestLocalTime = DateTime.UtcNow.AddHours(TimeZoneOffset);
 
// if request is hitting before 9 a.m., information is stale
if (requestLocalTime.Hour <= 8)
return DateTime.UtcNow.AddDays(-1);
 
// else, set tile to expire at midnight local time
else
{
var minutesUntilExpiry = (24 - requestLocalTime.Hour) * 60 - requestLocalTime.Minute;
return DateTime.UtcNow.AddMinutes(minutesUntilExpiry);
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.