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
impl Api
Sourcepub fn do_login(&mut self) -> Result<(), ApiError>
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);
}
Sourcepub fn get_whoami(&self) -> Result<Value, ApiError>
pub fn get_whoami(&self) -> Result<Value, ApiError>
Sourcepub fn get_username(&self) -> Option<String>
pub fn get_username(&self) -> Option<String>
Sourcepub fn query(
&self,
method: &str,
path: &str,
query: Option<Vec<(&str, &str)>>,
form_data: Option<HashMap<&str, &str>>,
) -> Result<Response, ApiError>
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 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),
}
Sourcepub fn query_listing(
&self,
ql: QueryListingRequest<'_>,
) -> Result<Vec<Value>, ApiError>
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
ql
- AQueryListingRequest
struct
§Examples
ⓘ
let ql = QueryListingRequest::new("r/rust/hot", 1, 1);
let data: Vec<Value> = api.query_listing(ql).unwrap();
println!("{:?}", data);
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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