1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use std::time::SystemTime;

use axum::{
    extract::{Query, State},
    Json,
};
use lipsum::lipsum;
use log::info;
use log_derive::logfn;
use reqwest::StatusCode;
use serde_json::{json, Value};

use crate::{
    app_error::AppError,
    reddit::{model::listing::KindContainer, request::RedditRequest},
    AppState,
};

use super::AnyParams;

/// Returns after a specified delay.
#[utoipa::path(
    get,
    path = "/api/debug/timeout",
    responses(
        (status = 200, description = "Timed out successfully"),
        (status = 400, description = "No parameter provided")
    ),
    params(
        ("t" = u64, description = "Time to wait in seconds", nullable = false)
    )
)]
#[logfn(err = "ERROR", fmt = "'timeout' failed: {:?}")]
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"))?
        .parse::<u64>()
        .unwrap();
    tokio::time::sleep(tokio::time::Duration::from_secs(t)).await;
    Ok(json!({
        "t" : t
    })
    .into())
}

/// Generates a random lorem ipsum text with a specified number of words.
/// Optionally waits for a specified time before responding.
#[utoipa::path(
    get,
    path = "/api/debug/lorem",
    responses(
        (status = 200, description = "Timed out successfully, returned lorem ipsum text"),
        (status = 400, description = "Invalid request")
    ),
    params(
        ("words" = usize, description = "Number of words to generate", nullable = true),
        ("t" = u64, description = "Time to wait in seconds", nullable = true)
    )
)]
#[logfn(err = "ERROR", fmt = "'lorem' failed: {:?}")]
pub async fn lorem(Query(params): Query<AnyParams>) -> Result<Json<Value>, AppError> {
    let words = params
        .get("words")
        .unwrap_or(&"50".to_string())
        .parse::<usize>()
        .unwrap();
    let t = params
        .get("t")
        .unwrap_or(&"0".to_string())
        .parse::<u64>()
        .unwrap();
    tokio::time::sleep(tokio::time::Duration::from_secs(t)).await;

    Ok(json!({
        "message": lipsum(words),
        "t": t
    })
    .into())
}

#[utoipa::path(get, path = "/api/debug/subreddit_info", responses(), params())]
pub async fn subreddit_info(
    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 `subreddit` parameter"))?;
    let req = RedditRequest::SubredditInfo {
        subreddit: subreddit.to_string(),
    };
    let json = state.reddit.fetch_raw(req).await?;

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

    Ok(Json(info))
}

#[utoipa::path(get, path = "/api/debug/post_comments", responses(), params())]
pub async fn post_comments(
    State(mut state): State<AppState>,
    Query(params): Query<AnyParams>,
) -> Result<Json<Vec<KindContainer>>, AppError> {
    let r = params
        .get("r")
        .ok_or_else(|| AppError::new(StatusCode::BAD_REQUEST, "Missing `subreddit` parameter"))?
        .to_string();

    let id = params
        .get("id")
        .ok_or_else(|| AppError::new(StatusCode::BAD_REQUEST, "Missing `id` parameter"))?
        .to_string();

    let req = RedditRequest::PostComments {
        subreddit: r,
        post_id: id,
        params: Default::default(),
    };

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

    let start = SystemTime::now();
    let val = serde_json::from_value(json).unwrap();
    let elapsed = SystemTime::now().duration_since(start).unwrap();
    info!("Data parsed successfully. Took {:?}", elapsed);
    Ok(Json(val))
}

#[utoipa::path(get, path = "/api/debug/user_info", responses(), params())]
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 {
        username: user.to_string(),
    };

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

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

    Ok(Json(info))
}

#[utoipa::path(get, path = "/api/debug/subreddit_posts", responses(), params())]
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: subreddit.into(),
        params: Default::default(),
    };
    let json = state.reddit.fetch_raw(req).await?;

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

    Ok(Json(info))
}

#[utoipa::path(get, path = "/api/debug/user_posts", responses(), params())]
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 {
        username: user.into(),
        params: Default::default(),
    };
    let json = state.reddit.fetch_raw(req).await?;

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

    Ok(Json(info))
}