twapi-v2

Twitter API v2 library

32 releases (14 breaking)

0.15.1 Jun 21, 2024
0.14.2 Apr 3, 2024
0.14.0 Mar 26, 2024
0.9.0 Dec 16, 2023
0.5.9 May 10, 2023
Download history 3049/week @ 2024-07-08 841/week @ 2024-07-15 177/week @ 2024-07-22 334/week @ 2024-07-29 405/week @ 2024-08-05 730/week @ 2024-08-12 342/week @ 2024-08-19 355/week @ 2024-08-26 283/week @ 2024-09-02 252/week @ 2024-09-09 381/week @ 2024-09-16 384/week @ 2024-09-23 318/week @ 2024-09-30 175/week @ 2024-10-07 112/week @ 2024-10-14 241/week @ 2024-10-21

860 downloads per month

MIT license

605KB
18K SLoC

twapi-v2-rs

Twitter API v2 library.

Documentation

  • Request builder
  • Retrive response headers
  • Convenience setted parameter methods
  • Bearer authentication(OAuth 2.0 Authorization Code Flow with PKCE)
  • OAuth1.0a authentication(OAuth 1.0a User Contex)
  • Upload Media upload.twitter.com APIs
  • Optional retriable and timeout and logging
  • Optional OAuth with web example
  • Optional v1 to v2 parser
  • Streaming example
  • Supported mocks. For example, mockito.
  • Type support.

Features

default

  • reqwest/default-tls

rustls-tls

  • reqwest/rustls-tls

retry

  • Retriable
  • Timeout
  • Logging

oauth

  • Twitter OAuth

oauth10a

  • Use api by OAuth1.0a

models

  • From v1 to v2

upload

  • Upload Media

Changes

CHANGELOG.md

Test status

TEST.md

Examples

API(bearer)

use twapi_v2::api::{get_2_tweets_id, BearerAuthentication};

#[tokio::main]
async fn main() {
    let bearer_code = std::env::var("BEARER_CODE").unwrap();
    let auth = BearerAuthentication::new(bearer_code);
    let tweet_id = std::env::var("TWEET_ID").unwrap();
    let res = get_2_tweets_id::Api::open(&tweet_id)
        .execute(&auth)
        .await;
    if let Some((val, headers)) = res {
        println!("{:?}", val);
        println!("{}", headers);
    }
}

API(OAuth1.0a)

use twapi_v2::api::{get_2_tweets_id, BearerAuthentication};
use twapi_v2::oauth10a::OAuthAuthentication;

#[tokio::main]
async fn main() {
    let auth = OAuthAuthentication::new(
        std::env::var("CONSUMER_KEY").unwrap_or_default(),
        std::env::var("CONSUMER_SECRET").unwrap_or_default(),
        std::env::var("ACCESS_KEY").unwrap_or_default(),
        std::env::var("ACCESS_SECRET").unwrap_or_default(),
    );
    let tweet_id = std::env::var("TWEET_ID").unwrap();
    let res = get_2_tweets_id::Api::open(&tweet_id)
        .execute(&auth)
        .await;
    if let Some((val, headers)) = res {
        println!("{:?}", val);
        println!("{}", headers);
    }
}

V1 parse

use std::{fs::File, io::{Read, Write}};
use twapi_v2::models::TweetModel;

#[tokio::main]
async fn main() {
    let mut file = File::open("samples/v1_tweet.json").unwrap();
    let mut data = String::new();
    file.read_to_string(&mut data).unwrap();
    let src = serde_json::from_str::<serde_json::Value>(&data).unwrap();
    let res = TweetModel::from_v1(&src);
    let mut file = File::create("result.json").unwrap();
    write!(file, "{}", serde_json::to_string_pretty(&res).unwrap()).unwrap();
    file.flush().unwrap();
}

Upload Media

cd examples/post-media
CONSUMER_KEY=xxx \
CONSUMER_SECRET=xxx \
ACCESS_KEY=xxx \
ACCESS_SECRET=xxx \
cargo run

Twitter OAuth Web

cd examples/oauth-web
API_KEY_CODE=XXXX API_SECRET_CODE=XXXX CALLBACK_URL=http://localhost:3000/oauth cargo run

http://localhost:3000/

Streaming

cd examples/streaming
BEARER_CODE=XXXXX cargo run

Mock(Use mockito)

#[tokio::test]
async fn test_mock_get_2_tweets_search_recent_oauth() -> Result<()> {
    // Setup mock server
    let mut server = Server::new_async().await;
    let mock = server
        .mock("GET", "/2/tweets/search/recent")
        .match_query(mockito::Matcher::Any)
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body("{ \"origin\": \"0.0.0.0\" }")
        .create_async()
        .await;

    // Setup OAuth
    let auth = OAuthAuthentication::new(
        std::env::var("CONSUMER_KEY").unwrap_or_default(),
        std::env::var("CONSUMER_SECRET").unwrap_or_default(),
        std::env::var("ACCESS_KEY").unwrap_or_default(),
        std::env::var("ACCESS_SECRET").unwrap_or_default(),
    );

    // Setup prefix all APIs
    api::setup_prefix_url(&server.url());
    let builder = get_2_tweets_search_recent::Api::open("東京")
        .max_results(10)
        .build(&auth);
    let (res, _headers) = execute_twitter::<get_2_tweets_search_recent::Response>(builder).await?;
    assert_eq!(res.extra.get("origin"), Some(&json!("0.0.0.0")));
    mock.assert();

    // Clear prefix url
    api::clear_prefix_url();
    // Override prefix url
    let twapi_options = TwapiOptions {
        prefix_url: Some(server.url().clone())
    };
    let builder = get_2_tweets_search_recent::Api::open("東京")
        .max_results(10)
        .twapi_options(Some(twapi_options))
        .build(&auth);
    let (res, _headers) = execute_twitter::<get_2_tweets_search_recent::Response>(builder).await?;
    assert_eq!(res.extra.get("origin"), Some(&json!("0.0.0.0")));

    Ok(())
}

Dependencies

~5–19MB
~310K SLoC