drop dependency on hyper-old-types in favor of parse_link_header
This commit is contained in:
parent
5832111f46
commit
f037379d0f
|
@ -21,13 +21,15 @@ serde_qs = "0.10.1"
|
||||||
serde_urlencoded = "0.7.1"
|
serde_urlencoded = "0.7.1"
|
||||||
tap-reader = "1"
|
tap-reader = "1"
|
||||||
url = "1"
|
url = "1"
|
||||||
# Provides parsing for the link header in get_links() in page.rs
|
|
||||||
hyper-old-types = "0.11.0"
|
|
||||||
futures-util = "0.3.25"
|
futures-util = "0.3.25"
|
||||||
static_assertions = "1.1.0"
|
static_assertions = "1.1.0"
|
||||||
percent-encoding = "2.2.0"
|
percent-encoding = "2.2.0"
|
||||||
thiserror = "1.0.38"
|
thiserror = "1.0.38"
|
||||||
|
|
||||||
|
[dependencies.parse_link_header]
|
||||||
|
version = "0.3.3"
|
||||||
|
features = ["url"]
|
||||||
|
|
||||||
[dependencies.uuid]
|
[dependencies.uuid]
|
||||||
version = "1.2.2"
|
version = "1.2.2"
|
||||||
features = ["v4"]
|
features = ["v4"]
|
||||||
|
|
|
@ -2,7 +2,6 @@ use std::{error, fmt, io::Error as IoError, num::TryFromIntError};
|
||||||
|
|
||||||
#[cfg(feature = "env")]
|
#[cfg(feature = "env")]
|
||||||
use envy::Error as EnvyError;
|
use envy::Error as EnvyError;
|
||||||
use hyper_old_types::Error as HeaderParseError;
|
|
||||||
use reqwest::{header::ToStrError as HeaderStrError, Error as HttpError, StatusCode};
|
use reqwest::{header::ToStrError as HeaderStrError, Error as HttpError, StatusCode};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::Error as SerdeError;
|
use serde_json::Error as SerdeError;
|
||||||
|
@ -69,8 +68,16 @@ pub enum Error {
|
||||||
#[error("Error converting an http header to a string")]
|
#[error("Error converting an http header to a string")]
|
||||||
HeaderStrError(#[from] HeaderStrError),
|
HeaderStrError(#[from] HeaderStrError),
|
||||||
/// Error parsing the http Link header
|
/// Error parsing the http Link header
|
||||||
#[error("Error parsing the http Link header")]
|
#[error("error parsing http link header")]
|
||||||
HeaderParseError(#[from] HeaderParseError),
|
LinkHeaderParse(#[from] parse_link_header::Error),
|
||||||
|
/// Error returned when an unexpected rel was parsed.
|
||||||
|
#[error("unrecognized rel {rel:?} in link header {link:?}")]
|
||||||
|
UnrecognizedRel {
|
||||||
|
/// The relation which was not recognized
|
||||||
|
rel: String,
|
||||||
|
/// The raw link header
|
||||||
|
link: String,
|
||||||
|
},
|
||||||
#[cfg(feature = "env")]
|
#[cfg(feature = "env")]
|
||||||
/// Error deserializing config from the environment
|
/// Error deserializing config from the environment
|
||||||
#[error("Error deserializing config from the environment")]
|
#[error("Error deserializing config from the environment")]
|
||||||
|
|
40
src/page.rs
40
src/page.rs
|
@ -1,9 +1,6 @@
|
||||||
use super::{Mastodon, Result};
|
use super::{Mastodon, Result};
|
||||||
use crate::{
|
use crate::{entities::itemsiter::ItemsIter, helpers::read_response::read_response, Error};
|
||||||
entities::itemsiter::ItemsIter, format_err, helpers::read_response::read_response, Error,
|
|
||||||
};
|
|
||||||
use futures::Stream;
|
use futures::Stream;
|
||||||
use hyper_old_types::header::{parsing, Link, RelationType};
|
|
||||||
use log::{as_debug, as_serde, debug, error, trace};
|
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};
|
||||||
|
@ -171,30 +168,19 @@ fn get_links(response: &Response, call_id: Uuid) -> Result<(Option<Url>, Option<
|
||||||
|
|
||||||
if let Some(link_header) = response.headers().get(LINK) {
|
if let Some(link_header) = response.headers().get(LINK) {
|
||||||
let link_header = link_header.to_str()?;
|
let link_header = link_header.to_str()?;
|
||||||
|
let raw_link_header = link_header.to_string();
|
||||||
trace!(link_header = link_header, call_id = as_debug!(call_id); "parsing link header");
|
trace!(link_header = link_header, call_id = as_debug!(call_id); "parsing link header");
|
||||||
let link_header = link_header.as_bytes();
|
let link_header = parse_link_header::parse(link_header)?;
|
||||||
let link_header: Link = parsing::from_raw_str(link_header)?;
|
for (rel, link) in link_header.iter() {
|
||||||
for value in link_header.values() {
|
match rel.as_ref().map(|it| it.as_str()) {
|
||||||
if let Some(relations) = value.rel() {
|
Some("next") => next = Some(link.uri.clone()),
|
||||||
if relations.contains(&RelationType::Next) {
|
Some("prev") => prev = Some(link.uri.clone()),
|
||||||
// next = Some(Url::parse(value.link())?);
|
None => debug!(link = as_debug!(link); "link header with no rel specified"),
|
||||||
next = if let Ok(url) = Url::parse(value.link()) {
|
Some(other) => {
|
||||||
trace!(next = url.as_str(), call_id = as_debug!(call_id); "parsed link header");
|
return Err(Error::UnrecognizedRel {
|
||||||
Some(url)
|
rel: other.to_string(),
|
||||||
} else {
|
link: raw_link_header,
|
||||||
// HACK: url::ParseError::into isn't working for some reason.
|
})
|
||||||
return Err(format_err!("error parsing url {:?}", value.link()));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if relations.contains(&RelationType::Prev) {
|
|
||||||
prev = if let Ok(url) = Url::parse(value.link()) {
|
|
||||||
trace!(prev = url.as_str(), call_id = as_debug!(call_id); "parsed link header");
|
|
||||||
Some(url)
|
|
||||||
} else {
|
|
||||||
// HACK: url::ParseError::into isn't working for some reason.
|
|
||||||
return Err(format_err!("error parsing url {:?}", value.link()));
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue