using AniAPI.NET.Models; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace AniAPI.NET.Helpers { internal class HttpHelper { private string _protocol; private string _hostName; private string _version; private string _jwt; private HttpClient _httpClient; public HttpHelper() { _protocol = "https"; _hostName = "api.aniapi.com"; _version = "v1"; _httpClient = new HttpClient(); } private string endpoint => $"{_protocol}://{_hostName}/{_version}"; private async Task> executeRequest(string path, HttpMethod method, string body = null, bool auth = false) { try { string uri = $"{endpoint}/{path}"; HttpRequestMessage request = new HttpRequestMessage(method, uri); if (auth) { request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _jwt); } if (!string.IsNullOrEmpty(body)) { request.Content = new StringContent(body, Encoding.UTF8, "application/json"); } HttpResponseMessage response = await _httpClient.SendAsync(request); if (!response.IsSuccessStatusCode) { throw new Exception(response.StatusCode.ToString()); } string responseContent = await response.Content.ReadAsStringAsync(); APIResponse result = null; try { result = JsonConvert.DeserializeObject>(responseContent); } catch { JObject error = JObject.Parse(responseContent); throw new InvalidOperationException(error.Value("data")); } if(result.StatusCode != 200) { throw new Exception($"{result.StatusCode}: {result.Message}"); } return result; } catch(Exception ex) { throw; } } public void UseHTTP() { _protocol = "http"; } public void UseHTTPS() { _protocol = "https"; } public void SetJWT(string jwt) { _jwt = jwt; } public async Task> UnauthorizedRequest(string path, HttpMethod method) { return await executeRequest(path, method); } public async Task> AuthorizedRequest(string path, HttpMethod method, string body = null) { if (string.IsNullOrEmpty(_jwt)) { throw new ArgumentException("You need to login to perform this request", "JWT"); } return await executeRequest(path, method, body, true); } } }