Skip to content

Commit

Permalink
Fix: Volumes paginated (#3)
Browse files Browse the repository at this point in the history
* small bug when creating paginated list for Volumes (doesn't seem to be an issue for other paginations)

* wrong string
  • Loading branch information
nicholi authored Sep 2, 2024
1 parent 67e8a60 commit 771b5ea
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion DigitalOcean.API/DigitalOceanClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

namespace DigitalOcean.API {
public class DigitalOceanClient : IDigitalOceanClient {
public static readonly string DigitalOceanApiUrl = "https://api.digitalocean.com/v2/";
// some APIs return only a relative URL of next paginated call
public const string RelativeUrl = "/v2/";
public const string DigitalOceanApiUrl = "https://api.digitalocean.com" + RelativeUrl;
private readonly IConnection _connection;

public DigitalOceanClient(string token) {
Expand Down
21 changes: 20 additions & 1 deletion DigitalOcean.API/Http/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,26 @@ public async Task<IReadOnlyList<T>> GetPaginated<T>(string endpoint, IList<Param
// loop until we are finished
var allItems = new List<T>(data);
while (page != null && page.Pages != null && !String.IsNullOrWhiteSpace(page.Pages.Next)) {
endpoint = page.Pages.Next.Replace(DigitalOceanClient.DigitalOceanApiUrl, "");
// some APIs seem to return the full URL in paginated links (or there is some code populating this?
// but others (notably /v2/volumes) returns only the relative URL
// maybe this was a bug introduced by DO? or it has always been this way, unknown
var absoluteIndex = page.Pages.Next.IndexOf(DigitalOceanClient.DigitalOceanApiUrl, StringComparison.Ordinal);
if (absoluteIndex == 0) {
// standard replacement of full URL (only if matches from beginning of string)
endpoint = page.Pages.Next.Substring(DigitalOceanClient.DigitalOceanApiUrl.Length);
}
else {
// test if relative URL replacement, and only replace from beginning of string
var relativeIndex = page.Pages.Next.IndexOf(DigitalOceanClient.RelativeUrl, StringComparison.Ordinal);
if (relativeIndex == 0) {
endpoint = page.Pages.Next.Substring(DigitalOceanClient.RelativeUrl.Length);
}
else {
// likely will error, but nothing is match to replacement
endpoint = page.Pages.Next;
}
}

var iter = await ExecuteRaw(endpoint, null).ConfigureAwait(false);

parsedJson = (JObject)JsonConvert.DeserializeObject(iter.Content);
Expand Down

0 comments on commit 771b5ea

Please sign in to comment.