Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mods and ruleset parameters to GetDifficultyAttributesAsync #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
48 changes: 46 additions & 2 deletions OsuSharp/Endpoints/Beatmaps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,54 @@ public async Task<BeatmapExtended[]> GetBeatmapsAsync(params int[] ids)
/// <a href="https://osu.ppy.sh/docs/index.html#get-beatmap"/>
/// </summary>
/// <param name="id">The ID of the beatmap.</param>
/// <param name="ruleset">The ruleset to get the attributes from.</param>
minisbett marked this conversation as resolved.
Show resolved Hide resolved
/// <returns>The difficulty attributes or null, if the beatmap was not found.</returns>
public async Task<DifficultyAttributes?> GetDifficultyAttributesAsync(int id)
public Task<DifficultyAttributes?> GetDifficultyAttributesAsync(int id, string? ruleset = null) => GetDifficultyAttributesInternalAsync(id, null, ruleset);
minisbett marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets the beatmap with the specified ID.
minisbett marked this conversation as resolved.
Show resolved Hide resolved
/// If the beatmap was not found, null is returned.
/// <br/><br/>
/// API notes:<br/>
/// <a href="https://osu.ppy.sh/docs/index.html#get-beatmap"/>
/// </summary>
/// <param name="id">The ID of the beatmap.</param>
/// <param name="mods">The mods in the acronym form to get the attributes from.</param>
minisbett marked this conversation as resolved.
Show resolved Hide resolved
/// <param name="ruleset">The ruleset to get the attributes from.</param>
minisbett marked this conversation as resolved.
Show resolved Hide resolved
/// <returns>The difficulty attributes or null, if the beatmap was not found.</returns>
public Task<DifficultyAttributes?> GetDifficultyAttributesAsync(int id, string[] mods, string? ruleset = null) => GetDifficultyAttributesInternalAsync(id, mods, ruleset);
minisbett marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets the beatmap with the specified ID.
minisbett marked this conversation as resolved.
Show resolved Hide resolved
/// If the beatmap was not found, null is returned.
/// <br/><br/>
/// API notes:<br/>
/// <a href="https://osu.ppy.sh/docs/index.html#get-beatmap"/>
/// </summary>
/// <param name="id">The ID of the beatmap.</param>
/// <param name="mods">The mods in a bitset form to get the attributes from.</param>
minisbett marked this conversation as resolved.
Show resolved Hide resolved
/// <param name="ruleset">The ruleset to get the attributes from.</param>
minisbett marked this conversation as resolved.
Show resolved Hide resolved
/// <returns>The difficulty attributes or null, if the beatmap was not found.</returns>
public Task<DifficultyAttributes?> GetDifficultyAttributesAsync(int id, int mods, string? ruleset = null) => GetDifficultyAttributesInternalAsync(id, mods, ruleset);
minisbett marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Gets the beatmap with the specified ID.
minisbett marked this conversation as resolved.
Show resolved Hide resolved
/// If the beatmap was not found, null is returned.
/// <br/><br/>
/// API notes:<br/>
/// <a href="https://osu.ppy.sh/docs/index.html#get-beatmap"/>
/// </summary>
/// <param name="id">The ID of the beatmap.</param>
/// <param name="mods">The mods in either a bitset of mods or a string array with the mods.</param>
minisbett marked this conversation as resolved.
Show resolved Hide resolved
/// <param name="ruleset">The ruleset to get the attributes from.</param>
/// <returns>The difficulty attributes or null, if the beatmap was not found.</returns>
private async Task<DifficultyAttributes?> GetDifficultyAttributesInternalAsync(int id, object? mods = null, string? ruleset = null)
minisbett marked this conversation as resolved.
Show resolved Hide resolved
{
// Send the request and return the difficulty attributes object.
return await GetFromJsonAsync<DifficultyAttributes>($"beatmaps/{id}/attributes", jsonSelector: x => x["attributes"], method: HttpMethod.Post);
return await GetFromJsonAsync<DifficultyAttributes>($"beatmaps/{id}/attributes", new Dictionary<string, object?>()
{
{ "ruleset", ruleset },
{ "mods", mods }
}, x => x["attributes"], method: HttpMethod.Post);
}
}
16 changes: 15 additions & 1 deletion OsuSharp/OsuApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.ComponentModel;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Reflection;
using System.Web;

Expand Down Expand Up @@ -116,8 +117,18 @@ public async Task EnsureAccessTokenAsync()

try
{
// Fallback to GET if nothing else is provided
method ??= HttpMethod.Get;

// Prepare HTTP request
var message = new HttpRequestMessage(method, method == HttpMethod.Get ? $"{url}?{BuildQueryString(parameters)}" : url);

// Append POST body if required
if (method == HttpMethod.Post)
message.Content = JsonContent.Create(parameters);

// Send the request and validate the response. If 404 is returned, return null.
HttpResponseMessage response = await _http.SendAsync(new HttpRequestMessage(method ?? HttpMethod.Get, $"{url}?{BuildQueryString(parameters)}"));
HttpResponseMessage response = await _http.SendAsync(message);
if (response.StatusCode == HttpStatusCode.NotFound)
return default;

Expand Down Expand Up @@ -165,6 +176,9 @@ private static string BuildQueryString(Dictionary<string, object?> parameters)
else if (kvp.Value is DateTime dt)
// Use the ISO 8601 format for dates.
str += dt.ToString("o");
else if (kvp.Value is bool boolean)
// The API is case-sensitive for booleans
str += boolean.ToString().ToLower();
minisbett marked this conversation as resolved.
Show resolved Hide resolved
else
str += HttpUtility.HtmlEncode(kvp.Value!.ToString());
}
Expand Down