This repository was archived by the owner on Dec 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAniAPI.cs
More file actions
181 lines (145 loc) · 5.7 KB
/
AniAPI.cs
File metadata and controls
181 lines (145 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using AniAPI.NET.Filters;
using AniAPI.NET.Helpers;
using AniAPI.NET.Interfaces;
using AniAPI.NET.Models;
using AniAPI.NET.Models.Resources;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace AniAPI.NET
{
public class AniAPI : IAniAPI
{
private HttpHelper _httpHelper;
private OAuthHelper _oAuthHelper;
#region Singleton
private static AniAPI _instance;
public static AniAPI Instance
{
get
{
if(_instance == null)
{
_instance = new AniAPI();
}
return _instance;
}
}
private AniAPI()
{
_httpHelper = new HttpHelper();
}
#endregion
public void UseHTTP()
{
_httpHelper.UseHTTP();
}
public void UseHTTPS()
{
_httpHelper.UseHTTPS();
}
public void UseImplicitGrant(string clientId, string redirectUri)
{
_oAuthHelper = new OAuthHelper(clientId, redirectUri);
}
public void UseAuthorizationCode(string clientId, string clientSecret, string redirectUri)
{
_oAuthHelper = new OAuthHelper(clientId, clientSecret, redirectUri);
}
public void ManualJWT(string jwt)
{
_httpHelper.SetJWT(jwt);
}
#region Implementation
public async Task Login()
{
if (_oAuthHelper == null)
{
throw new Exception("OAuth not setted up!");
}
string token = await _oAuthHelper.Login();
if (string.IsNullOrEmpty(token))
{
throw new Exception("Token not valid!");
}
_httpHelper.SetJWT(token);
}
public async Task<APIResponse<User>> GetMe()
{
return await _httpHelper.AuthorizedRequest<User>($"auth/me", HttpMethod.Get);
}
public async Task<APIResponse<Anime>> GetAnime(long id)
{
return await _httpHelper.UnauthorizedRequest<Anime>($"anime/{id}", HttpMethod.Get);
}
public async Task<APIResponse<Pagination<Anime>>> GetAnimeList(AnimeFilter filter)
{
return await _httpHelper.UnauthorizedRequest<Pagination<Anime>>($"anime/{filter.ToQueryString()}", HttpMethod.Get);
}
public async Task<APIResponse<Episode>> GetEpisode(long id)
{
return await _httpHelper.UnauthorizedRequest<Episode>($"episode/{id}", HttpMethod.Get);
}
public async Task<APIResponse<Pagination<Episode>>> GetEpisodeList(EpisodeFilter filter)
{
return await _httpHelper.UnauthorizedRequest<Pagination<Episode>>($"episode/{filter.ToQueryString()}", HttpMethod.Get);
}
public async Task<APIResponse<Song>> GetSong(long id)
{
return await _httpHelper.UnauthorizedRequest<Song>($"song/{id}", HttpMethod.Get);
}
public async Task<APIResponse<Pagination<Song>>> GetSongList(SongFilter filter)
{
return await _httpHelper.UnauthorizedRequest<Pagination<Song>>($"song/{filter.ToQueryString()}", HttpMethod.Get);
}
public async Task<APIResponse<GenresResource>> GetGenres(string version = "1.0")
{
return await _httpHelper.UnauthorizedRequest<GenresResource>($"resources/{version}/0", HttpMethod.Get);
}
public async Task<APIResponse<LocalizationsResource>> GetLocalizations(string version = "1.0")
{
return await _httpHelper.UnauthorizedRequest<LocalizationsResource>($"resources/{version}/1", HttpMethod.Get);
}
public async Task<APIResponse<User>> GetUser(long id)
{
return await _httpHelper.UnauthorizedRequest<User>($"user/{id}", HttpMethod.Get);
}
public async Task<APIResponse<Pagination<User>>> GetUserList(UserFilter filter)
{
return await _httpHelper.UnauthorizedRequest<Pagination<User>>($"user/{filter.ToQueryString()}", HttpMethod.Get);
}
public async Task<APIResponse<User>> UpdateUser(User model)
{
return await _httpHelper.AuthorizedRequest<User>("user", HttpMethod.Post, JsonConvert.SerializeObject(model));
}
public async Task<APIResponse<string>> DeleteUser(long id)
{
return await _httpHelper.AuthorizedRequest<string>($"user/{id}", HttpMethod.Delete);
}
public async Task<APIResponse<UserStory>> GetUserStory(long id)
{
return await _httpHelper.AuthorizedRequest<UserStory>($"user_story/{id}", HttpMethod.Get);
}
public async Task<APIResponse<Pagination<UserStory>>> GetUserStoryList(UserStoryFilter filter)
{
return await _httpHelper.AuthorizedRequest<Pagination<UserStory>>($"user_story/{filter.ToQueryString()}", HttpMethod.Get);
}
public async Task<APIResponse<UserStory>> CreateUserStory(UserStory model)
{
return await _httpHelper.AuthorizedRequest<UserStory>("user_story", HttpMethod.Put, JsonConvert.SerializeObject(model));
}
public async Task<APIResponse<UserStory>> UpdateUserStory(UserStory model)
{
return await _httpHelper.AuthorizedRequest<UserStory>("user_story", HttpMethod.Post, JsonConvert.SerializeObject(model));
}
public async Task<APIResponse<string>> DeleteUserStory(long id)
{
return await _httpHelper.AuthorizedRequest<string>($"user_story/{id}", HttpMethod.Delete);
}
#endregion
}
}