use crate::client::XClient;
use crate::types::{TweetRequest, TweetResponse, XResult};
use reqwest::Method;
pub struct Tweets<'a> {
client: &'a XClient,
}
impl<'a> Tweets<'a> {
pub(crate) fn new(client: &'a XClient) -> Self {
Self { client }
}
pub async fn post(&self, request: TweetRequest) -> XResult<TweetResponse> {
let url = format!("{}/2/tweets", self.client.base_url());
let body = serde_json::to_string(&request)?;
let http_request = self
.client
.http_client()
.request(Method::POST, &url)
.header("Content-Type", "application/json")
.body(body)
.build()?;
let response = self.client.execute(http_request).await?;
let tweet_response: TweetResponse = response.json().await?;
Ok(tweet_response)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::auth::Credentials;
fn test_client() -> XClient {
let credentials = Credentials::new("key", "secret", "token", "token_secret");
XClient::new(credentials).unwrap()
}
#[test]
fn test_tweets_endpoint_creation() {
let client = test_client();
let _tweets = client.tweets();
}
}