2024-12-28 17:02:22 -05:00
|
|
|
use reqwest::{multipart, StatusCode};
|
2024-12-28 14:02:02 -05:00
|
|
|
|
2024-12-28 17:02:22 -05:00
|
|
|
#[derive(Debug)]
|
2024-12-28 14:02:02 -05:00
|
|
|
pub enum Error {
|
|
|
|
|
ReqwestError(reqwest::Error),
|
|
|
|
|
FailureStatus(reqwest::StatusCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<reqwest::Error> for Error {
|
|
|
|
|
fn from(value: reqwest::Error) -> Self {
|
|
|
|
|
Self::ReqwestError(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
2024-12-28 17:02:22 -05:00
|
|
|
pub enum Visibility {
|
2024-12-28 14:02:02 -05:00
|
|
|
Public,
|
|
|
|
|
Unlisted,
|
|
|
|
|
Private,
|
|
|
|
|
Direct
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Visibility {
|
2024-12-28 21:01:11 -05:00
|
|
|
pub fn enum_name(&self) -> &'static str {
|
2024-12-28 14:02:02 -05:00
|
|
|
match self {
|
2024-12-28 21:01:11 -05:00
|
|
|
Self::Public => "public",
|
|
|
|
|
Self::Unlisted => "unlisted",
|
|
|
|
|
Self::Private => "private",
|
|
|
|
|
Self::Direct => "direct",
|
2024-12-28 14:02:02 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-28 17:02:22 -05:00
|
|
|
#[derive(Clone)]
|
2024-12-28 14:02:02 -05:00
|
|
|
pub struct Mastodon {
|
|
|
|
|
instance: String,
|
|
|
|
|
token: String
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Mastodon {
|
|
|
|
|
pub fn new(instance: String, token: String) -> Self {
|
|
|
|
|
Mastodon { instance, token }
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-28 17:02:22 -05:00
|
|
|
pub async fn post_text_status(&self, status: String, visibility: Visibility) -> Result<(), Error> {
|
2024-12-28 14:02:02 -05:00
|
|
|
let form = multipart::Form::new()
|
|
|
|
|
.text("status", status)
|
|
|
|
|
.text("visibility", visibility.enum_name());
|
2024-12-28 17:02:22 -05:00
|
|
|
let client = reqwest::Client::new();
|
2024-12-28 14:02:02 -05:00
|
|
|
let response = client
|
|
|
|
|
.post(format!("{}/api/v1/statuses", self.instance))
|
|
|
|
|
.header("Authorization", format!("Bearer {}", self.token))
|
|
|
|
|
.multipart(form)
|
2024-12-28 17:02:22 -05:00
|
|
|
.send().await?;
|
2024-12-28 14:02:02 -05:00
|
|
|
match response.status() {
|
|
|
|
|
StatusCode::OK => Ok(()),
|
|
|
|
|
s => Err(Error::FailureStatus(s))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|