fix: next/prev page handling in Page struct
Improve the handling of next and previous pages in the `Page` struct by fixing a bug that caused the URLs to be set to None when a page with no data was loaded. This change allows for the next and previous page URLs to be preserved for future retrieval even when there are no results in the current page.
This commit is contained in:
parent
1cb3b834cf
commit
d4956e943c
42
src/page.rs
42
src/page.rs
|
@ -5,18 +5,24 @@ use log::{as_debug, as_serde, debug, error, trace};
|
||||||
use reqwest::{header::LINK, Response, Url};
|
use reqwest::{header::LINK, Response, Url};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
// use url::Url;
|
|
||||||
|
|
||||||
macro_rules! pages {
|
macro_rules! pages {
|
||||||
($($direction:ident: $fun:ident),*) => {
|
($($direction:ident: $fun:ident),*) => {
|
||||||
|
|
||||||
$(
|
$(
|
||||||
doc_comment!(concat!(
|
doc_comment!(concat!(
|
||||||
"Method to retrieve the ", stringify!($direction), " page of results"),
|
"Method to retrieve the ", stringify!($direction), " page of results",
|
||||||
|
"Returns Ok(None) if there is no data in the ", stringify!($direction), " page.\n",
|
||||||
|
"Returns Ok(Some(Vec<T>)) if there are results.\n",
|
||||||
|
"Returns Err(Error) if there is an error.\n",
|
||||||
|
"If there are results, the next and previous page urls are stored.\n",
|
||||||
|
"If there are no results, the next and previous page urls are not stored.\n",
|
||||||
|
"This allows for the next page to be retrieved in the future even when\n",
|
||||||
|
"there are no results.",
|
||||||
|
),
|
||||||
pub async fn $fun(&mut self) -> Result<Option<Vec<T>>> {
|
pub async fn $fun(&mut self) -> Result<Option<Vec<T>>> {
|
||||||
let url = match self.$direction.take() {
|
let Some(ref url) = self.$direction else {
|
||||||
Some(s) => s,
|
return Ok(None);
|
||||||
None => return Ok(None),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
debug!(
|
debug!(
|
||||||
|
@ -25,22 +31,30 @@ macro_rules! pages {
|
||||||
direction = stringify!($direction);
|
direction = stringify!($direction);
|
||||||
"making API request"
|
"making API request"
|
||||||
);
|
);
|
||||||
let url: String = url.into(); // <- for logging
|
let url: String = url.to_string();
|
||||||
let response = self.mastodon.authenticated(self.mastodon.client.get(&url)).send().await?;
|
let response = self.mastodon.authenticated(self.mastodon.client.get(&url)).send().await?;
|
||||||
match response.error_for_status() {
|
match response.error_for_status() {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
let (prev, next) = get_links(&response, self.call_id)?;
|
let (prev, next) = get_links(&response, self.call_id)?;
|
||||||
let response = read_response(response).await?;
|
let response: Vec<T> = read_response(response).await?;
|
||||||
|
if response.is_empty() && prev.is_none() && next.is_none() {
|
||||||
|
debug!(
|
||||||
|
url = url, method = "get", call_id = as_debug!(self.call_id),
|
||||||
|
direction = stringify!($direction);
|
||||||
|
"received an empty page with no links"
|
||||||
|
);
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
debug!(
|
debug!(
|
||||||
url = url, method = "get", next = as_debug!(next),
|
url = url, method = "get",call_id = as_debug!(self.call_id),
|
||||||
prev = as_debug!(prev), call_id = as_debug!(self.call_id),
|
direction = stringify!($direction),
|
||||||
|
prev = as_debug!(prev),
|
||||||
|
next = as_debug!(next),
|
||||||
response = as_serde!(response);
|
response = as_serde!(response);
|
||||||
"received next pages from API"
|
"received next pages from API"
|
||||||
);
|
);
|
||||||
self.next = next;
|
self.next = next;
|
||||||
self.prev = prev;
|
self.prev = prev;
|
||||||
|
|
||||||
|
|
||||||
Ok(Some(response))
|
Ok(Some(response))
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -91,8 +105,10 @@ macro_rules! pages {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Page<T: for<'de> Deserialize<'de> + Serialize> {
|
pub struct Page<T: for<'de> Deserialize<'de> + Serialize> {
|
||||||
mastodon: Mastodon,
|
mastodon: Mastodon,
|
||||||
next: Option<Url>,
|
/// next url
|
||||||
prev: Option<Url>,
|
pub next: Option<Url>,
|
||||||
|
/// prev url
|
||||||
|
pub prev: Option<Url>,
|
||||||
/// Initial set of items
|
/// Initial set of items
|
||||||
pub initial_items: Vec<T>,
|
pub initial_items: Vec<T>,
|
||||||
pub(crate) call_id: Uuid,
|
pub(crate) call_id: Uuid,
|
||||||
|
|
Loading…
Reference in New Issue