2025-11-12 15:10:38 -05:00
|
|
|
use reqwest::{blocking::{Client, 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,
|
2025-11-12 15:10:38 -05:00
|
|
|
token: String,
|
2024-12-28 14:02:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Mastodon {
|
|
|
|
|
pub fn new(instance: String, token: String) -> Self {
|
2025-11-12 15:10:38 -05:00
|
|
|
Mastodon {
|
|
|
|
|
instance,
|
|
|
|
|
token,
|
|
|
|
|
|
|
|
|
|
}
|
2024-12-28 14:02:02 -05:00
|
|
|
}
|
|
|
|
|
|
2025-11-12 15:10:38 -05:00
|
|
|
pub 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());
|
2025-11-12 15:10:38 -05:00
|
|
|
let client = reqwest::blocking::Client::builder()
|
|
|
|
|
.danger_accept_invalid_certs(true)
|
|
|
|
|
.build()
|
|
|
|
|
.expect("");
|
|
|
|
|
let response = client
|
2024-12-28 14:02:02 -05:00
|
|
|
.post(format!("{}/api/v1/statuses", self.instance))
|
|
|
|
|
.header("Authorization", format!("Bearer {}", self.token))
|
|
|
|
|
.multipart(form)
|
2025-11-12 15:10:38 -05:00
|
|
|
.send()?;
|
2024-12-28 14:02:02 -05:00
|
|
|
match response.status() {
|
|
|
|
|
StatusCode::OK => Ok(()),
|
|
|
|
|
s => Err(Error::FailureStatus(s))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|