[][src]Struct redbot::Api

pub struct Api {
    pub whoami: Option<Value>,
    // some fields omitted
}

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

Methods

impl Api[src]

pub fn new(config: Config) -> Self[src]

Create a new API client.

Arguments

  • config - the configuration

Examples

This example is not tested
let config = Config::load_config().expect("Could not load config");
let mut api = Api::new(config);

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

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

This example is not tested
if let Err(err) = api.do_login() {
    panic!("Could not get an access token: {}", err);
}

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

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

Examples

This example is not tested
let whoami = match api.get_whoami() {
    Ok(data) => data,
    Err(err) => panic!(err),
};

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

Returns the username from the stored whoami data.

Examples

This example is not tested
let username = match api.get_username() {
    Ok(data) => data,
    Err(err) => panic!(err),
};

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

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:

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

Post data to an endpoint:

This example is not tested
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),
}

pub fn query_listing(
    &self,
    ql: QueryListingRequest
) -> Result<Vec<Value>, ApiError>
[src]

Query the Reddit API via a listing endpoint.

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

Arguments

Examples

This example is not tested
let ql = QueryListingRequest::new("r/rust/hot", 1, 1);
let data: Vec<Value> = api.query_listing(ql).unwrap();
println!("{:?}", data);

pub fn search_for_subreddit(
    &self,
    name: &str
) -> Result<Vec<Subreddit>, ApiError>
[src]

Search for subreddits matching the parameter.

Arguments

  • name - subreddit (partial) name

Examples

This example is not tested
let names = match api.search_for_subreddit("rust") {
    Ok(names) => names,
    Err(err) => panic!(err),
}

pub fn get_subreddit(&self, name: &str) -> Result<Subreddit, ApiError>[src]

Get a subreddit by its name.

Arguments

  • name - subreddit name

Examples

This example is not tested
let subreddit = match api.get_subreddit("rust") {
    Ok(sr) => sr,
    Err(err) => panic!(err),
}

pub fn get_user(&self, name: &str) -> Result<User, ApiError>[src]

Get a user by their name.

Queries the user's "about" page to verify valid username.

Arguments

  • name - username

Examples

This example is not tested
let user = match api.get_user("some-username") {
    Ok(u) => u,
    Err(err) => panic!(err),
}

Auto Trait Implementations

impl Send for Api

impl Sync for Api

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Erased for T

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

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