Skip to content

Commit

Permalink
moved CallType to AuthApiCall.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
vosmiic committed Mar 28, 2022
1 parent 7e2b6b6 commit 53faf69
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
20 changes: 14 additions & 6 deletions Api/AuthApiCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public AuthApiCall(ApiName provider, IHttpClientFactory httpClientFactory, IServ
/// <exception cref="NullReferenceException">Authentication details not found.</exception>
/// <exception cref="Exception">Non-200 response.</exception>
/// <exception cref="AuthenticationException">Could not authenticate with the API.</exception>
public async Task<HttpResponseMessage> AuthenticatedApiCall(ApiName provider, MalApiCalls.CallType callType, string url, FormUrlEncodedContent formUrlEncodedContent = null, StringContent stringContent = null) {
public async Task<HttpResponseMessage> AuthenticatedApiCall(ApiName provider, CallType callType, string url, FormUrlEncodedContent formUrlEncodedContent = null, StringContent stringContent = null) {
int attempts = 0;
UserApiAuth auth;
try {
Expand All @@ -60,19 +60,19 @@ public async Task<HttpResponseMessage> AuthenticatedApiCall(ApiName provider, Ma
HttpResponseMessage responseMessage = new HttpResponseMessage();
try {
switch (callType) {
case MalApiCalls.CallType.GET:
case CallType.GET:
responseMessage = await client.GetAsync(url);
break;
case MalApiCalls.CallType.POST:
case CallType.POST:
responseMessage = await client.PostAsync(url, formUrlEncodedContent != null ? formUrlEncodedContent : stringContent);
break;
case MalApiCalls.CallType.PATCH:
case CallType.PATCH:
responseMessage = await client.PatchAsync(url, formUrlEncodedContent != null ? formUrlEncodedContent : stringContent);
break;
case MalApiCalls.CallType.PUT:
case CallType.PUT:
responseMessage = await client.PutAsync(url, formUrlEncodedContent);
break;
case MalApiCalls.CallType.DELETE:
case CallType.DELETE:
responseMessage = await client.DeleteAsync(url);
break;
default:
Expand Down Expand Up @@ -110,5 +110,13 @@ public async Task<HttpResponseMessage> AuthenticatedApiCall(ApiName provider, Ma
_logger.LogError("Unable to authenticate the API call, re-authenticate the plugin");
return null;
}

public enum CallType {
GET,
POST,
PATCH,
PUT,
DELETE
}
}
}
12 changes: 6 additions & 6 deletions Api/Kitsu/KitsuApiCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public KitsuApiCalls(IHttpClientFactory httpClientFactory, ILoggerFactory logger

string builtUrl = url.Build();
_logger.LogInformation($"(Kitsu) Starting search for anime (GET {builtUrl})...");
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, MalApiCalls.CallType.GET, builtUrl);
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, AuthApiCall.CallType.GET, builtUrl);
if (apiCall != null) {
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
var animeList = JsonSerializer.Deserialize<KitsuSearch.KitsuSearchMedia>(await streamReader.ReadToEndAsync());
Expand Down Expand Up @@ -73,7 +73,7 @@ public KitsuApiCalls(IHttpClientFactory httpClientFactory, ILoggerFactory logger
};

_logger.LogInformation($"(Kitsu) Retrieving user information...");
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, MalApiCalls.CallType.GET, url.Build());
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, AuthApiCall.CallType.GET, url.Build());
if (apiCall != null) {
var xd = await apiCall.Content.ReadAsStringAsync();
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
Expand All @@ -94,7 +94,7 @@ public KitsuApiCalls(IHttpClientFactory httpClientFactory, ILoggerFactory logger
string builtUrl = url.Build();
_logger.LogInformation($"(Kitsu) Retrieving an anime from Kitsu (GET {builtUrl})...");
try {
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, MalApiCalls.CallType.GET, builtUrl);
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, AuthApiCall.CallType.GET, builtUrl);
if (apiCall != null) {
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
var anime = JsonSerializer.Deserialize<KitsuGet.KitsuGetAnime>(await streamReader.ReadToEndAsync());
Expand Down Expand Up @@ -122,7 +122,7 @@ public KitsuApiCalls(IHttpClientFactory httpClientFactory, ILoggerFactory logger

string builtUrl = url.Build();
try {
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, MalApiCalls.CallType.GET, builtUrl);
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, AuthApiCall.CallType.GET, builtUrl);
if (apiCall != null) {
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
var mediaRelationships = JsonSerializer.Deserialize<KitsuMediaRelationship.MediaRelationship>(await streamReader.ReadToEndAsync());
Expand Down Expand Up @@ -210,7 +210,7 @@ public async Task<bool> UpdateAnimeStatus(int animeId, int numberOfWatchedEpisod
};

var stringContent = new StringContent(JsonSerializer.Serialize(payload, jsonSerializerOptions), Encoding.UTF8, "application/vnd.api+json");
HttpResponseMessage? apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, libraryStatus != null ? MalApiCalls.CallType.PATCH : MalApiCalls.CallType.POST, url.Build(), stringContent: stringContent);
HttpResponseMessage? apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, libraryStatus != null ? AuthApiCall.CallType.PATCH : AuthApiCall.CallType.POST, url.Build(), stringContent: stringContent);

if (apiCall != null) {
return apiCall.IsSuccessStatusCode;
Expand All @@ -231,7 +231,7 @@ public async Task<bool> UpdateAnimeStatus(int animeId, int numberOfWatchedEpisod

_logger.LogInformation("(Kitsu) Fetching current user anime list status...");
try {
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, MalApiCalls.CallType.GET, url.Build());
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Kitsu, AuthApiCall.CallType.GET, url.Build());

if (apiCall != null) {
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
Expand Down
18 changes: 5 additions & 13 deletions Api/Mal/MalApiCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public async Task<User> GetUserInformation() {
UrlBuilder url = new UrlBuilder {
Base = $"{ApiUrl}/users/@me"
};
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, CallType.GET, url.Build());
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, AuthApiCall.CallType.GET, url.Build());
if (apiCall != null) {
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
string streamText = await streamReader.ReadToEndAsync();
Expand Down Expand Up @@ -85,7 +85,7 @@ public async Task<List<Anime>> SearchAnime(string? query, string[]? fields) {

string builtUrl = url.Build();
_logger.LogInformation($"Starting search for anime (GET {builtUrl})...");
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, CallType.GET, builtUrl);
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, AuthApiCall.CallType.GET, builtUrl);
if (apiCall != null) {
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
var animeList = JsonSerializer.Deserialize<SearchAnimeResponse>(await streamReader.ReadToEndAsync());
Expand Down Expand Up @@ -113,7 +113,7 @@ public async Task<Anime> GetAnime(int animeId, string[]? fields = null) {
string builtUrl = url.Build();
_logger.LogInformation($"Retrieving an anime from MAL (GET {builtUrl})...");
try {
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, CallType.GET, builtUrl);
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, AuthApiCall.CallType.GET, builtUrl);
if (apiCall != null) {
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
var options = new JsonSerializerOptions();
Expand Down Expand Up @@ -157,7 +157,7 @@ public async Task<List<UserAnimeListData>> GetUserAnimeList(Status? status = nul
UserAnimeList userAnimeList = new UserAnimeList { Data = new List<UserAnimeListData>() };
while (builtUrl != null) {
_logger.LogInformation($"Getting user anime list (GET {builtUrl})...");
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, CallType.GET, builtUrl);
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, AuthApiCall.CallType.GET, builtUrl);
if (apiCall != null) {
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
var options = new JsonSerializerOptions();
Expand Down Expand Up @@ -219,7 +219,7 @@ public async Task<UpdateAnimeStatusResponse> UpdateAnimeStatus(int animeId, int

UpdateAnimeStatusResponse updateResponse;
try {
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, CallType.PUT, builtUrl, new FormUrlEncodedContent(body.ToArray()));
var apiCall = await _authApiCall.AuthenticatedApiCall(ApiName.Mal, AuthApiCall.CallType.PUT, builtUrl, new FormUrlEncodedContent(body.ToArray()));

if (apiCall != null) {
StreamReader streamReader = new StreamReader(await apiCall.Content.ReadAsStreamAsync());
Expand All @@ -238,13 +238,5 @@ public async Task<UpdateAnimeStatusResponse> UpdateAnimeStatus(int animeId, int

return updateResponse;
}

public enum CallType {
GET,
POST,
PATCH,
PUT,
DELETE
}
}
}
2 changes: 1 addition & 1 deletion Helpers/GraphQlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static async Task<HttpResponseMessage> Request(HttpClient httpClient, str
public static async Task<HttpResponseMessage> AuthenticatedRequest(IHttpClientFactory httpClientFactory, ILoggerFactory loggerFactory, IServerApplicationHost serverApplicationHost, IHttpContextAccessor httpContextAccessor, UserConfig userConfig, string query, Dictionary<string, string> variables = null) {
AuthApiCall authApiCall = new AuthApiCall(ApiName.AniList, httpClientFactory, serverApplicationHost, httpContextAccessor, loggerFactory, userConfig);
var xd = JsonSerializer.Serialize(new GraphQl { Query = query, Variables = variables });
var call = await authApiCall.AuthenticatedApiCall(ApiName.AniList, MalApiCalls.CallType.POST, "https://graphql.anilist.co", stringContent: new StringContent(JsonSerializer.Serialize(new GraphQl { Query = query, Variables = variables }), Encoding.UTF8, "application/json"));
var call = await authApiCall.AuthenticatedApiCall(ApiName.AniList, AuthApiCall.CallType.POST, "https://graphql.anilist.co", stringContent: new StringContent(JsonSerializer.Serialize(new GraphQl { Query = query, Variables = variables }), Encoding.UTF8, "application/json"));

return call.IsSuccessStatusCode ? call : null;
}
Expand Down

0 comments on commit 53faf69

Please sign in to comment.