Skip to content

Commit

Permalink
Implement optional pagination for Http::get_guilds (#1403)
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerd008 authored and arqunis committed Mar 15, 2022
1 parent 5223ea0 commit 06d101b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
17 changes: 12 additions & 5 deletions src/http/client.rs
Expand Up @@ -2767,16 +2767,23 @@ impl Http {
///
/// let guild_id = GuildId(81384788765712384);
///
/// let guilds = http.get_guilds(&GuildPagination::After(guild_id), 10).await?;
/// let guilds = http.get_guilds(Some(&GuildPagination::After(guild_id)), Some(10)).await?;
/// # Ok(())
/// # }
/// ```
///
/// [docs]: https://discord.com/developers/docs/resources/user#get-current-user-guilds
pub async fn get_guilds(&self, target: &GuildPagination, limit: u64) -> Result<Vec<GuildInfo>> {
let (after, before) = match *target {
GuildPagination::After(id) => (Some(id.0), None),
GuildPagination::Before(id) => (None, Some(id.0)),
pub async fn get_guilds(
&self,
target: Option<&GuildPagination>,
limit: Option<u64>,
) -> Result<Vec<GuildInfo>> {
let (after, before) = match target {
None => (None, None),
Some(gp) => match gp {
GuildPagination::After(id) => (Some(id.0), None),
GuildPagination::Before(id) => (None, Some(id.0)),
},
};

self.fire(Request {
Expand Down
11 changes: 8 additions & 3 deletions src/http/routing.rs
Expand Up @@ -872,9 +872,14 @@ impl Route {
target: D,
after: Option<u64>,
before: Option<u64>,
limit: u64,
limit: Option<u64>,
) -> String {
let mut s = format!(api!("/users/{}/guilds?limit={}&"), target, limit);
let mut s = format!(api!("/users/{}/guilds?"), target);

if let Some(limit) = limit {
#[allow(clippy::let_underscore_must_use)]
let _ = write!(s, "&limit={}", limit);
}

if let Some(after) = after {
let _ = write!(s, "&after={}", after);
Expand Down Expand Up @@ -1442,7 +1447,7 @@ pub enum RouteInfo<'a> {
GetGuilds {
after: Option<u64>,
before: Option<u64>,
limit: u64,
limit: Option<u64>,
},
GetInvite {
code: &'a str,
Expand Down
6 changes: 4 additions & 2 deletions src/model/user.rs
Expand Up @@ -199,8 +199,10 @@ impl CurrentUser {
let mut pagination = http
.as_ref()
.get_guilds(
&GuildPagination::After(guilds.last().map_or(GuildId(1), |g: &GuildInfo| g.id)),
100,
Some(&GuildPagination::After(
guilds.last().map_or(GuildId(1), |g: &GuildInfo| g.id),
)),
Some(100),
)
.await?;
let len = pagination.len();
Expand Down

0 comments on commit 06d101b

Please sign in to comment.