mirror of
https://github.com/ppy/osu
synced 2025-01-11 08:39:31 +00:00
Add basic request / response support
This commit is contained in:
parent
3a36c0008f
commit
832822858c
39
osu.Game.Tests/Online/TestDummyAPIRequestHandling.cs
Normal file
39
osu.Game.Tests/Online/TestDummyAPIRequestHandling.cs
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using osu.Game.Online.API;
|
||||
using osu.Game.Online.API.Requests;
|
||||
using osu.Game.Online.API.Requests.Responses;
|
||||
using osu.Game.Tests.Visual;
|
||||
|
||||
namespace osu.Game.Tests.Online
|
||||
{
|
||||
public class TestDummyAPIRequestHandling : OsuTestScene
|
||||
{
|
||||
public TestDummyAPIRequestHandling()
|
||||
{
|
||||
AddStep("register request handling", () => ((DummyAPIAccess)API).HandleRequest = req =>
|
||||
{
|
||||
switch (req)
|
||||
{
|
||||
case CommentVoteRequest cRequest:
|
||||
cRequest.TriggerSuccess(new CommentBundle());
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
CommentVoteRequest request = null;
|
||||
CommentBundle response = null;
|
||||
|
||||
AddStep("fire request", () =>
|
||||
{
|
||||
response = null;
|
||||
request = new CommentVoteRequest(1, CommentVoteAction.Vote);
|
||||
request.Success += res => response = res;
|
||||
API.Queue(request);
|
||||
});
|
||||
|
||||
AddAssert("got response", () => response != null);
|
||||
}
|
||||
}
|
||||
}
|
@ -30,6 +30,8 @@ namespace osu.Game.Online.API
|
||||
/// This will be scheduled to the API's internal scheduler (run on update thread automatically).
|
||||
/// </summary>
|
||||
public new event APISuccessHandler<T> Success;
|
||||
|
||||
internal void TriggerSuccess(T result) => Success?.Invoke(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -1,6 +1,7 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@ -30,6 +31,11 @@ namespace osu.Game.Online.API
|
||||
|
||||
private readonly List<IOnlineComponent> components = new List<IOnlineComponent>();
|
||||
|
||||
/// <summary>
|
||||
/// Provide handling logic for an arbitrary API request.
|
||||
/// </summary>
|
||||
public Action<APIRequest> HandleRequest;
|
||||
|
||||
public APIState State
|
||||
{
|
||||
get => state;
|
||||
@ -55,6 +61,7 @@ namespace osu.Game.Online.API
|
||||
|
||||
public virtual void Queue(APIRequest request)
|
||||
{
|
||||
HandleRequest?.Invoke(request);
|
||||
}
|
||||
|
||||
public void Perform(APIRequest request) { }
|
||||
|
Loading…
Reference in New Issue
Block a user