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

Commit f282e9f

Browse files
committed
Minor fix
* Fixed a bug on API error JSON parse + Added GetMe method
1 parent 7d52e46 commit f282e9f

File tree

5 files changed

+42
-9
lines changed

5 files changed

+42
-9
lines changed

AniAPI.NET.Test/UserTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ public UserTest(ITestOutputHelper output)
2929
Build();
3030
}
3131

32+
[Fact]
33+
public async void Get_Me()
34+
{
35+
AniAPI.Instance.ManualJWT(this.configuration["JWT"]);
36+
37+
var result = await AniAPI.Instance.GetMe();
38+
39+
Assert.NotNull(result);
40+
Assert.IsType<APIResponse<User>>(result);
41+
42+
Assert.True(result.StatusCode == 200);
43+
Assert.NotNull(result.Data);
44+
45+
output.WriteLine(JsonConvert.SerializeObject(result));
46+
}
47+
3248
[Fact]
3349
public async void Get_User()
3450
{

AniAPI.NET/AniAPI.NET.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
55
<PackageId>AniAPI.NET</PackageId>
6-
<Version>1.0.1</Version>
6+
<Version>1.0.2</Version>
77
<Authors>Dazorn96</Authors>
88
<Company>AnIAPI</Company>
9-
<PackageReleaseNotes>Removed unused packages</PackageReleaseNotes>
9+
<PackageReleaseNotes>Fixed a bug and added GetMe method</PackageReleaseNotes>
1010
<PackageTags>aniapi anime api wrapper netcore</PackageTags>
1111
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1212
</PropertyGroup>

AniAPI.NET/AniAPI.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,16 @@ public void UseAuthorizationCode(string clientId, string clientSecret, string re
6262
_oAuthHelper = new OAuthHelper(clientId, clientSecret, redirectUri);
6363
}
6464

65+
public void ManualJWT(string jwt)
66+
{
67+
_httpHelper.SetJWT(jwt);
68+
}
69+
70+
#region Implementation
71+
6572
public async Task Login()
6673
{
67-
if(_oAuthHelper == null)
74+
if (_oAuthHelper == null)
6875
{
6976
throw new Exception("OAuth not setted up!");
7077
}
@@ -79,13 +86,11 @@ public async Task Login()
7986
_httpHelper.SetJWT(token);
8087
}
8188

82-
public void ManualJWT(string jwt)
89+
public async Task<APIResponse<User>> GetMe()
8390
{
84-
_httpHelper.SetJWT(jwt);
91+
return await _httpHelper.AuthorizedRequest<User>($"auth/me", HttpMethod.Get);
8592
}
8693

87-
#region Implementation
88-
8994
public async Task<APIResponse<Anime>> GetAnime(long id)
9095
{
9196
return await _httpHelper.UnauthorizedRequest<Anime>($"anime/{id}", HttpMethod.Get);

AniAPI.NET/Helpers/HttpHelper.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AniAPI.NET.Models;
22
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Linq;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
@@ -57,7 +58,17 @@ private async Task<APIResponse<T>> executeRequest<T>(string path, HttpMethod met
5758

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

60-
APIResponse<T> result = JsonConvert.DeserializeObject<APIResponse<T>>(responseContent);
61+
APIResponse<T> result = null;
62+
63+
try
64+
{
65+
result = JsonConvert.DeserializeObject<APIResponse<T>>(responseContent);
66+
}
67+
catch
68+
{
69+
JObject error = JObject.Parse(responseContent);
70+
throw new InvalidOperationException(error.Value<string>("data"));
71+
}
6172

6273
if(result.StatusCode != 200)
6374
{

AniAPI.NET/Interfaces/IAniAPI.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public interface IAniAPI
1313
{
1414
#region OAuth
1515

16-
16+
public Task Login();
17+
public Task<APIResponse<User>> GetMe();
1718

1819
#endregion
1920

0 commit comments

Comments
 (0)