1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 11:20:04 +08:00

Add support for non-generic requests

This commit is contained in:
Dean Herbert 2020-04-11 18:02:49 +09:00
parent 415adecdf6
commit c96df97586
2 changed files with 39 additions and 5 deletions

View File

@ -1,10 +1,13 @@
// 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 NUnit.Framework;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Chat;
using osu.Game.Tests.Visual;
using osu.Game.Users;
namespace osu.Game.Tests.Online
{
@ -38,6 +41,32 @@ namespace osu.Game.Tests.Online
AddAssert("request has response", () => request.Result == response);
}
[Test]
public void TestRequestHandling()
{
AddStep("register request handling", () => ((DummyAPIAccess)API).HandleRequest = req =>
{
switch (req)
{
case LeaveChannelRequest cRequest:
cRequest.TriggerSuccess();
break;
}
});
LeaveChannelRequest request;
bool gotResponse = false;
AddStep("fire request", () =>
{
gotResponse = false;
request = new LeaveChannelRequest(new Channel(), new User());
request.Success += () => gotResponse = true;
API.Queue(request);
});
AddAssert("response event fired", () => gotResponse);
}
}
}

View File

@ -17,22 +17,27 @@ namespace osu.Game.Online.API
{
protected override WebRequest CreateWebRequest() => new OsuJsonWebRequest<T>(Uri);
public T Result => ((OsuJsonWebRequest<T>)WebRequest)?.ResponseObject;
public T Result { get; private set; }
protected APIRequest()
{
base.Success += onSuccess;
base.Success += () => TriggerSuccess(((OsuJsonWebRequest<T>)WebRequest)?.ResponseObject);
}
private void onSuccess() => Success?.Invoke(Result);
/// <summary>
/// Invoked on successful completion of an API request.
/// 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);
internal void TriggerSuccess(T result)
{
// disallow calling twice
Debug.Assert(Result == null);
Result = result;
Success?.Invoke(result);
}
}
/// <summary>