1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 16:47:26 +08:00
osu-lazer/osu.Game/Online/API/DummyAPIAccess.cs
Dean Herbert f5ba746ae5 Fail all API requests sent to DummyAPIAccess
Until now, API requests sent to dummy API were just lost in the void. In most cases this somehow worked as expected, but any logic which is waiting on a request to finish will potentially never get a response.

Going forward, I'm not 100% sure that every `Wait` on a web response will have local timeout logic (I think there is a certain amount of assumption that this is being managed for us by `APIAccess`), so I've made this change to better handle such cases going forward. Now, rather than nothing happening, requests will trigger a failure via the existing exception logic rather than silently pretending the request never arrived.
2021-03-23 17:37:45 +09:00

106 lines
3.3 KiB
C#

// 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.Threading;
using System.Threading.Tasks;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Users;
namespace osu.Game.Online.API
{
public class DummyAPIAccess : Component, IAPIProvider
{
public Bindable<User> LocalUser { get; } = new Bindable<User>(new User
{
Username = @"Dummy",
Id = 1001,
});
public BindableList<User> Friends { get; } = new BindableList<User>();
public Bindable<UserActivity> Activity { get; } = new Bindable<UserActivity>();
public string AccessToken => "token";
public bool IsLoggedIn => State.Value == APIState.Online;
public string ProvidedUsername => LocalUser.Value.Username;
public string APIEndpointUrl => "http://localhost";
public string WebsiteRootUrl => "http://localhost";
/// <summary>
/// Provide handling logic for an arbitrary API request.
/// </summary>
public Action<APIRequest> HandleRequest;
private readonly Bindable<APIState> state = new Bindable<APIState>(APIState.Online);
/// <summary>
/// The current connectivity state of the API.
/// </summary>
public IBindable<APIState> State => state;
public DummyAPIAccess()
{
LocalUser.BindValueChanged(u =>
{
u.OldValue?.Activity.UnbindFrom(Activity);
u.NewValue.Activity.BindTo(Activity);
}, true);
}
public virtual void Queue(APIRequest request)
{
if (HandleRequest != null)
HandleRequest.Invoke(request);
else
// this will fail due to not receiving an APIAccess, and trigger a failure on the request.
// this is intended - any request in testing that needs non-failures should use HandleRequest.
request.Perform(this);
}
public void Perform(APIRequest request) => HandleRequest?.Invoke(request);
public Task PerformAsync(APIRequest request)
{
HandleRequest?.Invoke(request);
return Task.CompletedTask;
}
public void Login(string username, string password)
{
LocalUser.Value = new User
{
Username = username,
Id = 1001,
};
state.Value = APIState.Online;
}
public void Logout()
{
LocalUser.Value = new GuestUser();
state.Value = APIState.Offline;
}
public IHubClientConnector GetHubConnector(string clientName, string endpoint) => null;
public RegistrationRequest.RegistrationRequestErrors CreateAccount(string email, string username, string password)
{
Thread.Sleep(200);
return null;
}
public void SetState(APIState newState) => state.Value = newState;
IBindable<User> IAPIProvider.LocalUser => LocalUser;
IBindableList<User> IAPIProvider.Friends => Friends;
IBindable<UserActivity> IAPIProvider.Activity => Activity;
}
}