Skip to content

Instantly share code, notes, and snippets.

@justinyoo
Last active June 12, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinyoo/e5684525dd451640acf26d7a60687b42 to your computer and use it in GitHub Desktop.
Save justinyoo/e5684525dd451640acf26d7a60687b42 to your computer and use it in GitHub Desktop.
Azure Functions with Swagger
public static class GetSwaggerHttpTrigger
{
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, string version, TraceWriter log)
{
var wwwroot = Environment.GetEnvironmentVariable("WEBROOT_PATH");
var filepath = $"{wwwroot}\\swagger-{version}.yaml";
if (!File.Exists(filepath))
{
return req.CreateResponse(HttpStatusCode.NotFound);
}
var settings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Formatting.Indented
};
var formatter = new JsonMediaTypeFormatter() { SerializerSettings = settings };
using (var reader = File.OpenText(filepath))
{
var yaml = await reader.ReadToEndAsync().ConfigureAwait(false);
var deserialiser = new DeserializerBuilder().Build();
var deserialised = deserialiser.Deserialize<dynamic>(yaml);
return req.CreateResponse(HttpStatusCode.OK, (object)deserialised, formatter);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment