Add basic request / response support

This commit is contained in:
Dean Herbert 2020-04-11 17:47:51 +09:00
parent 3a36c0008f
commit 832822858c
3 changed files with 48 additions and 0 deletions

View 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);
}
}
}

View File

@ -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>

View File

@ -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) { }