Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from aspriddell/remove-redirect-handler
Browse files Browse the repository at this point in the history
Remove redirect handler
  • Loading branch information
aspriddell authored Jun 9, 2021
2 parents 17a9ddd + fa3fae8 commit 80ac8b2
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
5 changes: 4 additions & 1 deletion DragonFruit.Orbit.API.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_EXISTING_DECLARATION_PARENS_ARRANGEMENT/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_EXISTING_EXPR_MEMBER_ARRANGEMENT/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/KEEP_EXISTING_EXPR_MEMBER_ARRANGEMENT/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_EXPR_ACCESSOR_ON_SINGLE_LINE/@EntryValue">ALWAYS</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_EXPR_METHOD_ON_SINGLE_LINE/@EntryValue">ALWAYS</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_EXPR_PROPERTY_ON_SINGLE_LINE/@EntryValue">NEVER</s:String>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">Orbit API Copyright (C) 2019-$CURRENT_YEAR$ DragonFruit Network&#xD;
Licensed under the MIT License - see the LICENSE file at the root of the project for more info</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ArrangeAccessorOwnerBody/@EntryIndexedValue">WARNING</s:String>
Expand Down
14 changes: 0 additions & 14 deletions src/OrbitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Threading;
using DragonFruit.Common.Data;
using DragonFruit.Common.Data.Extensions;
using DragonFruit.Common.Data.Handlers;
using DragonFruit.Orbit.Api.Auth;
using DragonFruit.Orbit.Api.Auth.Requests;
using DragonFruit.Orbit.Api.Legacy.Requests;
Expand Down Expand Up @@ -109,19 +108,6 @@ public T Perform<T>(OsuAuthRequest request, CancellationToken token = default) w

#endregion

protected sealed override HttpMessageHandler CreateHandler()
{
var handler = base.CreateHandler();

if (handler == null)
{
return new HeaderPreservingRedirectHandler();
}

// we need to wrap the handler in a header preservation handler because of user lookup redirs
return handler is HeaderPreservingRedirectHandler ? handler : new HeaderPreservingRedirectHandler(handler);
}

protected override T ValidateAndProcess<T>(HttpResponseMessage response, HttpRequestMessage request)
{
switch (response.StatusCode)
Expand Down
26 changes: 18 additions & 8 deletions src/User/Extensions/OsuUserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class OsuUserExtensions
/// <param name="mode">Optional mode to return stats for</param>
public static OsuUser GetUser<T>(this T client, uint id, GameMode? mode = null) where T : OrbitClient
{
return GetUser(client, id.ToString(), mode);
return GetUser(client, id.ToString(), mode, false);
}

/// <summary>
Expand All @@ -28,30 +28,40 @@ public static OsuUser GetUser<T>(this T client, uint id, GameMode? mode = null)
/// <param name="mode">Optional mode name to return stats for</param>
public static OsuUser GetUser<T>(this T client, uint userId, string mode) where T : OrbitClient
{
return GetUser(client, userId.ToString(), mode);
return GetUser(client, userId.ToString(), mode, false);
}

/// <summary>
/// Lookup a user by their username
/// </summary>
/// <param name="client">The <see cref="OrbitClient"/> to use</param>
/// <param name="username">The username</param>
/// <param name="identifier">The username</param>
/// <param name="mode">Optional mode to return stats for</param>
public static OsuUser GetUser<T>(this T client, string username, GameMode? mode = null) where T : OrbitClient
/// <param name="isUsername">Whether the <see cref="identifier"/> represents a username. If unset, the server will guess</param>
public static OsuUser GetUser<T>(this T client, string identifier, GameMode? mode = null, bool? isUsername = null) where T : OrbitClient
{
var request = new OsuUserRequest(username, mode);
var request = new OsuUserRequest(identifier, mode)
{
IsUsername = isUsername
};

return client.Perform<OsuUser>(request);
}

/// <summary>
/// Lookup a user by their username
/// </summary>
/// <param name="client">The <see cref="OrbitClient"/> to use</param>
/// <param name="username">The username</param>
/// <param name="identifier">The username</param>
/// <param name="mode">Optional mode name to return stats for</param>
public static OsuUser GetUser<T>(this T client, string username, string mode) where T : OrbitClient
/// <param name="isUsername">Whether the <see cref="identifier"/> represents a username. If unset, the server will guess</param>
public static OsuUser GetUser<T>(this T client, string identifier, string mode, bool? isUsername = null) where T : OrbitClient
{
var request = new OsuUserRequest(username, mode);
var request = new OsuUserRequest(identifier, mode)
{
IsUsername = isUsername
};

return client.Perform<OsuUser>(request);
}

Expand Down
28 changes: 28 additions & 0 deletions src/User/Requests/OsuUserRequest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Orbit API Copyright (C) 2019-2021 DragonFruit Network
// Licensed under the MIT License - see the LICENSE file at the root of the project for more info

using DragonFruit.Common.Data.Parameters;
using DragonFruit.Orbit.Api.Utils;

namespace DragonFruit.Orbit.Api.User.Requests
Expand All @@ -26,14 +27,41 @@ public OsuUserRequest(string identifier, GameMode? mode)
Mode = mode;
}

/// <summary>
/// The user identifier to lookup (either a username or a user id)
/// </summary>
public string Identifier { get; set; }

/// <summary>
/// If set, will inform the server the <see cref="Identifier"/> of its type.
/// Leave as <c>null</c> to let the server predict the type.
/// </summary>
public bool? IsUsername { get; set; }

/// <summary>
/// The <see cref="GameMode"/> to return statistics for.
/// </summary>
public GameMode? Mode
{
get => EnumUtils.GetInternalValue<GameMode>(ModeName);
set => ModeName = value.ToExternalValue();
}

/// <summary>
/// The gamemode to return statistics for
/// </summary>
/// <remarks>
/// If possible, use the <see cref="Mode"/> property for strongly-typed modes
/// </remarks>
public string ModeName { get; set; }

[QueryParameter("key")]
private string IdentifierType => IsUsername switch
{
true => "username",
false => "id",

_ => null
};
}
}
2 changes: 1 addition & 1 deletion tests/User/OsuUserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void TestUserModeNameLookup(uint id, string mode)
}

[TestCase("PaPaCurry", 13723332u)]
public void TestUserRedirectLookup(string username, uint id)
public void TestUsernameLookup(string username, uint id)
{
var user = Client.GetUser(username);
Assert.AreEqual(id, user.Id);
Expand Down

0 comments on commit 80ac8b2

Please sign in to comment.