Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>TaleLearnCode.HttpRequestData.Extensions</AssemblyName>
<RootNamespace>TaleLearnCode</RootNamespace>
<Version>0.0.0-pre</Version>
<Version>0.0.1-pre</Version>
<Authors>TaleLearnCode</Authors>
<Company>Green Events &amp; Technology, LLC.</Company>
<Product>HttpRequestData Extensions</Product>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ namespace TaleLearnCode
public static class HttpRequestDataExtensions
{

/// <summary>
/// Creates a response with the provided body text.
/// </summary>
/// <param name="httpRequestData">The <see cref="HttpRequestData"/> for this response.</param>
/// <param name="httpStatusCode">The HTTP status code to return in the response.</param>
/// <param name="bodyText">The text of the response body.</param>
/// <returns>A <see cref="HttpResponseData"/> representing the response with the <paramref name="bodyText"/> in the body.</returns>
public static HttpResponseData CreateResponse(this HttpRequestData httpRequestData, HttpStatusCode httpStatusCode, string bodyText)
{
HttpResponseData response = httpRequestData.CreateResponse(httpStatusCode);
response.WriteString(bodyText);
return response;
}

/// <summary>
/// Creates a response with a body for the provided <see cref="HttpRequestData"/>.
/// </summary>
Expand Down Expand Up @@ -79,8 +93,7 @@ public static async Task<HttpResponseData> CreateResponseAsync(
/// </summary>
/// <param name="httpRequestData">The <see cref="HttpRequestData"/> for this response.</param>
/// <returns>A <see cref="HttpResponseData"/> with a status of Internal Server Error (500).</returns>
public static HttpResponseData CreateErrorResponse(
this HttpRequestData httpRequestData)
public static HttpResponseData CreateErrorResponse(this HttpRequestData httpRequestData)
{
return httpRequestData.CreateResponse(HttpStatusCode.InternalServerError);
}
Expand All @@ -91,25 +104,45 @@ public static HttpResponseData CreateErrorResponse(
/// <param name="httpRequestData">The <see cref="HttpRequestData"/> for this response.</param>
/// <param name="exception">The <see cref="Exception"/> causing the Internal Server Error to be returned.</param>
/// <returns>A <see cref="HttpResponseData"/> with a status of Internal Server Error (500) and a response body with the message from <paramref name="exception"/>.</returns>
public static HttpResponseData CreateErrorResponse(
this HttpRequestData httpRequestData,
Exception exception)
public static HttpResponseData CreateErrorResponse(this HttpRequestData httpRequestData, Exception exception)
{
if (exception == default) throw new ArgumentNullException(nameof(exception));
HttpResponseData response = httpRequestData.CreateResponse(HttpStatusCode.InternalServerError);
response.WriteString(exception.Message);
return response;
}

/// <summary>
/// Creates a bad request response.
/// </summary>
/// <param name="httpRequestData">The <see cref="HttpRequestData"/> for this response.</param>
/// <returns>A <see cref="HttpResponseData"/> with a status of Bad Request (400).</returns>
public static HttpResponseData CreateBadRequestResponse(this HttpRequestData httpRequestData)
{
return httpRequestData.CreateResponse(HttpStatusCode.BadRequest);
}

/// <summary>
/// Creates a bad request response with the exception message included.
/// </summary>
/// <param name="httpRequestData">The <see cref="HttpRequestData"/> for this response.</param>
/// <param name="exception">The <see cref="Exception"/> causing the Internal Server Error to be returned.</param>
/// <returns>A <see cref="HttpResponseData"/> with a status of Bad Request (400) and a response body with the message from <paramref name="exception"/>.</returns>
public static HttpResponseData CreateBadRequestResponse(this HttpRequestData httpRequestData, Exception exception)
{
HttpResponseData response = httpRequestData.CreateResponse(HttpStatusCode.BadRequest);
response.WriteString(exception.Message);
return response;
}

/// <summary>
/// Get a request object from the provide <see cref="HttpRequestData"/>.
/// </summary>
/// <typeparam name="T">The type of the request object to be returned.</typeparam>
/// <param name="httpRequestData">The <see cref="HttpRequestData"/> to be interrogated for the request object.</param>
/// <returns>A <typeparamref name="T"/> representing the request object from the request.</returns>
/// <exception cref="HttpRequestDataException">Thrown if there is an error reading the request object from the request.</exception>
public static Task<T> GetRequestParametersAsync<T>(
this HttpRequestData httpRequestData) where T : new()
public static Task<T> GetRequestParametersAsync<T>(this HttpRequestData httpRequestData) where T : new()
{
return GetRequestParametersAsync<T>(httpRequestData, new Dictionary<string, string>(), new JsonSerializerOptions());
}
Expand All @@ -122,9 +155,7 @@ public static Task<T> GetRequestParametersAsync<T>(
/// <param name="routeValues">Any route values supplied to the Azure Function.</param>
/// <returns>A <typeparamref name="T"/> representing the request object from the request.</returns>
/// <exception cref="HttpRequestDataException">Thrown if there is an error reading the request object from the request.</exception>
public static Task<T> GetRequestParametersAsync<T>(
this HttpRequestData httpRequestData,
Dictionary<string, string> routeValues) where T : new()
public static Task<T> GetRequestParametersAsync<T>(this HttpRequestData httpRequestData, Dictionary<string, string> routeValues) where T : new()
{
return GetRequestParametersAsync<T>(httpRequestData, routeValues, new JsonSerializerOptions());
}
Expand Down