tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
AuthorityBase.cs
Go to the documentation of this file.
1using System;
2using System.Globalization;
3
4using Microsoft.Extensions.Logging;
5
6using Octokit;
7
12
14{
18 abstract class AuthorityBase : IAuthority
19 {
24
29
33 protected ILogger<AuthorityBase> Logger { get; }
34
42 => new(
43 new ErrorMessageResponse(errorCode),
44 HttpFailureResponse.BadRequest);
45
52 => new(
54 HttpFailureResponse.Unauthorized);
55
62 => new(
63 new ErrorMessageResponse(ErrorCode.ResourceNotPresent),
65
72 => new(
74 HttpFailureResponse.Forbidden);
75
82 => new(
83 new ErrorMessageResponse(ErrorCode.ResourceNeverPresent),
84 HttpFailureResponse.NotFound);
85
93 => new(
94 new ErrorMessageResponse(errorCode),
95 HttpFailureResponse.Conflict);
96
103 protected AuthorityBase(
104 IAuthenticationContext authenticationContext,
105 IDatabaseContext databaseContext,
106 ILogger<AuthorityBase> logger)
107 {
108 AuthenticationContext = authenticationContext ?? throw new ArgumentNullException(nameof(authenticationContext));
109 DatabaseContext = databaseContext ?? throw new ArgumentNullException(nameof(databaseContext));
110 Logger = logger ?? throw new ArgumentNullException(nameof(logger));
111 }
112
119 protected AuthorityResponse<TResult> RateLimit<TResult>(RateLimitExceededException rateLimitException)
120 {
121 Logger.LogWarning(rateLimitException, "Exceeded GitHub rate limit!");
122 var secondsString = Math.Ceiling(rateLimitException.GetRetryAfterTimeSpan().TotalSeconds).ToString(CultureInfo.InvariantCulture);
123 return new(
124 new ErrorMessageResponse(ErrorCode.GitHubApiRateLimit)
125 {
126 AdditionalData = $"Retry-After: {secondsString}s",
127 },
128 HttpFailureResponse.RateLimited);
129 }
130 }
131}
Represents an error message returned by the server.
static AuthorityResponse< TResult > Forbid< TResult >()
Generates a HttpFailureResponse.Forbidden type AuthorityResponse<TResult>.
static AuthorityResponse< TResult > Gone< TResult >()
Generates a HttpFailureResponse.Forbidden type AuthorityResponse<TResult>.
static AuthorityResponse< TResult > NotFound< TResult >()
Generates a HttpFailureResponse.NotFound type AuthorityResponse<TResult>.
static AuthorityResponse< TResult > Unauthorized< TResult >()
Generates a HttpFailureResponse.Unauthorized type AuthorityResponse<TResult>.
AuthorityResponse< TResult > RateLimit< TResult >(RateLimitExceededException rateLimitException)
Generates a HttpFailureResponse.RateLimited type AuthorityResponse.
ILogger< AuthorityBase > Logger
Gets the ILogger for the AuthorityBase.
AuthorityBase(IAuthenticationContext authenticationContext, IDatabaseContext databaseContext, ILogger< AuthorityBase > logger)
Initializes a new instance of the AuthorityBase class.
static AuthorityResponse< TResult > Conflict< TResult >(ErrorCode errorCode)
Generates a HttpFailureResponse.Conflict type AuthorityResponse<TResult>.
static AuthorityResponse< TResult > BadRequest< TResult >(ErrorCode errorCode)
Generates a HttpFailureResponse.BadRequest type AuthorityResponse<TResult>.
Backend abstract implementation of IDatabaseContext.
Business logic for interating with the server.
Definition IAuthority.cs:9
For creating and accessing authentication contexts.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12
HttpFailureResponse
Indicates the type of HTTP status code an failing AuthorityResponse should generate.