Struct Api

Source
pub struct Api {
    pub whoami: Option<Value>,
    /* private fields */
}
Expand description

Reddit API access. This is the struct that you’ll be using to interact with the API.

Fields§

§whoami: Option<Value>

The account’s whoami info

Implementations§

Source§

impl Api

Source

pub fn new(config: Config) -> Self

Create a new API client.

§Arguments
  • config - the configuration
§Examples
let config = Config::load_config().expect("Could not load config");
let mut api = Api::new(config);
Source

pub fn do_login(&mut self) -> Result<(), ApiError>

Uses the values from the config to get an access token from the OAuth endpoint, and stores it in the struct.

This method should be called after creating the struct, and before attempting to query any inforamtion from the API.

§Examples
if let Err(err) = api.do_login() {
    panic!("Could not get an access token: {}", err);
}
Source

pub fn get_whoami(&self) -> Result<Value, ApiError>

Returns the account’s username from the ‘api/v1/me’ endpoint.

§Examples
let whoami = match api.get_whoami() {
    Ok(data) => data,
    Err(err) => panic!(err),
};
Source

pub fn get_username(&self) -> Option<String>

Returns the username from the stored whoami data.

§Examples
let username = match api.get_username() {
    Ok(data) => data,
    Err(err) => panic!(err),
};
Source

pub fn query( &self, method: &str, path: &str, query: Option<Vec<(&str, &str)>>, form_data: Option<HashMap<&str, &str>>, ) -> Result<Response, ApiError>

Query the Reddit API.

§Arguments
  • method - A string representing an HTTP method, capable of being parsed by reqwest, i.e. “GET”, “POST”, etc.
  • path - A relative URL path (everything after reddit.com/)
  • query - An optional collection of query parameters
  • form_data - An optional collection of form data to submit

[reqwest: https://docs.rs/reqwest/latest/reqwest/struct.Method.html#method.from_bytes

§Examples

Get data from an endpoint:

match api.query("GET", "some/endpoint", None, None) {
    Ok(data) => println!("{}", data),
    Err(err) => panic!(err),
};

Post data to an endpoint:

let mut post_data = HashMap::new();
post_data.insert("id", "t3_aaaaaa");
match api.query("POST", "api/save", None, Some(post_data)) {
    Ok(data) => println!("{}", data),
    Err(err) => panic!(err),
}
Source

pub fn query_listing( &self, ql: QueryListingRequest<'_>, ) -> Result<Vec<Value>, ApiError>

Query the Reddit API via a listing endpoint.

Information on listing endpoints can be found in the offial docs.

§Arguments
§Examples
let ql = QueryListingRequest::new("r/rust/hot", 1, 1);
let data: Vec<Value> = api.query_listing(ql).unwrap();
println!("{:?}", data);
Source

pub fn search_for_subreddit( &self, name: &str, ) -> Result<Vec<Subreddit<'_>>, ApiError>

Search for subreddits matching the parameter.

§Arguments
  • name - subreddit (partial) name
§Examples
let names = match api.search_for_subreddit("rust") {
    Ok(names) => names,
    Err(err) => panic!(err),
}
Source

pub fn get_subreddit(&self, name: &str) -> Result<Subreddit<'_>, ApiError>

Get a subreddit by its name.

§Arguments
  • name - subreddit name
§Examples
let subreddit = match api.get_subreddit("rust") {
    Ok(sr) => sr,
    Err(err) => panic!(err),
}
Source

pub fn get_user(&self, name: &str) -> Result<User<'_>, ApiError>

Get a user by their name.

Queries the user’s “about” page to verify valid username.

§Arguments
  • name - username
§Examples
let user = match api.get_user("some-username") {
    Ok(u) => u,
    Err(err) => panic!(err),
}

Auto Trait Implementations§

§

impl Freeze for Api

§

impl !RefUnwindSafe for Api

§

impl Send for Api

§

impl Sync for Api

§

impl Unpin for Api

§

impl !UnwindSafe for Api

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Err = <U as TryFrom<T>>::Err

Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Err>

Source§

impl<T> ErasedDestructor for T
where T: 'static,