2019-01-24 16:43:03 +08:00
|
|
|
|
// 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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
2019-11-12 18:34:20 +08:00
|
|
|
|
using Newtonsoft.Json;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.IO.Network;
|
2018-12-18 19:19:40 +08:00
|
|
|
|
using osu.Framework.Logging;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Online.API
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// An API request with a well-defined response type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">Type of the response (used for deserialisation).</typeparam>
|
2020-03-30 14:05:40 +08:00
|
|
|
|
public abstract class APIRequest<T> : APIRequest where T : class
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-12-28 23:07:55 +08:00
|
|
|
|
protected override WebRequest CreateWebRequest() => new OsuJsonWebRequest<T>(Uri);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-04-11 17:02:49 +08:00
|
|
|
|
public T Result { get; private set; }
|
2018-06-08 13:37:48 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
protected APIRequest()
|
|
|
|
|
{
|
2020-04-11 17:02:49 +08:00
|
|
|
|
base.Success += () => TriggerSuccess(((OsuJsonWebRequest<T>)WebRequest)?.ResponseObject);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-08 13:37:27 +08:00
|
|
|
|
/// <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>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public new event APISuccessHandler<T> Success;
|
2020-04-11 16:47:51 +08:00
|
|
|
|
|
2020-04-11 17:02:49 +08:00
|
|
|
|
internal void TriggerSuccess(T result)
|
|
|
|
|
{
|
2020-04-13 20:24:47 +08:00
|
|
|
|
if (Result != null)
|
|
|
|
|
throw new InvalidOperationException("Attempted to trigger success more than once");
|
2020-04-11 17:02:49 +08:00
|
|
|
|
|
|
|
|
|
Result = result;
|
|
|
|
|
Success?.Invoke(result);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// AN API request with no specified response type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public abstract class APIRequest
|
|
|
|
|
{
|
2018-12-12 18:03:27 +08:00
|
|
|
|
protected abstract string Target { get; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-12-28 23:07:55 +08:00
|
|
|
|
protected virtual WebRequest CreateWebRequest() => new OsuWebRequest(Uri);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
protected virtual string Uri => $@"{API.Endpoint}/api/v2/{Target}";
|
|
|
|
|
|
|
|
|
|
protected APIAccess API;
|
|
|
|
|
protected WebRequest WebRequest;
|
|
|
|
|
|
2018-06-08 13:37:27 +08:00
|
|
|
|
/// <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>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public event APISuccessHandler Success;
|
2018-06-08 13:37:27 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked on failure to complete an API request.
|
|
|
|
|
/// This will be scheduled to the API's internal scheduler (run on update thread automatically).
|
|
|
|
|
/// </summary>
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public event APIFailureHandler Failure;
|
|
|
|
|
|
|
|
|
|
private bool cancelled;
|
|
|
|
|
|
|
|
|
|
private Action pendingFailure;
|
|
|
|
|
|
2019-03-13 11:56:47 +08:00
|
|
|
|
public void Perform(IAPIProvider api)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2019-03-13 11:56:47 +08:00
|
|
|
|
if (!(api is APIAccess apiAccess))
|
2019-09-25 14:00:08 +08:00
|
|
|
|
{
|
|
|
|
|
Fail(new NotSupportedException($"A {nameof(APIAccess)} is required to perform requests."));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-03-13 11:56:47 +08:00
|
|
|
|
|
|
|
|
|
API = apiAccess;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-12-18 19:19:40 +08:00
|
|
|
|
if (checkAndScheduleFailure())
|
2018-04-13 17:19:50 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
WebRequest = CreateWebRequest();
|
2018-06-03 12:01:52 +08:00
|
|
|
|
WebRequest.Failed += Fail;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
WebRequest.AllowRetryOnTimeout = false;
|
2019-03-13 11:56:47 +08:00
|
|
|
|
WebRequest.AddHeader("Authorization", $"Bearer {API.AccessToken}");
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-12-18 19:19:40 +08:00
|
|
|
|
if (checkAndScheduleFailure())
|
2018-04-13 17:19:50 +08:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!WebRequest.Aborted) //could have been aborted by a Cancel() call
|
2018-12-18 19:19:40 +08:00
|
|
|
|
{
|
|
|
|
|
Logger.Log($@"Performing request {this}", LoggingTarget.Network);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
WebRequest.Perform();
|
2018-12-18 19:19:40 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-12-18 19:19:40 +08:00
|
|
|
|
if (checkAndScheduleFailure())
|
2018-04-13 17:19:50 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2019-06-25 12:52:43 +08:00
|
|
|
|
API.Schedule(delegate
|
|
|
|
|
{
|
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
2020-04-11 17:02:43 +08:00
|
|
|
|
TriggerSuccess();
|
2019-06-25 12:52:43 +08:00
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-11 17:02:43 +08:00
|
|
|
|
internal void TriggerSuccess()
|
|
|
|
|
{
|
|
|
|
|
Success?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public void Cancel() => Fail(new OperationCanceledException(@"Request cancelled"));
|
|
|
|
|
|
|
|
|
|
public void Fail(Exception e)
|
|
|
|
|
{
|
2018-12-26 20:11:31 +08:00
|
|
|
|
if (WebRequest?.Completed == true)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (cancelled)
|
|
|
|
|
return;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-12-18 19:19:40 +08:00
|
|
|
|
cancelled = true;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
WebRequest?.Abort();
|
|
|
|
|
|
2019-11-21 09:55:31 +08:00
|
|
|
|
string responseString = WebRequest?.GetResponseString();
|
2019-11-12 18:34:20 +08:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(responseString))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// attempt to decode a displayable error string.
|
|
|
|
|
var error = JsonConvert.DeserializeObject<DisplayableError>(responseString);
|
|
|
|
|
if (error != null)
|
2019-11-28 21:52:05 +08:00
|
|
|
|
e = new APIException(error.ErrorMessage, e);
|
2019-11-12 18:34:20 +08:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-18 19:19:40 +08:00
|
|
|
|
Logger.Log($@"Failing request {this} ({e})", LoggingTarget.Network);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
pendingFailure = () => Failure?.Invoke(e);
|
2018-12-18 19:19:40 +08:00
|
|
|
|
checkAndScheduleFailure();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checked for cancellation or error. Also queues up the Failed event if we can.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Whether we are in a failed or cancelled state.</returns>
|
2018-12-18 19:19:40 +08:00
|
|
|
|
private bool checkAndScheduleFailure()
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
if (API == null || pendingFailure == null) return cancelled;
|
|
|
|
|
|
|
|
|
|
API.Schedule(pendingFailure);
|
|
|
|
|
pendingFailure = null;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-11-12 18:34:20 +08:00
|
|
|
|
|
|
|
|
|
private class DisplayableError
|
|
|
|
|
{
|
|
|
|
|
[JsonProperty("error")]
|
2019-11-25 10:30:55 +08:00
|
|
|
|
public string ErrorMessage { get; set; }
|
2019-11-12 18:34:20 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 21:52:05 +08:00
|
|
|
|
public class APIException : InvalidOperationException
|
|
|
|
|
{
|
|
|
|
|
public APIException(string messsage, Exception innerException)
|
|
|
|
|
: base(messsage, innerException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public delegate void APIFailureHandler(Exception e);
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public delegate void APISuccessHandler();
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public delegate void APIProgressHandler(long current, long total);
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
public delegate void APISuccessHandler<in T>(T content);
|
|
|
|
|
}
|