[−][src]Struct redbot::Api
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
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
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
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
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]
&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 parametersform_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), }
pub fn query_listing(
&self,
ql: QueryListingRequest
) -> Result<Vec<Value>, ApiError>
[src]
&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
ql
- AQueryListingRequest
struct
Examples
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]
&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), }
pub fn get_subreddit(&self, name: &str) -> Result<Subreddit, ApiError>
[src]
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), }
pub fn get_user(&self, name: &str) -> Result<User, ApiError>
[src]
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Erased for T
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
U: TryFrom<T>,