59 lines
1.3 KiB
Rust
59 lines
1.3 KiB
Rust
|
|
use reqwest::{blocking::multipart, StatusCode};
|
||
|
|
|
||
|
|
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)]
|
||
|
|
enum Visibility {
|
||
|
|
Public,
|
||
|
|
Unlisted,
|
||
|
|
Private,
|
||
|
|
Direct
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Visibility {
|
||
|
|
pub fn enum_name(&self) -> String {
|
||
|
|
match self {
|
||
|
|
Self::Public => "public".to_string(),
|
||
|
|
Self::Unlisted => "unlisted".to_string(),
|
||
|
|
Self::Private => "private".to_string(),
|
||
|
|
Self::Direct => "direct".to_string(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub struct Mastodon {
|
||
|
|
instance: String,
|
||
|
|
token: String
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Mastodon {
|
||
|
|
pub fn new(instance: String, token: String) -> Self {
|
||
|
|
Mastodon { instance, token }
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn post_text_status(&self, status: String, visibility: Visibility) -> Result<(), Error> {
|
||
|
|
let form = multipart::Form::new()
|
||
|
|
.text("status", status)
|
||
|
|
.text("visibility", visibility.enum_name());
|
||
|
|
let client = reqwest::blocking::Client::new();
|
||
|
|
let response = client
|
||
|
|
.post(format!("{}/api/v1/statuses", self.instance))
|
||
|
|
.header("Authorization", format!("Bearer {}", self.token))
|
||
|
|
.multipart(form)
|
||
|
|
.send()?;
|
||
|
|
match response.status() {
|
||
|
|
StatusCode::OK => Ok(()),
|
||
|
|
s => Err(Error::FailureStatus(s))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|