Skip to content

Commit

Permalink
Add all debug endpoints (#63)
Browse files Browse the repository at this point in the history
* Read Reddit API ratelimit headers and log info. Remove dead code

* Update api/test/subreddit-info to return parsed data

* Add user info debug endpoint

* Add subreddit posts debug endpoint

* Add user posts debug endpoint
  • Loading branch information
m-milek authored Jul 24, 2024
1 parent e64b551 commit c568efb
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 46 deletions.
13 changes: 7 additions & 6 deletions backend/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::collections::HashMap;

use axum::{routing::get, Router};

use crate::AppState;
use axum::{routing::get, Router};
use std::collections::HashMap;

pub mod test;

type PlainParams = HashMap<String, String>;
type AnyParams = HashMap<String, String>;

pub fn router() -> Router<AppState> {
Router::<AppState>::new()
.route("/test/timeout", get(test::timeout))
.route("/test/lorem", get(test::lorem))
.route("/test/subreddit-info", get(test::subreddit_info))
.route("/test/subreddit-comments", get(test::subreddit_comments))
.route("/test/post-comments", get(test::post_comments))
.route("/test/user-info", get(test::user_info))
.route("/test/subreddit-posts", get(test::subreddit_posts))
.route("/test/user-posts", get(test::user_posts))
}
74 changes: 57 additions & 17 deletions backend/src/api/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,18 @@ use axum::{
Json,
};
use lipsum::lipsum;
use log::{info, warn};
use log::info;
use log_derive::logfn;
use reqwest::StatusCode;
use serde_json::{json, Value};

use crate::{
app_error::AppError,
reddit::{
model::{
listing::KindContainer,
subreddit_info::{RedditSubredditInfo, RedditSubredditInfoRaw},
},
RedditRequest,
},
reddit::{model::listing::KindContainer, RedditRequest},
AppState,
};

use super::PlainParams;
use super::AnyParams;

/// Returns after a specified delay.
#[utoipa::path(
Expand All @@ -37,7 +31,7 @@ use super::PlainParams;
)
)]
#[logfn(err = "ERROR", fmt = "'timeout' failed: {:?}")]
pub async fn timeout(Query(params): Query<PlainParams>) -> Result<Json<Value>, AppError> {
pub async fn timeout(Query(params): Query<AnyParams>) -> Result<Json<Value>, AppError> {
let t = params
.get("t")
.ok_or_else(|| AppError::new(StatusCode::BAD_REQUEST, "Missing `t` parameter"))?
Expand Down Expand Up @@ -65,7 +59,7 @@ pub async fn timeout(Query(params): Query<PlainParams>) -> Result<Json<Value>, A
)
)]
#[logfn(err = "ERROR", fmt = "'lorem' failed: {:?}")]
pub async fn lorem(Query(params): Query<PlainParams>) -> Result<Json<Value>, AppError> {
pub async fn lorem(Query(params): Query<AnyParams>) -> Result<Json<Value>, AppError> {
let words = params
.get("words")
.unwrap_or(&"50".to_string())
Expand All @@ -87,22 +81,22 @@ pub async fn lorem(Query(params): Query<PlainParams>) -> Result<Json<Value>, App

pub async fn subreddit_info(
State(mut state): State<AppState>,
Query(params): Query<PlainParams>,
) -> Result<Json<RedditSubredditInfo>, AppError> {
Query(params): Query<AnyParams>,
) -> Result<Json<KindContainer>, AppError> {
let subreddit = params
.get("r")
.ok_or_else(|| AppError::new(StatusCode::BAD_REQUEST, "Missing `subreddit` parameter"))?;
let req = RedditRequest::SubredditInfo(subreddit.into());
let json = state.reddit.fetch_raw(req).await?;

let info = serde_json::from_value::<RedditSubredditInfoRaw>(json).unwrap();
let info = serde_json::from_value::<KindContainer>(json).unwrap();

Ok(Json(info.data))
Ok(Json(info))
}

pub async fn subreddit_comments(
pub async fn post_comments(
State(mut state): State<AppState>,
Query(params): Query<PlainParams>,
Query(params): Query<AnyParams>,
) -> Result<Json<Vec<KindContainer>>, AppError> {
let r = params
.get("r")
Expand All @@ -127,3 +121,49 @@ pub async fn subreddit_comments(
info!("Data parsed successfully. Took {:?}", elapsed);
Ok(Json(val))
}

pub async fn user_info(
State(mut state): State<AppState>,
Query(params): Query<AnyParams>,
) -> Result<Json<KindContainer>, AppError> {
let user = params
.get("u")
.ok_or_else(|| AppError::new(StatusCode::BAD_REQUEST, "Missing `u` parameter"))?;
let req = RedditRequest::UserInfo(user.into());

let json = state.reddit.fetch_raw(req).await?;

let info = serde_json::from_value::<KindContainer>(json).unwrap();

Ok(Json(info))
}

pub async fn subreddit_posts(
State(mut state): State<AppState>,
Query(params): Query<AnyParams>,
) -> Result<Json<KindContainer>, AppError> {
let subreddit = params
.get("r")
.ok_or_else(|| AppError::new(StatusCode::BAD_REQUEST, "Missing `r` parameter"))?;
let req = RedditRequest::SubredditPosts(subreddit.into());
let json = state.reddit.fetch_raw(req).await?;

let info = serde_json::from_value::<KindContainer>(json).unwrap();

Ok(Json(info))
}

pub async fn user_posts(
State(mut state): State<AppState>,
Query(params): Query<AnyParams>,
) -> Result<Json<KindContainer>, AppError> {
let user = params
.get("u")
.ok_or_else(|| AppError::new(StatusCode::BAD_REQUEST, "Missing `u` parameter"))?;
let req = RedditRequest::UserPosts(user.into());
let json = state.reddit.fetch_raw(req).await?;

let info = serde_json::from_value(json).unwrap();

Ok(Json(info))
}
24 changes: 22 additions & 2 deletions backend/src/reddit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,29 @@ impl RedditConnection {
.build()?;

let start = SystemTime::now();
let res = self.http.execute(req).await?.json().await?;
let res = self.http.execute(req).await?;
let elapsed = SystemTime::now().duration_since(start).unwrap();

let ratelimit_headers = const {
[
"x-ratelimit-remaining",
"x-ratelimit-reset",
"x-ratelimit-used",
]
};
let header_log = &res
.headers()
.iter()
.filter(|h| ratelimit_headers.contains(&h.0.as_str()))
.fold(String::from("\n"), |acc, (k, v)| {
let s = format!("{}: {}\n", k, v.to_str().unwrap());
acc + s.as_str()
});

info!("Headers: {}", header_log.trim_end());
info!("Data fetched successfully. Took {:?}", elapsed);
Ok(res)

let json = res.json().await?;
Ok(json)
}
}
4 changes: 3 additions & 1 deletion backend/src/reddit/model/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ pub struct RedditComment {
/// UNIX timestamp of the comment creation
created_utc: f32,
/// Depth of the comment in the thread. 0 is the top-level comment, 1 is a reply to the top-level comment, etc.
depth: u32,
///
/// When fetching posts/comments from a user, this field is always `None`.
depth: Option<u32>,
/// Upvotes - downvotes
score: i64,
}
10 changes: 5 additions & 5 deletions backend/src/reddit/model/listing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{comment::RedditComment, post::RedditPost};
use super::{comment::RedditComment, post::RedditPost, subreddit_info::RedditSubredditInfo, user::RedditUser};
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand All @@ -15,19 +15,19 @@ pub enum KindContainer {
T1(Box<RedditComment>), // Comment

#[serde(rename = "t2")]
T2(Value), // Account TODO
T2(Box<RedditUser>), // Account TODO

#[serde(rename = "t3")]
T3(Box<RedditPost>), // Link/Post

#[serde(rename = "t4")]
T4(Value), // Message TODO
T4(Box<Value>), // Message TODO

#[serde(rename = "t5")]
T5(Value), // Subreddit TODO
T5(Box<RedditSubredditInfo>), // Subreddit Info

#[serde(rename = "t6")]
T6(Value), // Award TODO
T6(Box<Value>), // Award TODO
}

/// List of IDs of items to fetch to get the ones that didn't fit in the first response.
Expand Down
6 changes: 0 additions & 6 deletions backend/src/reddit/model/subreddit_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
use derive_getters::Getters;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedditSubredditInfoRaw {
pub kind: String,
pub data: RedditSubredditInfo
}

/// Reddit subreddit data
/// https://www.reddit.com/r/Polska/about.json
#[derive(Getters, Debug, PartialEq, Clone, Serialize, Deserialize)]
Expand Down
9 changes: 0 additions & 9 deletions backend/src/reddit/reddit_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ pub enum RedditError {
#[error("Domain '{0}' is not a valid Reddit API domain")]
InvalidDomain(String),

#[error("Failed to read credentials file. It should be placed in the same dir as Cargo.toml")]
CredentialsNotFound,

#[error("Malformed .reddit-credentials.json file. Please make sure it's valid JSON")]
MalformedCredentials,

#[error("Credentials file contains no credentials in the array")]
NoClientsInCredentials,

#[error("HTTP Error: `{0}`")]
HttpError(#[from] reqwest::Error),

Expand Down

0 comments on commit c568efb

Please sign in to comment.