Add back basic API support for channel/message retrieval.

This commit is contained in:
Dean Herbert
2016-09-27 18:31:36 +09:00
parent cd57661aa2
commit 28045b7136
11 changed files with 174 additions and 27 deletions

View File

@ -10,11 +10,11 @@ using osu.Game.Online.API.Requests;
namespace osu.Game.Online.API
{
internal class APIAccess
public class APIAccess
{
private OAuth authentication;
internal string Endpoint = @"https://new.ppy.sh";
public string Endpoint = @"https://new.ppy.sh";
const string ClientId = @"daNBnfdv7SppRVc61z0XuOI13y6Hroiz";
const string ClientSecret = @"d6fgZuZeQ0eSXkEj5igdqQX6ztdtS6Ow";
@ -52,7 +52,7 @@ namespace osu.Game.Online.API
Logger log;
internal APIAccess()
public APIAccess()
{
authentication = new OAuth(ClientId, ClientSecret, Endpoint);
log = Logger.GetLogger(LoggingTarget.Network);
@ -61,7 +61,7 @@ namespace osu.Game.Online.API
thread.Start();
}
internal string AccessToken => authentication.RequestAccessToken();
public string AccessToken => authentication.RequestAccessToken();
/// <summary>
/// Number of consecutive requests which failed due to network issues.
@ -221,16 +221,16 @@ namespace osu.Game.Online.API
}
}
internal void Queue(APIRequest request)
public void Queue(APIRequest request)
{
queue.Enqueue(request);
}
internal event StateChangeDelegate OnStateChange;
public event StateChangeDelegate OnStateChange;
internal delegate void StateChangeDelegate(APIState oldState, APIState newState);
public delegate void StateChangeDelegate(APIState oldState, APIState newState);
internal enum APIState
public enum APIState
{
/// <summary>
/// We cannot login (not enough credentials).
@ -268,7 +268,7 @@ namespace osu.Game.Online.API
}
}
internal void Logout()
public void Logout()
{
authentication.Clear();
State = APIState.Offline;

View File

@ -11,7 +11,7 @@ namespace osu.Game.Online.API
/// An API request with a well-defined response type.
/// </summary>
/// <typeparam name="T">Type of the response (used for deserialisation).</typeparam>
internal class APIRequest<T> : APIRequest
public class APIRequest<T> : APIRequest
{
protected override WebRequest CreateWebRequest() => new JsonWebRequest<T>(Uri);
@ -36,7 +36,7 @@ namespace osu.Game.Online.API
/// <summary>
/// The maximum amount of time before this request will fail.
/// </summary>
internal int Timeout = WebRequest.DEFAULT_TIMEOUT;
public int Timeout = WebRequest.DEFAULT_TIMEOUT;
protected virtual string Target => string.Empty;
@ -46,11 +46,11 @@ namespace osu.Game.Online.API
private double remainingTime => Math.Max(0, Timeout - (DateTime.Now.TotalMilliseconds() - (startTime ?? 0)));
internal bool ExceededTimeout => remainingTime == 0;
public bool ExceededTimeout => remainingTime == 0;
private double? startTime;
internal double StartTime => startTime ?? -1;
public double StartTime => startTime ?? -1;
private APIAccess api;
protected WebRequest WebRequest;
@ -58,7 +58,7 @@ namespace osu.Game.Online.API
public event APISuccessHandler Success;
public event APIFailureHandler Failure;
internal void Perform(APIAccess api)
public void Perform(APIAccess api)
{
if (startTime == null)
startTime = DateTime.Now.TotalMilliseconds();
@ -79,7 +79,7 @@ namespace osu.Game.Online.API
//});
}
internal void Fail(Exception e)
public void Fail(Exception e)
{
WebRequest?.Abort();
//OsuGame.Scheduler.Add(delegate {

View File

@ -7,7 +7,7 @@ using osu.Game.Online.Chat;
namespace osu.Game.Online.API.Requests
{
internal class GetMessagesRequest : APIRequest<List<Message>>
public class GetMessagesRequest : APIRequest<List<Message>>
{
List<Channel> channels;
long? since;

View File

@ -6,7 +6,7 @@ using osu.Game.Online.Chat;
namespace osu.Game.Online.API.Requests
{
internal class ListChannelsRequest : APIRequest<List<Channel>>
public class ListChannelsRequest : APIRequest<List<Channel>>
{
protected override string Target => @"chat/channels";
}

View File

@ -24,7 +24,7 @@ namespace osu.Game.Online.Chat
internal string Content;
[JsonProperty(@"sender")]
internal string User;
internal User User;
[JsonConstructor]
public Message()

16
osu.Game/Online/User.cs Normal file
View File

@ -0,0 +1,16 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using Newtonsoft.Json;
namespace osu.Game.Online
{
public class User
{
[JsonProperty(@"username")]
public string Name;
[JsonProperty(@"colour")]
public string Colour;
}
}

View File

@ -25,14 +25,6 @@ namespace osu.Game
Add(new MainMenu());
}
protected override void Dispose(bool isDisposing)
{
//refresh token may have changed.
Config.Set(OsuConfig.Token, API.Token);
base.Dispose(isDisposing);
}
public override bool Invalidate(Invalidation invalidation = Invalidation.All, Drawable source = null, bool shallPropagate = true)
{
if (!base.Invalidate(invalidation, source, shallPropagate)) return false;

View File

@ -15,7 +15,7 @@ namespace osu.Game
protected override string MainResourceFile => @"osu.Game.Resources.dll";
internal APIAccess API;
public APIAccess API;
protected override Container AddTarget => ratioContainer?.IsLoaded == true ? ratioContainer : base.AddTarget;
@ -49,5 +49,14 @@ namespace osu.Game
}
});
}
protected override void Dispose(bool isDisposing)
{
//refresh token may have changed.
Config.Set(OsuConfig.Token, API.Token);
Config.Save();
base.Dispose(isDisposing);
}
}
}

View File

@ -62,6 +62,7 @@
<Compile Include="Online\API\Requests\ListChannels.cs" />
<Compile Include="Online\Chat\Channel.cs" />
<Compile Include="Online\Chat\Message.cs" />
<Compile Include="Online\User.cs" />
<Compile Include="OsuGame.cs" />
<Compile Include="OsuGameBase.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />