1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 09:27:34 +08:00

Merge pull request #7014 from peppy/add-guest-perform-api

Add API methods to perform requests out-of-queue
This commit is contained in:
Dan Balasescu 2019-12-02 13:25:50 +09:00 committed by GitHub
commit fd0361160c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 46 additions and 35 deletions

View File

@ -228,7 +228,7 @@ namespace osu.Game.Tournament
if (b.BeatmapInfo == null && b.ID > 0)
{
var req = new GetBeatmapRequest(new BeatmapInfo { OnlineBeatmapID = b.ID });
req.Perform(API);
API.Perform(req);
b.BeatmapInfo = req.Result?.ToBeatmap(RulesetStore);
addedInfo = true;

View File

@ -398,7 +398,7 @@ namespace osu.Game.Beatmaps
try
{
// intentionally blocking to limit web request concurrency
req.Perform(api);
api.Perform(req);
var res = req.Result;

View File

@ -99,17 +99,7 @@ namespace osu.Game.Database
currentDownloads.Add(request);
PostNotification?.Invoke(notification);
Task.Factory.StartNew(() =>
{
try
{
request.Perform(api);
}
catch (Exception error)
{
triggerFailure(error);
}
}, TaskCreationOptions.LongRunning);
api.PerformAsync(request);
DownloadBegan?.Invoke(request);
return true;

View File

@ -7,6 +7,7 @@ using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -198,6 +199,22 @@ namespace osu.Game.Online.API
}
}
public void Perform(APIRequest request)
{
try
{
request.Perform(this);
}
catch (Exception e)
{
// todo: fix exception handling
request.Fail(e);
}
}
public Task PerformAsync(APIRequest request) =>
Task.Factory.StartNew(() => Perform(request), TaskCreationOptions.LongRunning);
public void Login(string username, string password)
{
Debug.Assert(State == APIState.Offline);

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Users;
@ -56,6 +57,10 @@ namespace osu.Game.Online.API
{
}
public void Perform(APIRequest request) { }
public Task PerformAsync(APIRequest request) => Task.CompletedTask;
public void Register(IOnlineComponent component)
{
Scheduler.Add(delegate { components.Add(component); });

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.Threading.Tasks;
using osu.Framework.Bindables;
using osu.Game.Users;
@ -42,6 +43,24 @@ namespace osu.Game.Online.API
/// <param name="request">The request to perform.</param>
void Queue(APIRequest request);
/// <summary>
/// Perform a request immediately, bypassing any API state checks.
/// </summary>
/// <remarks>
/// Can be used to run requests as a guest user.
/// </remarks>
/// <param name="request">The request to perform.</param>
void Perform(APIRequest request);
/// <summary>
/// Perform a request immediately, bypassing any API state checks.
/// </summary>
/// <remarks>
/// Can be used to run requests as a guest user.
/// </remarks>
/// <param name="request">The request to perform.</param>
Task PerformAsync(APIRequest request);
/// <summary>
/// Register a component to receive state changes.
/// </summary>

View File

@ -4,7 +4,6 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
@ -43,18 +42,7 @@ namespace osu.Game.Overlays.Changelog
};
req.Failure += _ => complete = true;
// This is done on a separate thread to support cancellation below
Task.Run(() =>
{
try
{
req.Perform(api);
}
catch
{
complete = true;
}
});
api.PerformAsync(req);
while (!complete)
{

View File

@ -191,15 +191,7 @@ namespace osu.Game.Overlays
tcs.SetResult(false);
};
try
{
req.Perform(API);
}
catch
{
initialFetchTask = null;
tcs.SetResult(false);
}
await API.PerformAsync(req);
await tcs.Task;
});