-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change the way how (most) API request are done
- Loading branch information
Showing
18 changed files
with
310 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,6 +55,7 @@ private void load() | |
{ | ||
Children = new Drawable[] | ||
{ | ||
Fluxel, | ||
AudioClock, | ||
Samples, | ||
NotificationManager, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using osu.Framework.IO.Network; | ||
|
||
namespace fluXis.Game.Online.API; | ||
|
||
public class APIRequest<T> where T : class | ||
{ | ||
protected virtual string Path => string.Empty; | ||
protected virtual HttpMethod Method => HttpMethod.Get; | ||
|
||
public event Action<APIResponse<T>> Success; | ||
public event Action<Exception> Failure; | ||
public event Action<long, long> Progress; | ||
|
||
public APIResponse<T> Response { get; private set; } | ||
|
||
private Fluxel.Fluxel fluxel; | ||
private APIEndpointConfig config => fluxel.Endpoint; | ||
|
||
public void Perform(Fluxel.Fluxel fluxel) | ||
{ | ||
this.fluxel = fluxel; | ||
|
||
var request = new JsonWebRequest<APIResponse<T>>($"{config.APIUrl}{Path}", Method); | ||
request.AllowInsecureRequests = true; | ||
request.UploadProgress += (current, total) => Progress?.Invoke(current, total); | ||
|
||
if (!string.IsNullOrEmpty(fluxel.Token)) | ||
request.AddHeader("Authorization", fluxel.Token); | ||
|
||
try | ||
{ | ||
CreatePostData(request); | ||
request.Perform(); | ||
Response = request.ResponseObject; | ||
} | ||
catch (Exception e) | ||
{ | ||
Response = new APIResponse<T>(400, e.Message, null); | ||
Failure?.Invoke(e); | ||
} | ||
|
||
if (Response != null) | ||
fluxel.Schedule(() => Success?.Invoke(Response)); | ||
} | ||
|
||
public Task PerformAsync(Fluxel.Fluxel fluxel) | ||
{ | ||
var task = new Task(() => Perform(fluxel)); | ||
task.Start(); | ||
return task; | ||
} | ||
|
||
protected virtual void CreatePostData(JsonWebRequest<APIResponse<T>> request) { } | ||
} |
22 changes: 22 additions & 0 deletions
22
fluXis.Game/Online/API/Requests/Account/DisplayNameUpdateRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System.Net.Http; | ||
using osu.Framework.IO.Network; | ||
|
||
namespace fluXis.Game.Online.API.Requests.Account; | ||
|
||
public class DisplayNameUpdateRequest : APIRequest<dynamic> | ||
{ | ||
protected override string Path => "/account/update/displayname"; | ||
protected override HttpMethod Method => HttpMethod.Post; | ||
|
||
private string displayName { get; } | ||
|
||
public DisplayNameUpdateRequest(string displayName) | ||
{ | ||
this.displayName = displayName; | ||
} | ||
|
||
protected override void CreatePostData(JsonWebRequest<APIResponse<dynamic>> request) | ||
{ | ||
request.AddRaw(displayName); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
fluXis.Game/Online/API/Requests/Account/SocialUpdateRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using System.Net.Http; | ||
using Newtonsoft.Json; | ||
using osu.Framework.IO.Network; | ||
|
||
namespace fluXis.Game.Online.API.Requests.Account; | ||
|
||
public class SocialUpdateRequest : APIRequest<dynamic> | ||
{ | ||
protected override string Path => "/account/update/socials"; | ||
protected override HttpMethod Method => HttpMethod.Post; | ||
|
||
private string twitter { get; } | ||
private string youtube { get; } | ||
private string twitch { get; } | ||
private string discord { get; } | ||
|
||
public SocialUpdateRequest(string twitter, string youtube, string twitch, string discord) | ||
{ | ||
this.twitter = twitter; | ||
this.youtube = youtube; | ||
this.twitch = twitch; | ||
this.discord = discord; | ||
} | ||
|
||
protected override void CreatePostData(JsonWebRequest<APIResponse<dynamic>> request) | ||
{ | ||
request.AddRaw(JsonConvert.SerializeObject(new Dictionary<string, string> | ||
{ | ||
{ "twitter", twitter }, | ||
{ "youtube", youtube }, | ||
{ "twitch", twitch }, | ||
{ "discord", discord } | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Collections.Generic; | ||
using fluXis.Game.Online.API.Models.Maps; | ||
|
||
namespace fluXis.Game.Online.API.Requests.MapSets; | ||
|
||
public class MapSetsRequest : APIRequest<List<APIMapSet>> | ||
{ | ||
protected override string Path => "/mapsets"; | ||
} |
15 changes: 15 additions & 0 deletions
15
fluXis.Game/Online/API/Requests/Maps/MapHashLookupRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using fluXis.Game.Online.API.Models.Maps; | ||
|
||
namespace fluXis.Game.Online.API.Requests.Maps; | ||
|
||
public class MapHashLookupRequest : APIRequest<APIMap> | ||
{ | ||
protected override string Path => $"/map/hash/{hash}"; | ||
|
||
private string hash { get; } | ||
|
||
public MapHashLookupRequest(string hash) | ||
{ | ||
this.hash = hash; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
fluXis.Game/Online/API/Requests/Maps/MapLeaderboardRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using fluXis.Game.Online.API.Models.Scores; | ||
using fluXis.Game.Screens.Select.Info.Scores; | ||
|
||
namespace fluXis.Game.Online.API.Requests.Maps; | ||
|
||
public class MapLeaderboardRequest : APIRequest<APIScores> | ||
{ | ||
protected override string Path => type switch | ||
{ | ||
ScoreListType.Global => $"/map/{id}/scores", | ||
ScoreListType.Country => $"/map/{id}/scores/country", | ||
ScoreListType.Friends => $"/map/{id}/scores/friends", | ||
ScoreListType.Club => $"/map/{id}/scores/club", | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
|
||
private ScoreListType type { get; } | ||
private int id { get; } | ||
|
||
public MapLeaderboardRequest(ScoreListType type, int id) | ||
{ | ||
this.type = type; | ||
this.id = id; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
fluXis.Game/Online/API/Requests/Maps/MapSetUploadRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Net.Http; | ||
using fluXis.Game.Database.Maps; | ||
using fluXis.Game.Online.API.Models.Maps; | ||
using osu.Framework.IO.Network; | ||
|
||
namespace fluXis.Game.Online.API.Requests.Maps; | ||
|
||
public class MapSetUploadRequest : APIRequest<APIMapSet> | ||
{ | ||
protected override string Path => map.OnlineID != -1 ? $"/map/{map.OnlineID}/update" : "/maps"; | ||
protected override HttpMethod Method => HttpMethod.Post; | ||
|
||
private byte[] file { get; } | ||
private RealmMapSet map { get; } | ||
|
||
public MapSetUploadRequest(byte[] file, RealmMapSet map) | ||
{ | ||
this.file = file; | ||
this.map = map; | ||
} | ||
|
||
protected override void CreatePostData(JsonWebRequest<APIResponse<APIMapSet>> request) | ||
{ | ||
request.AddFile("file", file); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
fluXis.Game/Online/API/Requests/Scores/ScoreSubmitRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Net.Http; | ||
using fluXis.Game.Online.API.Models.Scores; | ||
using fluXis.Game.Scoring; | ||
using Newtonsoft.Json; | ||
using osu.Framework.IO.Network; | ||
|
||
namespace fluXis.Game.Online.API.Requests.Scores; | ||
|
||
public class ScoreSubmitRequest : APIRequest<APIScoreResponse> | ||
{ | ||
protected override string Path => "/scores/upload"; | ||
protected override HttpMethod Method => HttpMethod.Post; | ||
|
||
private ScoreInfo score { get; } | ||
private float scrollspeed { get; } | ||
|
||
public ScoreSubmitRequest(ScoreInfo score, float scrollspeed) | ||
{ | ||
this.score = score; | ||
this.scrollspeed = scrollspeed; | ||
} | ||
|
||
protected override void CreatePostData(JsonWebRequest<APIResponse<APIScoreResponse>> request) | ||
{ | ||
request.AddRaw(JsonConvert.SerializeObject(new | ||
{ | ||
hash = score.MapHash, | ||
mods = score.Mods, | ||
scrollSpeed = scrollspeed, | ||
maxCombo = score.MaxCombo, | ||
flawless = score.Flawless, | ||
perfect = score.Perfect, | ||
great = score.Great, | ||
alright = score.Alright, | ||
okay = score.Okay, | ||
miss = score.Miss | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using fluXis.Game.Online.API.Models.Users; | ||
|
||
namespace fluXis.Game.Online.API.Requests.Users; | ||
|
||
public class OnlineUsersRequest : APIRequest<APIOnlineUsers> | ||
{ | ||
protected override string Path => "/users/online"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.