-
-
Save justinyoo/e5684525dd451640acf26d7a60687b42 to your computer and use it in GitHub Desktop.
Azure Functions with Swagger
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
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