tgstation-server 6.19.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
28 protected ILogger<AuthorityBase> Logger { get; }
29
37 => new(
38 new ErrorMessageResponse(errorCode),
39 HttpFailureResponse.BadRequest);
40
47 => new(
49 HttpFailureResponse.Unauthorized);
50
57 => new(
58 new ErrorMessageResponse(ErrorCode.ResourceNotPresent),
60
67 => new(
69 HttpFailureResponse.Forbidden);
70
77 => new(
78 new ErrorMessageResponse(ErrorCode.ResourceNeverPresent),
79 HttpFailureResponse.NotFound);
80
88 protected static AuthorityResponse<TResult> Conflict<TResult>(ErrorCode errorCode, string? additionalData = null)
89 => new(
90 new ErrorMessageResponse(errorCode)
91 {
92 AdditionalData = additionalData,
93 },
94 HttpFailureResponse.Conflict);
95
102 protected static FlagRightsConditional<TRights> Flag<TRights>(TRights flag)
103 where TRights : Enum
104 => new(flag);
105
114 where TRights : Enum
115 => new(lhs, rhs);
116
125 where TRights : Enum
126 => new(lhs, rhs);
127
133 protected AuthorityBase(
134 IDatabaseContext databaseContext,
135 ILogger<AuthorityBase> logger)
136 {
137 DatabaseContext = databaseContext ?? throw new ArgumentNullException(nameof(databaseContext));
138 Logger = logger ?? throw new ArgumentNullException(nameof(logger));
139 }
140
147 protected AuthorityResponse<TResult> RateLimit<TResult>(RateLimitExceededException rateLimitException)
148 {
149 Logger.LogWarning(rateLimitException, "Exceeded GitHub rate limit!");
150 var secondsString = Math.Ceiling(rateLimitException.GetRetryAfterTimeSpan().TotalSeconds).ToString(CultureInfo.InvariantCulture);
151 return new(
152 new ErrorMessageResponse(ErrorCode.GitHubApiRateLimit)
153 {
154 AdditionalData = $"Retry-After: {secondsString}s",
155 },
156 HttpFailureResponse.RateLimited);
157 }
158 }
159}
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 OrRightsConditional< TRights > Or< TRights >(RightsConditional< TRights > lhs, RightsConditional< TRights > rhs)
Helper to quickly construct an OrRightsConditional<TRights>.
static AuthorityResponse< TResult > Conflict< TResult >(ErrorCode errorCode, string? additionalData=null)
Generates a HttpFailureResponse.Conflict type AuthorityResponse<TResult>.
static AuthorityResponse< TResult > NotFound< TResult >()
Generates a HttpFailureResponse.NotFound type AuthorityResponse<TResult>.
static FlagRightsConditional< TRights > Flag< TRights >(TRights flag)
Helper to quickly construct a FlagRightsConditional<TRights>.
static AndRightsConditional< TRights > And< TRights >(RightsConditional< TRights > lhs, RightsConditional< TRights > rhs)
Helper to quickly construct an AndRightsConditional<TRights>.
static AuthorityResponse< TResult > Unauthorized< TResult >()
Generates a HttpFailureResponse.Unauthorized type AuthorityResponse<TResult>.
AuthorityBase(IDatabaseContext databaseContext, ILogger< AuthorityBase > logger)
Initializes a new instance of the AuthorityBase class.
AuthorityResponse< TResult > RateLimit< TResult >(RateLimitExceededException rateLimitException)
Generates a HttpFailureResponse.RateLimited type AuthorityResponse.
ILogger< AuthorityBase > Logger
Gets the ILogger for the AuthorityBase.
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
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.