Struct rawr::client::RedditClient
[−]
[src]
pub struct RedditClient { pub client: Client, // some fields omitted }
A client to connect to Reddit. See the module-level documentation for examples.
Fields
client | The internal HTTP client. You should not need to manually use this. If you do, file an issue saying why the API does not support your use-case, and we'll try to add it. |
Methods
impl RedditClient
[src]
fn new(user_agent: &str, authenticator: Arc<Mutex<Box<Authenticator + Send>>>) -> RedditClient
Creates an instance of the RedditClient
using the provided user agent.
fn set_auto_logout(&mut self, val: bool)
Disables the automatic logout that occurs when the client drops out of scope. In the case of OAuth, it will prevent your access token or refresh token from being revoked, though they may expire anyway.
Although not necessary, it is good practice to revoke tokens when you're done with them. This will not affect the client ID or client secret.
Examples
use rawr::client::RedditClient; use rawr::auth::PasswordAuthenticator; let mut client = RedditClient::new("rawr", PasswordAuthenticator::new("a", "b", "c", "d")); client.set_auto_logout(false); // Auto-logout disabled. Set to `true` to enable.
fn ensure_authenticated<F, T>(&self, lambda: F) -> Result<T, APIError> where F: Fn() -> Result<T, APIError>
Runs the lambda passed in. Refreshes the access token if it fails due to an HTTP 401 Unauthorized error, then reruns the lambda. If the lambda fails twice, or fails due to a different error, the error is returned.
fn get_authenticator(&self) -> MutexGuard<Box<Authenticator + Send + 'static>>
Gets a mutable reference to the authenticator using a &RedditClient
. Mainly used
in the ensure_authenticated
method to update tokens if necessary.
fn subreddit(&self, name: &str) -> Subreddit
Provides an interface to the specified subreddit which can be used to access subreddit-related API endpoints such as post listings.
fn user(&self, name: &str) -> User
Gets the specified user in order to get user-related data such as the 'about' page.
fn build_url(&self, dest: &str, oauth_required: bool, authenticator: &mut MutexGuard<Box<Authenticator + Send + 'static>>) -> String
Creates a full URL using the correct access point (API or OAuth) from the stem.
fn get(&self, dest: &str, oauth_required: bool) -> RequestBuilder
Wrapper around the get
function of hyper::client::Client
, which sends a HTTP GET
request. The correct user agent header is also sent using this function, which is necessary
to prevent 403 errors.
fn get_json<T>(&self, dest: &str, oauth_required: bool) -> Result<T, APIError> where T: Deserialize
Sends a GET request with the specified parameters, and returns the resulting deserialized object.
fn post(&self, dest: &str, oauth_required: bool) -> RequestBuilder
Wrapper around the post
function of hyper::client::Client
, which sends a HTTP POST
request. The correct user agent header is also sent using this function, which is necessary
to prevent 403 errors.
fn post_json<T>(&self, dest: &str, body: &str, oauth_required: bool) -> Result<T, APIError> where T: Deserialize
Sends a post request with the specified parameters, and converts the resulting JSON into a deserialized object.
fn post_success(&self, dest: &str, body: &str, oauth_required: bool) -> Result<(), APIError>
Sends a post request with the specified parameters, and ensures that the response has a success header (HTTP 2xx).
fn url_escape(&self, item: String) -> String
URL encodes the specified string so that it can be sent in GET and POST requests.
This is only done when data is being sent that isn't from the API (we assume that API data is safe)
Examples
assert_eq!(client.url_escape(String::from("test&co")), String::from("test%26co")); assert_eq!(client.url_escape(String::from("👍")), String::from("%F0%9F%91%8D"));
fn get_by_id(&self, id: &str) -> LazySubmission
Gets a LazySubmission
object which can be used to access the information/comments of a
specified post. The full name of the item should be used.
Examples
use rawr::prelude::*; let client = RedditClient::new("rawr", AnonymousAuthenticator::new()); let post = client.get_by_id("t3_4uule8").get().expect("Could not get post."); assert_eq!(post.title(), "[C#] Abstract vs Interface");
fn messages(&self) -> MessageInterface
Gets a MessageInterface
object which allows access to the message listings (e.g. inbox
,
unread
, etc.)
Examples
use rawr::prelude::*; let client = RedditClient::new("rawr", PasswordAuthenticator::new("a", "b", "c", "d")); let messages = client.messages(); for message in messages.unread(ListingOptions::default()) { }