Rust Documentation Crate

What is this?

WIP Rust bindings for the Reddit API

This is a WIP, you likely won't find it particularly useful

Set up

Follow https://github.com/reddit-archive/reddit/wiki/OAuth2 for set up instructions. You can use https://tobymurray.github.io/reddit-auth-generator/ to help generate an auth string.

Use

Set up a script with access to a Reddit account, collect the access token, the client ID, and the client secret. Once you have that, get a refresh token. Once you have that you can do:

// pants is mutable so the refresh token can be updated let mut pants = Pants::new( USER_AGENT, "<access-token>", "<refresh_token>", "<client-id>", "<client-secret>", ); For example, if you're using dotenv and reading values from the environment: // pants is mutable so the refresh token can be updated let mut pants = Pants::new( USER_AGENT, env::var("ACCESS_TOKEN").unwrap(), &env::var("REFRESH_TOKEN").unwrap(), &env::var("CLIENT_ID").unwrap(), &env::var("CLIENT_SECRET").unwrap(), ); Then you can invoke things, e.g:

pants.me()

If your refresh token expires, it should automatically refresh.

Currently implemented with (partially) structured response:

Account: - GET /api/v1/me - GET /api/v1/me/karma - GET /api/v1/me/prefs - GET /prefs/friends

Currently kind of implemented (no query parameters), with JSON response:

Account: - GET /api/v1/me/trophies - GET /prefs/blocked - GET /prefs/messaging - GET /prefs/trusted - GET /api/v1/me/friends - GET /api/v1/me/blocked

Listing

Links and Comments - POST /api/submit - POST /api/del

To submit a post to Reddit:

``` // Build the submission let requestbody = linksandcomments::ApiSubmit { ad: "".tostring(), apitype: "".tostring(), app: "".tostring(), collectionid: "".tostring(), eventend: "".tostring(), eventstart: "".tostring(), eventtz: "".tostring(), extension: "".tostring(), flairid: "".tostring(), flairtext: "".tostring(), grecaptcharesponse: "".tostring(), kind: "self".tostring(), nsfw: "".tostring(), resubmit: "".tostring(), richtextjson: "".tostring(), sendreplies: "".tostring(), spoiler: "".tostring(), sr: "nameofsubreddit".tostring(), text: "Here's an example of the post's body".tostring(), title: "This is the title of the post".tostring(), uh: "".tostring(), url: "".tostring(), videoposterurl: "".tostring(), };

// then submit the post let submissionname = pants.submit(requestbody).await { Ok(response) => { println!("Response to submit is: {}", serdejson::tostring_pretty(&response).unwrap()); response.json.data.name }, Err(e) => panic!("An error ocurred: {}", e), };

// remove it if you'd like let deleterequestbody = linksandcomments::ApiDel { id: submission_name };

pants.del(deleterequestbody).await; ```

Streaming support for:

Disclaimer: This implementation of streaming is not compatible with very high traffic subreddits. If more than 25 posts are submitted within any 30 second period, this streaming method will miss some.

``` use futuresutil::pinmut; use futuresutil::stream::StreamExt; ... let stream = pants.streamsubredditnew("testingground4bots"); pinmut!(stream);

while let Some(value) = tokiotest::blockon(stream.next()) { match value { Ok(data) => { println!("New post: {}", data); } Err(e) => { // Note, this can get noisy if the failure persists println!("Encountered an error: {}", e); } } } ```

All other APIs are not implemented