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 support for min_id pagination parameter #50

Merged
merged 3 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,27 @@

To use Mastonet, you need a `MastodonClient` object (see [README.md](https://github.com/glacasa/Mastonet/blob/master/README.md) ), and you have C# methods to call every [Mastodon API](https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md) method.

If a method is missing, please [submit an issue](https://github.com/glacasa/Mastonet/issues)
If a method is missing, please [submit an issue](https://github.com/glacasa/Mastonet/issues).

## Working with lists
## Working with pagination

Several Mastodon methods returns arrays of different types of items. Default usage will usually return the last 20 or 40 items, and you can add params to you request to handle pagination.
Some Mastodon REST APIs that return an array of items, also return pagination hint in their response header.

Those methods contains 3 parameters : `maxId`, `sinceId`, and `limit` ; and return a `MastodonList<T>`.
`MastodonList<T>` inherits `List<T>` and can be used the same way, and contains 2 more properties : `NextPageMaxId` and `PreviousPageSinceId`.
Methods corresponding to these APIs return `MastodonList<T>` and have 2 overloads: One takes 3 parameters: `maxId`, `sinceId` and `limit`, and the other takes a single `ArrayOptions options` parameter.
`ArrayOptions` is basically a bundle of the 3 parameters (it has `MaxId`, `SinceId` and `Limit` properties), except it can have `MinId` property supported since Mastodon v2.6.0.

The returned `MastodonList<T>` inherits `List<T>` and can be used the same way, and contains 3 more properties : `NextPageMaxId`, `PreviousPageSinceId` and `PreviousPageMinId`.

The default behaviour of a client app should be like this :

- On the first load, the methods is called with no params, and gets the last items
- If you want to load older items (next page), call the method with `maxId` param, defined with the `NextPageMaxId` property from the previous list
- To get newer items (previous page), call the method with `sinceId` param, defined with the `PreviousPageSinceId` property from the previous list
- To get newer items (previous page), call the method with `minId` param, defined with the `PreviousPageMinId` property from the previous list
- If you want to load newest items but don't need already fetched ones, call the method with `options` param with `MinId` property set, defined with the `PreviousPageSinceId` property from the previous list

You can always let the `limit` to its default value or define the number of items you want to load.

All the methods can be called with the 3 params `maxId`, `sinceId`, and `limit`, or using an `ArrayOptions` object. The result will be the same. Only the methods with `ArrayOptions` are presented below.
Note that the 3-parameter overloads don't have `minId` for backward compatibility. Use the overloads with `ArrayOptions` if you need it.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should mark those 3-parameters methods obsolete in a future (major) release ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. They can be easily rewritten to use ArrayOptions and their advantage is just a little convenience. Furthermore, if min_id is present, the other id parameters seem to be ignored (mastodon/mastodon#8736), so arbitrarily specifyable parameters can be misleading to users. Class can handle it.


## Methods

Expand Down
6 changes: 6 additions & 0 deletions Mastonet/ArrayOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class ArrayOptions

public long? SinceId { get; set; }

public long? MinId { get; set; }

public int? Limit { get; set; }

internal string ToQueryString()
Expand All @@ -24,6 +26,10 @@ internal string ToQueryString()
{
query.Add("since_id=" + this.SinceId);
}
if (this.MinId.HasValue)
{
query.Add("min_id=" + this.MinId);
}
if (this.Limit.HasValue)
{
query.Add("limit=" + this.Limit);
Expand Down
9 changes: 8 additions & 1 deletion Mastonet/BaseHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,14 @@ protected async Task<MastodonList<T>> GetMastodonList<T>(string route, IEnumerab

if (link.Contains("rel=\"prev\""))
{
result.PreviousPageSinceId = long.Parse(idFinderRegex.Match(link).Groups[1].Value);
if (link.Contains("since_id"))
{
result.PreviousPageSinceId = long.Parse(idFinderRegex.Match(link).Groups[1].Value);
}
if (link.Contains("min_id"))
{
result.PreviousPageMinId = long.Parse(idFinderRegex.Match(link).Groups[1].Value);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Mastonet/Entities/MastodonList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public class MastodonList<T> : List<T>
{
public long? NextPageMaxId { get; internal set; }
public long? PreviousPageSinceId { get; internal set; }
public long? PreviousPageMinId { get; internal set; }
}
}