implement API methods

This commit is contained in:
Denis-Cosmin Nutiu 2025-01-04 16:59:37 +02:00
parent 1918ae9e65
commit 4050a96887
4 changed files with 43 additions and 6 deletions

View file

@ -15,5 +15,5 @@ anyhow = "1.0.95"
serde = { version = "1.0.216", features = ["derive"] } serde = { version = "1.0.216", features = ["derive"] }
base64 = "0.22.1" base64 = "0.22.1"
serde_json = "1.0.134" serde_json = "1.0.134"
reqwest = { version = "0.12.11", features = ["json"] } reqwest = { version = "0.12.11", features = ["json", "multipart"] }
chrono = "0.4.39" chrono = "0.4.39"

View file

@ -1,8 +1,8 @@
use crate::bluesky::BlueSkyClient; use crate::bluesky::BlueSkyClient;
use crate::cli::{CliArgs, Command}; use crate::cli::{CliArgs, Command};
use crate::mastodon::api::{PartialMediaResponse, PartialPostStatusResponse, PostStatusRequest}; use crate::mastodon::api::PostStatusRequest;
use crate::mastodon::MastodonClient; use crate::mastodon::MastodonClient;
use anyhow::{anyhow}; use anyhow::anyhow;
use clap::Parser; use clap::Parser;
use infrastructure::RedisService; use infrastructure::RedisService;
use log::{error, info, warn}; use log::{error, info, warn};

View file

@ -18,6 +18,7 @@ impl MastodonClient {
} }
} }
/// Posts a new status to Mastodon.
pub async fn post_status<T>( pub async fn post_status<T>(
&mut self, &mut self,
data: T, data: T,
@ -25,13 +26,50 @@ impl MastodonClient {
where where
T: Into<PostStatusRequest>, T: Into<PostStatusRequest>,
{ {
unimplemented!() let post_status_request: PostStatusRequest = data.into();
let json = serde_json::to_string(&post_status_request)?;
Ok(self
.client
.post("https://mastodon.social/api/v1/statuses")
.header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", self.access_token))
.body(json)
.send()
.await?
.json()
.await?)
} }
/// Uploads an image to Mastodon.
pub async fn upload_media_by_url( pub async fn upload_media_by_url(
&mut self, &mut self,
image_url: &str, image_url: &str,
) -> Result<PartialMediaResponse, anyhow::Error> { ) -> Result<PartialMediaResponse, anyhow::Error> {
unimplemented!() let data: Vec<u8> = self
.client
.get(image_url)
.send()
.await?
.bytes()
.await?
.to_vec();
let file_part = reqwest::multipart::Part::bytes(data)
.file_name("image.jpg")
.mime_str("image/jpg")?;
let form = reqwest::multipart::Form::new().part("file", file_part);
Ok(self
.client
.post("https://mastodon.social/api/v2/media")
.header("Content-Type", "multipart/form-data")
.header("Authorization", format!("Bearer {}", self.access_token))
.multipart(form)
.send()
.await?
.json()
.await?)
} }
} }

View file

@ -44,7 +44,6 @@ impl From<NewsPost> for PostStatusRequest {
// Push the summary // Push the summary
if character_budget > 0 { if character_budget > 0 {
status.push_str(summary.get(0..character_budget).unwrap_or(summary.as_str())); status.push_str(summary.get(0..character_budget).unwrap_or(summary.as_str()));
character_budget -= summary.len() + 2;
status.push_str("\n") status.push_str("\n")
} }