Aliencube.AzureFunctions.Extensions.Common
November 27, 2021 ยท View on GitHub
This extension provides constants and extension methods frequently used while developing Azure Functions apps.
Constants
The following constants represent the string respectively.
ContentTypes
It specifies the content type.
ContentTypes.PlainText:text/plainContentTypes.TextHtml:text/htmlContentTypes.ApplicationJson:application/jsonContentTypes.TextVndYaml:text/vnd.yaml
HttpVerbs
It specifies the HTTP method.
HttpVerbs.GET:GETHttpVerbs.POST:POSTHttpVerbs.PUT:PUTHttpVerbs.PATCH:PATCHHttpVerbs.DELETE:DELETE
Enums
SourceFrom
It specifies whether the HTTP request comes from.
Header: Request source comes from the request header.Query: Request source comes from the request querystring.Body: Request source comes from the request body.
Extension Methods
HttpRequestExtensions.To<T>(SourceFrom source)
It extracts relevant data from the given source of the HTTP request.
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, HttpVerbs.GET)] HttpRequest req)
{
var headers = await req.To<IHeaderDictionary>(SourceFrom.Header)
.ConfigureAwait(false);
var queries = await req.To<IQueryCollection>(SourceFrom.Query)
.ConfigureAwait(false);
var payload = await req.To<MyClass>(SourceFrom.Body)
.ConfigureAwait(false);
...
}
OpenApiFormatExtensions.GetContentType()
It returns the content type based on the OpenAPI document format.
var format = OpenApiFormat.Json;
var contentType = format.GetContentType();
PayloadExtensions.ToJson<T>()
It serialises the given payload to JSON string.
var payload = new MyClass() { Message = "hello world" };
var serialised = payload.ToJson();
PayloadExtensions.FromJson<T>()
It deserialises the JSON string value to given type.
var payload = "{ \"message\": \"hello world\" }"
var deserialised = payload.FromJson<MyClass>(payload);