Rewrite awkward code
This commit is contained in:
parent
ed55570646
commit
5c764185bb
|
@ -25,6 +25,6 @@ async fn main() -> Result<()> {
|
|||
fn main() {
|
||||
println!(
|
||||
"examples require the `toml` feature, run this command for this example:\n\ncargo run \
|
||||
--example print_your_profile --features toml\n"
|
||||
--example follows_me --features toml\n"
|
||||
);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
#![cfg_attr(not(feature = "toml"), allow(dead_code))]
|
||||
#![cfg_attr(not(feature = "toml"), allow(unused_imports))]
|
||||
mod register;
|
||||
use mastodon_async::Result;
|
||||
|
||||
#[cfg(feature = "toml")]
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
use futures_util::StreamExt;
|
||||
use mastodon_async::StatusesRequest;
|
||||
|
||||
let mut filters = StatusesRequest::new();
|
||||
filters.limit(3);
|
||||
let mastodon = register::get_mastodon_data().await?;
|
||||
let you = mastodon.verify_credentials().await?;
|
||||
|
||||
mastodon
|
||||
.statuses(&you.id, filters)
|
||||
.await?
|
||||
.items_iter()
|
||||
.for_each(|status| async move { println!("{status:?}") })
|
||||
.await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "toml"))]
|
||||
fn main() {
|
||||
println!(
|
||||
"examples require the `toml` feature, run this command for this example:\n\ncargo run \
|
||||
--example get_statuses --features toml\n"
|
||||
);
|
||||
}
|
|
@ -12,7 +12,7 @@ async fn main() -> Result<()> {
|
|||
.status(register::read_line(
|
||||
"Enter a status to post privately (enter to send): ",
|
||||
)?)
|
||||
.visibility(Visibility::Private)
|
||||
.visibility(Visibility::Unlisted)
|
||||
.language(Language::Eng)
|
||||
.build()?;
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ use serde::{Deserialize, Serialize};
|
|||
/// tokio_test::block_on(async {
|
||||
/// let data = Data::default();
|
||||
/// let client = Mastodon::from(data);
|
||||
/// let statuses = client.statuses("user-id", None).await.unwrap().items_iter();
|
||||
/// let statuses = client.statuses("user-id", Default::default()).await.unwrap().items_iter();
|
||||
/// statuses.for_each(|status| async move {
|
||||
/// // Do something with the status
|
||||
/// }).await;
|
||||
|
|
|
@ -94,11 +94,7 @@ pub use mastodon::{Mastodon, MastodonUnauthenticated};
|
|||
// pub use mastodon_client::{MastodonClient, MastodonUnauthenticated};
|
||||
pub use registration::Registration;
|
||||
pub use requests::{
|
||||
AddFilterRequest,
|
||||
AddPushRequest,
|
||||
StatusesRequest,
|
||||
UpdateCredsRequest,
|
||||
UpdatePushRequest,
|
||||
AddFilterRequest, AddPushRequest, StatusesRequest, UpdateCredsRequest, UpdatePushRequest,
|
||||
};
|
||||
pub use status_builder::{NewStatus, StatusBuilder, Visibility};
|
||||
|
||||
|
|
|
@ -225,7 +225,7 @@ impl Mastodon {
|
|||
/// tokio_test::block_on(async {
|
||||
/// let data = Data::default();
|
||||
/// let client = Mastodon::from(data);
|
||||
/// let statuses = client.statuses("user-id", None).await.unwrap();
|
||||
/// let statuses = client.statuses("user-id", Default::default()).await.unwrap();
|
||||
/// });
|
||||
/// ```
|
||||
///
|
||||
|
@ -239,16 +239,15 @@ impl Mastodon {
|
|||
/// let statuses = client.statuses("user-id", request).await.unwrap();
|
||||
/// });
|
||||
/// ```
|
||||
pub async fn statuses<'a, 'b: 'a, S>(&'b self, id: &'b str, request: S) -> Result<Page<Status>>
|
||||
where
|
||||
S: Into<Option<StatusesRequest<'a>>>,
|
||||
{
|
||||
pub async fn statuses<'a, 'b: 'a>(
|
||||
&'b self,
|
||||
id: &'b str,
|
||||
request: StatusesRequest<'a>,
|
||||
) -> Result<Page<Status>> {
|
||||
let call_id = Uuid::new_v4();
|
||||
let mut url = format!("{}/api/v1/accounts/{}/statuses", self.data.base, id);
|
||||
|
||||
if let Some(request) = request.into() {
|
||||
url = format!("{}{}", url, request.to_querystring()?);
|
||||
}
|
||||
url += request.to_query_string()?.as_str();
|
||||
|
||||
debug!(url = url, method = stringify!($method), call_id = as_debug!(call_id); "making API request");
|
||||
let response = self.client.get(&url).send().await?;
|
||||
|
|
10
src/page.rs
10
src/page.rs
|
@ -1,9 +1,6 @@
|
|||
use super::{Mastodon, Result};
|
||||
use crate::{
|
||||
entities::itemsiter::ItemsIter,
|
||||
format_err,
|
||||
helpers::read_response::read_response,
|
||||
Error,
|
||||
entities::itemsiter::ItemsIter, format_err, helpers::read_response::read_response, Error,
|
||||
};
|
||||
use futures::Stream;
|
||||
use hyper_old_types::header::{parsing, Link, RelationType};
|
||||
|
@ -130,10 +127,7 @@ impl<'a, T: for<'de> Deserialize<'de> + Serialize> Page<T> {
|
|||
})
|
||||
} else {
|
||||
let response = response.json().await?;
|
||||
Err(Error::Api {
|
||||
status,
|
||||
response,
|
||||
})
|
||||
Err(Error::Api { status, response })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ mod bool_qs_serialize {
|
|||
/// use mastodon_async::requests::StatusesRequest;
|
||||
/// let mut request = StatusesRequest::new();
|
||||
/// request.only_media().pinned().since_id("foo");
|
||||
/// assert_eq!(&request.to_querystring().expect("Couldn't serialize qs")[..], "?only_media=1&pinned=1&since_id=foo");
|
||||
/// assert_eq!(&request.to_query_string().expect("Couldn't serialize qs")[..], "?only_media=1&pinned=1&since_id=foo");
|
||||
/// ```
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
|
||||
pub struct StatusesRequest<'a> {
|
||||
|
@ -75,8 +75,9 @@ impl<'a> StatusesRequest<'a> {
|
|||
/// // Example
|
||||
///
|
||||
/// ```
|
||||
/// let mut request = mastodon_async::requests::StatusesRequest::new();
|
||||
/// assert_eq!(&request.only_media().to_querystring().expect("Couldn't serialize qs"), "?only_media=1");
|
||||
/// use mastodon_async::requests::StatusesRequest;
|
||||
/// let mut request = StatusesRequest::new();
|
||||
/// assert_eq!(&request.only_media().to_query_string().expect("Couldn't serialize qs"), "?only_media=1");
|
||||
/// ```
|
||||
pub fn only_media(&mut self) -> &mut Self {
|
||||
self.only_media = true;
|
||||
|
@ -88,11 +89,12 @@ impl<'a> StatusesRequest<'a> {
|
|||
/// // Example
|
||||
///
|
||||
/// ```
|
||||
/// let mut request = mastodon_async::requests::StatusesRequest::new();
|
||||
/// use mastodon_async::requests::StatusesRequest;
|
||||
/// let mut request = StatusesRequest::new();
|
||||
/// assert_eq!(
|
||||
/// &request
|
||||
/// .exclude_replies()
|
||||
/// .to_querystring()
|
||||
/// .to_query_string()
|
||||
/// .expect("Couldn't serialize qs"),
|
||||
/// "?exclude_replies=1"
|
||||
/// );
|
||||
|
@ -107,11 +109,12 @@ impl<'a> StatusesRequest<'a> {
|
|||
/// // Example
|
||||
///
|
||||
/// ```
|
||||
/// let mut request = mastodon_async::requests::StatusesRequest::new();
|
||||
/// use mastodon_async::requests::StatusesRequest;
|
||||
/// let mut request = StatusesRequest::new();
|
||||
/// assert_eq!(
|
||||
/// &request
|
||||
/// .pinned()
|
||||
/// .to_querystring()
|
||||
/// .to_query_string()
|
||||
/// .expect("Couldn't serialize qs"),
|
||||
/// "?pinned=1"
|
||||
/// );
|
||||
|
@ -126,11 +129,12 @@ impl<'a> StatusesRequest<'a> {
|
|||
/// // Example
|
||||
///
|
||||
/// ```
|
||||
/// let mut request = mastodon_async::requests::StatusesRequest::new();
|
||||
/// use mastodon_async::requests::StatusesRequest;
|
||||
/// let mut request = StatusesRequest::new();
|
||||
/// assert_eq!(
|
||||
/// &request
|
||||
/// .max_id("foo")
|
||||
/// .to_querystring()
|
||||
/// .to_query_string()
|
||||
/// .expect("Couldn't serialize qs"),
|
||||
/// "?max_id=foo"
|
||||
/// );
|
||||
|
@ -145,11 +149,12 @@ impl<'a> StatusesRequest<'a> {
|
|||
/// // Example
|
||||
///
|
||||
/// ```
|
||||
/// let mut request = mastodon_async::requests::StatusesRequest::new();
|
||||
/// use mastodon_async::requests::StatusesRequest;
|
||||
/// let mut request = StatusesRequest::new();
|
||||
/// assert_eq!(
|
||||
/// &request
|
||||
/// .since_id("foo")
|
||||
/// .to_querystring()
|
||||
/// .to_query_string()
|
||||
/// .expect("Couldn't serialize qs"),
|
||||
/// "?since_id=foo"
|
||||
/// );
|
||||
|
@ -164,11 +169,12 @@ impl<'a> StatusesRequest<'a> {
|
|||
/// // Example
|
||||
///
|
||||
/// ```
|
||||
/// let mut request = mastodon_async::requests::StatusesRequest::new();
|
||||
/// use mastodon_async::requests::StatusesRequest;
|
||||
/// let mut request = StatusesRequest::new();
|
||||
/// assert_eq!(
|
||||
/// &request
|
||||
/// .limit(10)
|
||||
/// .to_querystring()
|
||||
/// .to_query_string()
|
||||
/// .expect("Couldn't serialize qs"),
|
||||
/// "?limit=10"
|
||||
/// );
|
||||
|
@ -183,11 +189,12 @@ impl<'a> StatusesRequest<'a> {
|
|||
/// // Example
|
||||
///
|
||||
/// ```
|
||||
/// let mut request = mastodon_async::requests::StatusesRequest::new();
|
||||
/// use mastodon_async::requests::StatusesRequest;
|
||||
/// let mut request = StatusesRequest::new();
|
||||
/// assert_eq!(
|
||||
/// &request
|
||||
/// .min_id("foobar")
|
||||
/// .to_querystring()
|
||||
/// .to_query_string()
|
||||
/// .expect("Couldn't serialize qs"),
|
||||
/// "?min_id=foobar"
|
||||
/// );
|
||||
|
@ -197,22 +204,8 @@ impl<'a> StatusesRequest<'a> {
|
|||
self
|
||||
}
|
||||
|
||||
/// Turns this builder into a querystring
|
||||
///
|
||||
/// // Example
|
||||
///
|
||||
/// ```
|
||||
/// let mut request = mastodon_async::requests::StatusesRequest::new();
|
||||
/// assert_eq!(
|
||||
/// &request
|
||||
/// .limit(10)
|
||||
/// .pinned()
|
||||
/// .to_querystring()
|
||||
/// .expect("Couldn't serialize qs"),
|
||||
/// "?pinned=1&limit=10"
|
||||
/// );
|
||||
/// ```
|
||||
pub fn to_querystring(&self) -> Result<String, Error> {
|
||||
/// Serialize into a query string
|
||||
pub fn to_query_string(&self) -> Result<String, Error> {
|
||||
Ok(format!("?{}", serde_qs::to_string(&self)?))
|
||||
}
|
||||
}
|
||||
|
@ -359,13 +352,13 @@ mod tests {
|
|||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_to_querystring() {
|
||||
fn test_to_query_string() {
|
||||
macro_rules! qs_test {
|
||||
(|$r:ident| $b:block, $expected:expr) => {
|
||||
{
|
||||
let mut $r = StatusesRequest::new();
|
||||
$b
|
||||
let qs = $r.to_querystring().expect("Failed to serialize querystring");
|
||||
let qs = $r.to_query_string().expect("Failed to serialize querystring");
|
||||
assert_eq!(&qs, $expected);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue