From 53faf6907956c55a8ef0d900bc1f67ce263a8987 Mon Sep 17 00:00:00 2001 From: Patrick <44092756+vigetious@users.noreply.github.com> Date: Mon, 28 Mar 2022 12:00:03 +0100 Subject: [PATCH] moved CallType to AuthApiCall.cs --- Api/AuthApiCall.cs | 20 ++++++++++++++------ Api/Kitsu/KitsuApiCalls.cs | 12 ++++++------ Api/Mal/MalApiCalls.cs | 18 +++++------------- Helpers/GraphQlHelper.cs | 2 +- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Api/AuthApiCall.cs b/Api/AuthApiCall.cs index 05ec757..dc1fa95 100644 --- a/Api/AuthApiCall.cs +++ b/Api/AuthApiCall.cs @@ -42,7 +42,7 @@ public AuthApiCall(ApiName provider, IHttpClientFactory httpClientFactory, IServ /// Authentication details not found. /// Non-200 response. /// Could not authenticate with the API. - public async Task AuthenticatedApiCall(ApiName provider, MalApiCalls.CallType callType, string url, FormUrlEncodedContent formUrlEncodedContent = null, StringContent stringContent = null) { + public async Task AuthenticatedApiCall(ApiName provider, CallType callType, string url, FormUrlEncodedContent formUrlEncodedContent = null, StringContent stringContent = null) { int attempts = 0; UserApiAuth auth; try { @@ -60,19 +60,19 @@ public async Task 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: @@ -110,5 +110,13 @@ public async Task 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 + } } } \ No newline at end of file diff --git a/Api/Kitsu/KitsuApiCalls.cs b/Api/Kitsu/KitsuApiCalls.cs index c294c36..73e8f50 100644 --- a/Api/Kitsu/KitsuApiCalls.cs +++ b/Api/Kitsu/KitsuApiCalls.cs @@ -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(await streamReader.ReadToEndAsync()); @@ -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()); @@ -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(await streamReader.ReadToEndAsync()); @@ -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(await streamReader.ReadToEndAsync()); @@ -210,7 +210,7 @@ public async Task 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; @@ -231,7 +231,7 @@ public async Task 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()); diff --git a/Api/Mal/MalApiCalls.cs b/Api/Mal/MalApiCalls.cs index c290bdb..697346e 100644 --- a/Api/Mal/MalApiCalls.cs +++ b/Api/Mal/MalApiCalls.cs @@ -48,7 +48,7 @@ public async Task 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(); @@ -85,7 +85,7 @@ public async Task> 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(await streamReader.ReadToEndAsync()); @@ -113,7 +113,7 @@ public async Task 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(); @@ -157,7 +157,7 @@ public async Task> GetUserAnimeList(Status? status = nul UserAnimeList userAnimeList = new UserAnimeList { Data = new List() }; 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(); @@ -219,7 +219,7 @@ public async Task 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()); @@ -238,13 +238,5 @@ public async Task UpdateAnimeStatus(int animeId, int return updateResponse; } - - public enum CallType { - GET, - POST, - PATCH, - PUT, - DELETE - } } } \ No newline at end of file diff --git a/Helpers/GraphQlHelper.cs b/Helpers/GraphQlHelper.cs index 0663f9a..af0034e 100644 --- a/Helpers/GraphQlHelper.cs +++ b/Helpers/GraphQlHelper.cs @@ -22,7 +22,7 @@ public static async Task Request(HttpClient httpClient, str public static async Task AuthenticatedRequest(IHttpClientFactory httpClientFactory, ILoggerFactory loggerFactory, IServerApplicationHost serverApplicationHost, IHttpContextAccessor httpContextAccessor, UserConfig userConfig, string query, Dictionary 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; }