2017-02-07 12:59:30 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2016-08-31 18:49:34 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
2016-09-01 20:46:19 +08:00
|
|
|
|
using osu.Framework.Extensions;
|
2016-08-31 18:49:34 +08:00
|
|
|
|
using osu.Framework.IO.Network;
|
|
|
|
|
|
|
|
|
|
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>
|
2016-09-27 17:31:36 +08:00
|
|
|
|
public class APIRequest<T> : APIRequest
|
2016-08-31 18:49:34 +08:00
|
|
|
|
{
|
|
|
|
|
protected override WebRequest CreateWebRequest() => new JsonWebRequest<T>(Uri);
|
|
|
|
|
|
|
|
|
|
public APIRequest()
|
|
|
|
|
{
|
|
|
|
|
base.Success += onSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void onSuccess()
|
|
|
|
|
{
|
2017-03-07 09:59:19 +08:00
|
|
|
|
Success?.Invoke(((JsonWebRequest<T>)WebRequest).ResponseObject);
|
2016-08-31 18:49:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public new event APISuccessHandler<T> Success;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// AN API request with no specified response type.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class APIRequest
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The maximum amount of time before this request will fail.
|
|
|
|
|
/// </summary>
|
2016-09-27 17:31:36 +08:00
|
|
|
|
public int Timeout = WebRequest.DEFAULT_TIMEOUT;
|
2016-08-31 18:49:34 +08:00
|
|
|
|
|
|
|
|
|
protected virtual string Target => string.Empty;
|
|
|
|
|
|
|
|
|
|
protected virtual WebRequest CreateWebRequest() => new WebRequest(Uri);
|
|
|
|
|
|
|
|
|
|
protected virtual string Uri => $@"{api.Endpoint}/api/v2/{Target}";
|
|
|
|
|
|
|
|
|
|
private double remainingTime => Math.Max(0, Timeout - (DateTime.Now.TotalMilliseconds() - (startTime ?? 0)));
|
|
|
|
|
|
2016-09-27 17:31:36 +08:00
|
|
|
|
public bool ExceededTimeout => remainingTime == 0;
|
2016-08-31 18:49:34 +08:00
|
|
|
|
|
|
|
|
|
private double? startTime;
|
|
|
|
|
|
2016-09-27 17:31:36 +08:00
|
|
|
|
public double StartTime => startTime ?? -1;
|
2016-08-31 18:49:34 +08:00
|
|
|
|
|
|
|
|
|
private APIAccess api;
|
|
|
|
|
protected WebRequest WebRequest;
|
|
|
|
|
|
|
|
|
|
public event APISuccessHandler Success;
|
|
|
|
|
public event APIFailureHandler Failure;
|
|
|
|
|
|
2017-03-15 17:07:26 +08:00
|
|
|
|
private bool cancelled;
|
|
|
|
|
|
2016-09-27 17:31:36 +08:00
|
|
|
|
public void Perform(APIAccess api)
|
2016-08-31 18:49:34 +08:00
|
|
|
|
{
|
2017-03-15 17:07:26 +08:00
|
|
|
|
if (cancelled) return;
|
|
|
|
|
|
2016-08-31 18:49:34 +08:00
|
|
|
|
if (startTime == null)
|
|
|
|
|
startTime = DateTime.Now.TotalMilliseconds();
|
|
|
|
|
|
|
|
|
|
this.api = api;
|
|
|
|
|
|
|
|
|
|
if (remainingTime <= 0)
|
|
|
|
|
throw new TimeoutException(@"API request timeout hit");
|
|
|
|
|
|
|
|
|
|
WebRequest = CreateWebRequest();
|
|
|
|
|
WebRequest.RetryCount = 0;
|
|
|
|
|
WebRequest.Headers[@"Authorization"] = $@"Bearer {api.AccessToken}";
|
|
|
|
|
|
|
|
|
|
WebRequest.BlockingPerform();
|
|
|
|
|
|
2017-03-15 17:07:26 +08:00
|
|
|
|
if (WebRequest.Completed)
|
|
|
|
|
api.Scheduler.Add(delegate { Success?.Invoke(); });
|
2016-08-31 18:49:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-15 17:07:26 +08:00
|
|
|
|
public void Cancel() => Fail(new OperationCanceledException(@"Request cancelled"));
|
|
|
|
|
|
2016-09-27 17:31:36 +08:00
|
|
|
|
public void Fail(Exception e)
|
2016-08-31 18:49:34 +08:00
|
|
|
|
{
|
2017-03-15 17:07:26 +08:00
|
|
|
|
cancelled = true;
|
|
|
|
|
|
2016-08-31 18:49:34 +08:00
|
|
|
|
WebRequest?.Abort();
|
2016-09-27 18:22:02 +08:00
|
|
|
|
api.Scheduler.Add(delegate
|
|
|
|
|
{
|
2016-08-31 18:49:34 +08:00
|
|
|
|
Failure?.Invoke(e);
|
2016-09-27 18:22:02 +08:00
|
|
|
|
});
|
2016-08-31 18:49:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public delegate void APIFailureHandler(Exception e);
|
|
|
|
|
public delegate void APISuccessHandler();
|
2017-03-09 13:24:16 +08:00
|
|
|
|
public delegate void APISuccessHandler<in T>(T content);
|
2016-08-31 18:49:34 +08:00
|
|
|
|
}
|