/
ConfigurationCredentialsService.cs
118 lines (96 loc) · 3.81 KB
/
ConfigurationCredentialsService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
namespace MangaDexSharp;
/// <summary>
/// Represents a provider that fetches the <see cref="ICredentialsService"/> from the configuration
/// </summary>
public class ConfigurationCredentialsService : ICredentialsService
{
private readonly IConfiguration _config;
/// <summary>
/// Where to fetch the API token from in the config file
/// </summary>
public static string TokenPath { get; set; } = "Mangadex:Token";
/// <summary>
/// Where to fetch the API url from in the config file
/// </summary>
public static string ApiPath { get; set; } = "Mangadex:ApiUrl";
/// <summary>
/// Where to fetch the Auth URL service from in the config file
/// </summary>
public static string AuthPath { get; set; } = "Mangadex:AuthUrl";
/// <summary>
/// Where to fetch the client ID from in the config file
/// </summary>
public static string ClientIdPath { get; set; } = "Mangadex:ClientId";
/// <summary>
/// Where to fetch the client secret from in the config file
/// </summary>
public static string ClientSecretPath { get; set; } = "Mangadex:ClientSecret";
/// <summary>
/// Where to fetch the username (for the password grant OAuth2 requests) from in the config file
/// </summary>
public static string UsernamePath { get; set; } = "Mangadex:Username";
/// <summary>
/// Where to fetch the password (for the password grant OAuth2 requests) from in the config file
/// </summary>
public static string PasswordPath { get; set; } = "Mangadex:Password";
/// <summary>
/// Where to fetch the User-Agent header from in the config file
/// </summary>
public static string UserAgentPath { get; set; } = "Mangadex:UserAgent";
/// <summary>
/// Where to fetch the ThrowOnError flag from in the config file
/// </summary>
public static string ErrorThrownPath { get; set; } = "Mangadex:ThrowOnError";
/// <summary>
/// The authentication token from the configuration file
/// </summary>
public string? Token => _config[TokenPath];
/// <summary>
/// The API url from the configuration file
/// </summary>
public string ApiUrl => _config[ApiPath] ?? API_ROOT;
/// <summary>
/// The Auth URL service for MangaDex
/// </summary>
public string AuthUrl => _config[AuthPath] ?? AUTH_URL;
/// <summary>
/// The User-Agent header to send with requests
/// </summary>
public string UserAgent => _config[UserAgentPath] ?? API_USER_AGENT;
/// <summary>
/// The client ID for the authorization endpoint
/// </summary>
public string? ClientId => _config[ClientIdPath];
/// <summary>
/// The client secret for the authorization endpoint
/// </summary>
public string? ClientSecret => _config[ClientSecretPath];
/// <summary>
/// The username for password grant requests
/// </summary>
public string? Username => _config[UsernamePath];
/// <summary>
/// The password for password grant requests
/// </summary>
public string? Password => _config[PasswordPath];
/// <summary>
/// Whether or not to throw an exception if the API returns an error
/// </summary>
public bool ThrowOnError => _config[ErrorThrownPath] == "true";
/// <summary>
/// Represents a provider that fetches the <see cref="ICredentialsService"/> from the configuration
/// </summary>
/// <param name="config">The <see cref="IConfiguration"/> object to fetch the variables from</param>
public ConfigurationCredentialsService(IConfiguration config)
{
_config = config;
}
/// <summary>
/// Fetches the user's authentication token from the config file
/// </summary>
/// <returns>The user's authentication token</returns>
public Task<string?> GetToken()
{
return Task.FromResult(Token);
}
}