Skip to content

Commit

Permalink
Updated for new BeatSaver API updates to /users/name (new) and /searc…
Browse files Browse the repository at this point in the history
…h/text
  • Loading branch information
Auros committed Aug 20, 2021
1 parent 3f165de commit 09379a8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 52 deletions.
41 changes: 1 addition & 40 deletions BeatSaverSharp.Tests/CachingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,14 @@ public async Task TestSmartUserCachingBeatmapDirect()
Assert.IsNull(toxicVioletCubes.Uploader.Stats);

// Now we get the user directly
var abcbadq = await Client.User(390);
var abcbadq = await Client.User(4284943);
Assert.IsNotNull(abcbadq);
Assert.IsNotNull(abcbadq.Stats);

// And the user stats property from Toxic Violet Cubes should have been updated
Assert.IsNotNull(toxicVioletCubes.Uploader.Stats);
}

[TestMethod]
public async Task TestSmartUserCachingUserDirect()
{
// Umbranox
var umbranoxByName = await Client.User("umbranox");
Assert.IsNotNull(umbranoxByName);

// Stats should be null since we searched by username.
Assert.IsNull(umbranoxByName.Stats);

// True Umbranox
var umbranoxByID = await Client.User(24854);
Assert.IsNotNull(umbranoxByID);

// Stats should be NOT null since we searched by ID.
Assert.IsNotNull(umbranoxByID.Stats);

// The umbranox by name search should have non-null user stats.
Assert.IsNotNull(umbranoxByName.Stats);
}


[TestMethod]
public async Task TestSmartUserCachingUserRefresh()
{
// zorowo
var zorowo = await Client.User("zorowo");
Assert.IsNotNull(zorowo);

// Stats should be null since we searched by username.
Assert.IsNull(zorowo.Stats);

// Now we refresh
await zorowo.Refresh();

// The zorowo by name search should have non-null user stats.
Assert.IsNotNull(zorowo.Stats);
}

[TestMethod]
public async Task TestSmartUserCachingUserInverseDirect()
{
Expand Down
2 changes: 1 addition & 1 deletion BeatSaverSharp.Tests/PagingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task TestPaging()
Assert.IsFalse(ratingPage.Empty);

// Now we check to see if they're in the right order
Assert.IsTrue(ratingPage.Beatmaps[ratingPage.Beatmaps.Count - 1].Stats.Score > nextPage.Beatmaps[0].Stats.Score);
Assert.IsTrue(ratingPage.Beatmaps[ratingPage.Beatmaps.Count - 1].Stats.Score >= nextPage.Beatmaps[0].Stats.Score);

// And now we test is the previous page is the same as the first.
var previousPage = await nextPage.Previous();
Expand Down
26 changes: 18 additions & 8 deletions BeatSaverSharp/BeatSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,20 @@ public BeatSaver(string applicationName, Version version) : this(new BeatSaverOp
sb.Append("maps/latest?automapper=");
sb.Append(options.IncludeAutomappers);
if (options.StartDate.HasValue)
sb.Append("&after=").Append(options.StartDate.Value.ToString("yyyy-MM-ddTHH:mm:ssZ"));
sb.Append("&before=").Append(options.StartDate.Value.ToString("yyyy-MM-ddTHH:mm:ssZ"));
if (options.Before.HasValue)
sb.Append("&after=").Append(options.Before.Value.ToString("yyyy-MM-ddTHH:mm:ssZ"));
if (options.Sort.HasValue)
{
string sort = options.Sort.Value switch
{
LatestFilterSort.CREATED => "CREATED",
LatestFilterSort.UPDATED => "UPDATED",
LatestFilterSort.LAST_PUBLISHED => "LAST_PUBLISHED",
_ => "FIRST_PUBLISHED",
};
sb.Append("&sort=").Append(sort);
}

var result = await GetBeatmapsFromPage(sb.ToString(), token).ConfigureAwait(false);
if (result is null)
Expand Down Expand Up @@ -204,18 +217,15 @@ public BeatSaver(string applicationName, Version version) : this(new BeatSaverOp
return user;
}

var response = await _httpService.GetAsync("search/text/0?q=" + name, token).ConfigureAwait(false);
var response = await _httpService.GetAsync("users/name/" + name, token).ConfigureAwait(false);
if (!response.Successful)
return null;

var page = await response.ReadAsObjectAsync<SerializableSearch>().ConfigureAwait(false);
if (page.Docs.Count == 0)
return null;

if (page.User is null)
var fetchedUser = await response.ReadAsObjectAsync<User>().ConfigureAwait(false);
if (fetchedUser is null)
return null;

GetOrAddUserToCache(page.User, out user);
GetOrAddUserToCache(fetchedUser, out user);
return user;
}

Expand Down
2 changes: 1 addition & 1 deletion BeatSaverSharp/BeatSaverSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
<Version>3.0.1</Version>
<Version>3.0.2</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<Configurations>Debug;Release;Release-Unity</Configurations>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand Down
9 changes: 7 additions & 2 deletions BeatSaverSharp/Models/Pages/UploadedPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ public UploadedPage(UploadedFilterOptions query, IReadOnlyList<Beatmap> beatmaps
return nextPage;
}

public override Task<Page?> Previous(CancellationToken token = default)
public override async Task<Page?> Previous(CancellationToken token = default)
{
return Task.FromResult(PreviousPage);
if (PreviousPage is null && Beatmaps.Count > 0)
{
UploadedFilterOptions options = new UploadedFilterOptions(_query.StartDate, Beatmaps[0].Uploaded, _query.IncludeAutomappers, _query.Sort);
PreviousPage = await Client.LatestBeatmaps(options, token).ConfigureAwait(false);
}
return await Task.FromResult(PreviousPage);
}
}
}
27 changes: 27 additions & 0 deletions BeatSaverSharp/UploadedFilterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,42 @@ public class UploadedFilterOptions
/// </summary>
public DateTime? StartDate { get; set; }

/// <summary>
/// Returns maps before the time specified.
/// </summary>
public DateTime? Before { get; set; }

/// <summary>
/// Whether or not to include automapped beatmaps in your search.
/// </summary>
public bool IncludeAutomappers { get; set; }

/// <summary>
/// The sorting applied to the search.
/// </summary>
public LatestFilterSort? Sort { get; set; }

public UploadedFilterOptions(DateTime? startDate, bool includeAutomappers)
{
StartDate = startDate;
IncludeAutomappers = includeAutomappers;
}

public UploadedFilterOptions(DateTime? startDate, DateTime? beforeDate, bool includeAutomappers, LatestFilterSort? sort)
{
Sort = sort;
Before = beforeDate;
StartDate = startDate;
IncludeAutomappers = includeAutomappers;
}
}


public enum LatestFilterSort
{
FIRST_PUBLISHED,
UPDATED,
LAST_PUBLISHED,
CREATED
}
}

0 comments on commit 09379a8

Please sign in to comment.