tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
RestAuthorityInvoker{TAuthority}.cs
Go to the documentation of this file.
1using System;
2using System.Net;
3using System.Threading.Tasks;
4
5using Microsoft.AspNetCore.Mvc;
6
9
11{
13 sealed class RestAuthorityInvoker<TAuthority> : AuthorityInvokerBase<TAuthority>, IRestAuthorityInvoker<TAuthority>
14 where TAuthority : IAuthority
15 {
25 static IActionResult CreateSuccessfulActionResult<TResult, TApiModel>(ApiController controller, Func<TResult, TApiModel> resultTransformer, AuthorityResponse<TResult> authorityResponse)
26 where TApiModel : notnull
27 {
28 if (authorityResponse.IsNoContent!.Value)
29 return controller.NoContent();
30
31 var successResponse = authorityResponse.SuccessResponse;
32 var result = resultTransformer(authorityResponse.Result!);
33 return successResponse switch
34 {
35 HttpSuccessResponse.Ok => controller.Json(result),
36 HttpSuccessResponse.Created => controller.Created(result),
37 HttpSuccessResponse.Accepted => controller.Accepted(result),
38 _ => throw new InvalidOperationException($"Invalid {nameof(HttpSuccessResponse)}: {successResponse}"),
39 };
40 }
41
48 static IActionResult? CreateErroredActionResult(ApiController controller, AuthorityResponse authorityResponse)
49 {
50 if (authorityResponse.Success)
51 return null;
52
53 var errorMessage = authorityResponse.ErrorMessage;
54 var failureResponse = authorityResponse.FailureResponse;
55 return failureResponse switch
56 {
57 HttpFailureResponse.BadRequest => controller.BadRequest(errorMessage),
58 HttpFailureResponse.Unauthorized => controller.Unauthorized(),
59 HttpFailureResponse.Forbidden => controller.Forbid(),
60 HttpFailureResponse.NotFound => controller.NotFound(errorMessage),
61 HttpFailureResponse.NotAcceptable => controller.StatusCode(HttpStatusCode.NotAcceptable, errorMessage),
62 HttpFailureResponse.Conflict => controller.Conflict(errorMessage),
63 HttpFailureResponse.Gone => controller.StatusCode(HttpStatusCode.Gone, errorMessage),
64 HttpFailureResponse.UnprocessableEntity => controller.UnprocessableEntity(errorMessage),
65 HttpFailureResponse.FailedDependency => controller.StatusCode(HttpStatusCode.FailedDependency, errorMessage),
66 HttpFailureResponse.RateLimited => controller.StatusCode(HttpStatusCode.TooManyRequests, errorMessage),
67 HttpFailureResponse.NotImplemented => controller.StatusCode(HttpStatusCode.NotImplemented, errorMessage),
68 HttpFailureResponse.ServiceUnavailable => controller.StatusCode(HttpStatusCode.ServiceUnavailable, errorMessage),
69 _ => throw new InvalidOperationException($"Invalid {nameof(HttpFailureResponse)}: {failureResponse}"),
70 };
71 }
72
77 public RestAuthorityInvoker(TAuthority authority)
78 : base(authority)
79 {
80 }
81
83 async ValueTask<IActionResult> IRestAuthorityInvoker<TAuthority>.Invoke(ApiController controller, Func<TAuthority, ValueTask<AuthorityResponse>> authorityInvoker)
84 {
85 ArgumentNullException.ThrowIfNull(controller);
86 ArgumentNullException.ThrowIfNull(authorityInvoker);
87
88 var authorityResponse = await authorityInvoker(Authority);
89 return CreateErroredActionResult(controller, authorityResponse) ?? controller.NoContent();
90 }
91
93 async ValueTask<IActionResult> IRestAuthorityInvoker<TAuthority>.Invoke<TResult, TApiModel>(ApiController controller, Func<TAuthority, ValueTask<AuthorityResponse<TResult>>> authorityInvoker)
94 {
95 ArgumentNullException.ThrowIfNull(controller);
96 ArgumentNullException.ThrowIfNull(authorityInvoker);
97
98 var authorityResponse = await authorityInvoker(Authority);
99 var erroredResult = CreateErroredActionResult(controller, authorityResponse);
100 if (erroredResult != null)
101 return erroredResult;
102
103 return CreateSuccessfulActionResult(controller, result => result, authorityResponse);
104 }
105
107 async ValueTask<IActionResult> IRestAuthorityInvoker<TAuthority>.InvokeTransformable<TResult, TApiModel>(ApiController controller, Func<TAuthority, ValueTask<AuthorityResponse<TResult>>> authorityInvoker)
108 {
109 ArgumentNullException.ThrowIfNull(controller);
110 ArgumentNullException.ThrowIfNull(authorityInvoker);
111
112 var authorityResponse = await authorityInvoker(Authority);
113 var erroredResult = CreateErroredActionResult(controller, authorityResponse);
114 if (erroredResult != null)
115 return erroredResult;
116
117 return CreateSuccessfulActionResult(controller, result => result.ToApi(), authorityResponse);
118 }
119 }
120}
virtual bool Success
Checks if the AuthorityResponse was successful.
ErrorMessageResponse? ErrorMessage
Gets the associated ErrorMessageResponse. Must only be used if Success is false.
HttpFailureResponse? FailureResponse
The HttpFailureResponse.
static ? IActionResult CreateErroredActionResult(ApiController controller, AuthorityResponse authorityResponse)
Create an IActionResult for a given authorityResponse if it is erroring.
static IActionResult CreateSuccessfulActionResult< TResult, TApiModel >(ApiController controller, Func< TResult, TApiModel > resultTransformer, AuthorityResponse< TResult > authorityResponse)
Create an IActionResult for a given successfulyauthorityResponse .
RestAuthorityInvoker(TAuthority authority)
Initializes a new instance of the RestAuthorityInvoker<TAuthority> class.
Base Controller for API functions.
new NotFoundObjectResult NotFound()
Generic 404 response.
StatusCodeResult StatusCode(HttpStatusCode statusCode)
Strongly type calls to ControllerBase.StatusCode(int).
Business logic for interating with the server.
Definition IAuthority.cs:9
Invokes TAuthority methods and generates IActionResult responses.
ValueTask< IActionResult > Invoke(ApiController controller, Func< TAuthority, ValueTask< AuthorityResponse > > authorityInvoker)
Invoke a TAuthority method with no success result.