summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorScott Boggs <scott@tams.tech>2023-09-29 06:55:04 -0400
committerGitHub <noreply@github.com>2023-09-29 06:55:04 -0400
commit010b958c076f2fce21ab6c9312fcf3d1b9084ea9 (patch)
tree005b7f2d37ee61ba7cf982ddf229e2662e39cf94
parent53b4cc0a3fb8da773d4d61cbc0a02f72fdd7fecd (diff)
parent44234f5f0171c6fc56ad1b2ed5e9416fbacc2801 (diff)
Merge pull request #122 from dscottboggs/feature/add-post-macro
Feature: add post macro
-rw-r--r--src/macros.rs33
-rw-r--r--src/mastodon.rs34
2 files changed, 38 insertions, 29 deletions
diff --git a/src/macros.rs b/src/macros.rs
index 540beb4..c4485e3 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -667,3 +667,36 @@ macro_rules! query_form_route {
)*
};
}
+
+macro_rules! post_route {
+ {
+ $desc:tt
+ [$http_method:ident] $method_name:ident($body_type:ty)@$route:literal -> $return_type:ty,
+ $(
+ $desc_rest:tt
+ [$http_method_rest:ident] $method_name_rest:ident($body_type_rest:ty)@$route_rest:literal -> $return_type_rest:ty,
+ )*
+ } => {
+ #[doc=$desc]
+ pub async fn $method_name(
+ &self,
+ post_body: $body_type,
+ ) -> Result<$return_type> {
+ let url = self.route($route);
+ let response = self
+ .client
+ .$http_method(&url)
+ .json(&post_body)
+ .send()
+ .await?;
+
+ read_response(response).await
+ }
+ $(
+ post_route! {
+ $desc_rest
+ [$http_method_rest] $method_name_rest($body_type_rest)@$route_rest -> $return_type_rest,
+ }
+ )*
+ };
+}
diff --git a/src/mastodon.rs b/src/mastodon.rs
index 1bd82c9..caafce1 100644
--- a/src/mastodon.rs
+++ b/src/mastodon.rs
@@ -184,20 +184,11 @@ impl Mastodon {
read_response(response).await
}
- /// Update the user credentials
- pub async fn update_credentials(
- &self,
- changes: account::CredentialsBuilder,
- ) -> Result<Account> {
- let url = self.route("/api/v1/accounts/update_credentials");
- let response = self
- .client
- .patch(&url)
- .json(&changes.build()?)
- .send()
- .await?;
-
- read_response(response).await
+ post_route! {
+ "Update the user credentials"
+ [patch] update_credentials(account::Credentials)@"/api/v1/accounts/update_credentials" -> Account,
+ "Post a new status to the account."
+ [post] new_status(NewStatus)@"/api/v1/statuses" -> Status,
}
/// Edit existing status
@@ -215,21 +206,6 @@ impl Mastodon {
read_response(response).await
}
- /// Post a new status to the account.
- pub async fn new_status(&self, status: NewStatus) -> Result<Status> {
- let url = self.route("/api/v1/statuses");
- let response = self
- .authenticated(self.client.post(&url))
- .json(&status)
- .send()
- .await?;
- debug!(
- response = as_value!(response, Response),
- "received API response"
- );
- read_response(response).await
- }
-
/// Get timeline filtered by a hashtag(eg. `#coffee`) either locally or
/// federated.
pub async fn get_tagged_timeline(&self, hashtag: String, local: bool) -> Result<Vec<Status>> {