WIP Rust bindings for the Reddit API
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.
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:
``` use futuresutil::pinmut; use futuresutil::stream::StreamExt; ... let stream = pants.streamsubredditnew("testingground4bots"); pinmut!(stream);
while let Some(value) = tokiotest::blockon(stream.next()) { println!("New post: {}", value); } ```
All other APIs are not implemented
Should things go sideways, the response bodies are logged at a trace log level. For example, using fern:
fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"{}[{}][{}] {}",
chrono::Local::now().format("[%Y-%m-%d][%H:%M:%S]"),
record.target(),
record.level(),
message
))
})
.level(log::LevelFilter::Trace) // This has to be trace level
.chain(std::io::stdout())
.chain(fern::log_file("output.log")?)
.apply()?;