Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
Minor fix
Browse files Browse the repository at this point in the history
* Fixed a bug on API error JSON parse
+ Added GetMe method
  • Loading branch information
DazornSama committed Sep 20, 2021
1 parent 7d52e46 commit f282e9f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
16 changes: 16 additions & 0 deletions AniAPI.NET.Test/UserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ public UserTest(ITestOutputHelper output)
Build();
}

[Fact]
public async void Get_Me()
{
AniAPI.Instance.ManualJWT(this.configuration["JWT"]);

var result = await AniAPI.Instance.GetMe();

Assert.NotNull(result);
Assert.IsType<APIResponse<User>>(result);

Assert.True(result.StatusCode == 200);
Assert.NotNull(result.Data);

output.WriteLine(JsonConvert.SerializeObject(result));
}

[Fact]
public async void Get_User()
{
Expand Down
4 changes: 2 additions & 2 deletions AniAPI.NET/AniAPI.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<PackageId>AniAPI.NET</PackageId>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<Authors>Dazorn96</Authors>
<Company>AnIAPI</Company>
<PackageReleaseNotes>Removed unused packages</PackageReleaseNotes>
<PackageReleaseNotes>Fixed a bug and added GetMe method</PackageReleaseNotes>
<PackageTags>aniapi anime api wrapper netcore</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
Expand Down
15 changes: 10 additions & 5 deletions AniAPI.NET/AniAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ public void UseAuthorizationCode(string clientId, string clientSecret, string re
_oAuthHelper = new OAuthHelper(clientId, clientSecret, redirectUri);
}

public void ManualJWT(string jwt)
{
_httpHelper.SetJWT(jwt);
}

#region Implementation

public async Task Login()
{
if(_oAuthHelper == null)
if (_oAuthHelper == null)
{
throw new Exception("OAuth not setted up!");
}
Expand All @@ -79,13 +86,11 @@ public async Task Login()
_httpHelper.SetJWT(token);
}

public void ManualJWT(string jwt)
public async Task<APIResponse<User>> GetMe()
{
_httpHelper.SetJWT(jwt);
return await _httpHelper.AuthorizedRequest<User>($"auth/me", HttpMethod.Get);
}

#region Implementation

public async Task<APIResponse<Anime>> GetAnime(long id)
{
return await _httpHelper.UnauthorizedRequest<Anime>($"anime/{id}", HttpMethod.Get);
Expand Down
13 changes: 12 additions & 1 deletion AniAPI.NET/Helpers/HttpHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AniAPI.NET.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -57,7 +58,17 @@ private async Task<APIResponse<T>> executeRequest<T>(string path, HttpMethod met

string responseContent = await response.Content.ReadAsStringAsync();

APIResponse<T> result = JsonConvert.DeserializeObject<APIResponse<T>>(responseContent);
APIResponse<T> result = null;

try
{
result = JsonConvert.DeserializeObject<APIResponse<T>>(responseContent);
}
catch
{
JObject error = JObject.Parse(responseContent);
throw new InvalidOperationException(error.Value<string>("data"));
}

if(result.StatusCode != 200)
{
Expand Down
3 changes: 2 additions & 1 deletion AniAPI.NET/Interfaces/IAniAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public interface IAniAPI
{
#region OAuth


public Task Login();
public Task<APIResponse<User>> GetMe();

#endregion

Expand Down

0 comments on commit f282e9f

Please sign in to comment.