Daanniello / ScoreSaberLib

Scoresaber API library
Other
1 stars 0 forks source link

Add baseURL and generic GetEndpoint. #1

Closed RickTimmer closed 3 years ago

RickTimmer commented 3 years ago

After reading through the code for a bit I noticed that the methods seem to not differ much. It seems like a lot of the duplicate code can be avoided by something like this:

readonly string baseURL = "https://new.scoresaber.com/api";
private async Task<T> GetEndpoint<T>(string endpoint)
{
    HttpResponseMessage response = await client.GetAsync($"{baseURL}{endpoint}");
    if (!response.IsSuccessStatusCode)
    {
        return null;
    }
    return JsonConvert.DeserializeObject<T>(await response.Content.ReadAsStringAsync());
}

I think this should work for all current endpoints apart from GetAvatar. But that seems to not really be an endpoint.

Bare in mind that I am not a c# developer so the syntax might not be completely correct.

RickTimmer commented 3 years ago

PR -> https://github.com/Daanniello/ScoreSaberLib/pull/2

Daanniello commented 3 years ago

Noiceee done!