// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Diagnostics; using System.Globalization; using System.Net.Http; using System.Threading.Tasks; using osu.Framework.Bindables; using osu.Framework.IO.Network; using osu.Game.Online; using osu.Game.Online.API; using PerformanceCalculatorGUI.Configuration; namespace PerformanceCalculatorGUI { internal class APIManager { public static readonly EndpointConfiguration ENDPOINT_CONFIGURATION = new ProductionEndpointConfiguration(); private readonly Bindable clientIdBindable; private readonly Bindable clientSecretBindable; private OAuthToken token; // WARN: keep in sync with /osu.Game/Online/API/APIAccess.cs APIVersion private const int api_version = 20220705; public APIManager(SettingsManager configManager) { clientIdBindable = configManager.GetBindable(Settings.ClientId); clientSecretBindable = configManager.GetBindable(Settings.ClientSecret); } public async Task GetJsonFromApi(string request) { if (token == null) { await getAccessToken().ConfigureAwait(false); Debug.Assert(token != null); } using var req = new JsonWebRequest($"{ENDPOINT_CONFIGURATION.APIUrl}/api/v2/{request}"); req.AddHeader("x-api-version", api_version.ToString(CultureInfo.InvariantCulture)); req.AddHeader(System.Net.HttpRequestHeader.Authorization.ToString(), $"Bearer {token.AccessToken}"); await req.PerformAsync().ConfigureAwait(false); return req.ResponseObject; } private async Task getAccessToken() { using var req = new JsonWebRequest($"{ENDPOINT_CONFIGURATION.APIUrl}/oauth/token") { Method = HttpMethod.Post }; req.AddParameter("client_id", clientIdBindable.Value); req.AddParameter("client_secret", clientSecretBindable.Value); req.AddParameter("grant_type", "client_credentials"); req.AddParameter("scope", "public"); await req.PerformAsync().ConfigureAwait(false); token = req.ResponseObject; } } }