stack_overflow_client/questions/
mod.rs1use serde::{Deserialize, Serialize};
2
3use crate::{
4 common::{ApiVersion, Response, StackSite, STACK_APP_API},
5 user::User,
6};
7
8#[derive(Debug, Serialize, Deserialize)]
9pub struct Question {
10 pub tagged: Option<String>,
11 pub owner: User,
12 pub is_answered: bool,
13 pub view_count: u32,
14 pub favorite_count: Option<u32>,
15 pub down_vote_count: Option<u32>,
16 pub answer_count: u32,
17 pub score: u32,
18 pub last_activity_date: u32,
19 pub creation_date: u32,
20 pub question_id: u32,
21 pub title: String,
22}
23
24#[derive(Debug, Serialize, Deserialize)]
25pub struct QuestionRequest {
26 pub tag: Option<String>,
27 pub site: StackSite,
28 pub api_version: ApiVersion,
29}
30
31#[deprecated]
32pub fn get_client() -> reqwest::Client {
33 reqwest::Client::builder()
34 .gzip(true)
35 .build()
36 .expect("unable to create client")
37}
38
39#[deprecated]
40pub async fn get_featured_questions(
41 client: &reqwest::Client,
42 req: QuestionRequest,
43) -> Result<Response<Question>, Box<dyn std::error::Error>> {
44 let url = format!(
45 "{}/{}/questions/featured?tagged={}&site={}",
46 STACK_APP_API,
47 req.api_version.to_string(),
48 req.tag.unwrap_or_default(),
49 req.site.to_string()
50 );
51 println!("{:#?}", &url);
52
53 let resp = client.get(&url).send().await?.text().await?;
54 let deserialized: Response<Question> = serde_json::from_str(&resp.as_str())
55 .expect(format!("Failed to deserialized response: {}", resp).as_str());
56 Ok(deserialized)
57}
58
59#[cfg(test)]
60mod tests {
61 use crate::common::Response;
62
63 use super::Question;
64
65 #[test]
66 fn test_parse_response() {
67 let response = "{\"items\":[{\"tags\":[\"docker\",\"docker-compose\",\"ngrok\",\"vapor\"],\"owner\":{\"account_id\":14409195,\"reputation\":2356,\"user_id\":10408494,\"user_type\":\"registered\",\"profile_image\":\"https://i.stack.imgur.com/6Clbj.jpg?s=256&g=1\",\"display_name\":\"Roland Lariotte\",\"link\":\"https://stackoverflow.com/users/10408494/roland-lariotte\"},\"is_answered\":true,\"view_count\":52,\"bounty_amount\":50,\"bounty_closes_date\":1684225877,\"accepted_answer_id\":76224966,\"answer_count\":2,\"score\":2,\"last_activity_date\":1683789088,\"creation_date\":1682626252,\"last_edit_date\":1683635969,\"question_id\":76124274,\"content_license\":\"CC BY-SA 4.0\",\"link\":\"https://stackoverflow.com/questions/76124274/how-can-i-set-ngrok-on-vapor-to-work-locally-with-an-owned-domain\",\"title\":\"How can I set ngrok on Vapor to work locally with an owned domain?\"},{\"tags\":[\"docker\",\"security\",\"github\",\"containers\",\"github-actions\"],\"owner\":{\"account_id\":2319125,\"reputation\":485,\"user_id\":2035417,\"user_type\":\"registered\",\"accept_rate\":75,\"profile_image\":\"https://www.gravatar.com/avatar/be22c7b705fa45820b740d9256e1593e?s=256&d=identicon&r=PG\",\"display_name\":\"ruckc\",\"link\":\"https://stackoverflow.com/users/2035417/ruckc\"},\"is_answered\":false,\"view_count\":160,\"bounty_amount\":50,\"bounty_closes_date\":1683835712,\"answer_count\":1,\"score\":2,\"last_activity_date\":1683555302,\"creation_date\":1666965308,\"question_id\":74236426,\"content_license\":\"CC BY-SA 4.0\",\"link\":\"https://stackoverflow.com/questions/74236426/github-actions-scheduled-container-rebuild-on-latest-release-tag-only\",\"title\":\"GitHub Actions - Scheduled Container Rebuild On Latest Release Tag only\"},{\"tags\":[\"docker\"],\"owner\":{\"account_id\":1312027,\"reputation\":4629,\"user_id\":1259842,\"user_type\":\"registered\",\"accept_rate\":49,\"profile_image\":\"https://www.gravatar.com/avatar/d23f0b2bb6cb82b239c03a31beb625a2?s=256&d=identicon&r=PG&f=y&so-version=2\",\"display_name\":\"miltone\",\"link\":\"https://stackoverflow.com/users/1259842/miltone\"},\"is_answered\":false,\"view_count\":40,\"bounty_amount\":50,\"bounty_closes_date\":1683972718,\"answer_count\":1,\"score\":1,\"last_activity_date\":1683368008,\"creation_date\":1683187049,\"last_edit_date\":1683368008,\"question_id\":76170819,\"content_license\":\"CC BY-SA 4.0\",\"link\":\"https://stackoverflow.com/questions/76170819/docker-compose-build-generate-failed-to-fetch-oauth-token-post-https-auth-do\",\"title\":\"docker-compose build generate failed to fetch oauth token: Post "https://auth.docker.io/token": proxyconnect tcp: dial tcp 127.0.0.1:7080\"},{\"tags\":[\"docker\",\"heroku\",\"docker-compose\",\"fastapi\",\"streamlit\"],\"owner\":{\"account_id\":20813,\"reputation\":21665,\"user_id\":50065,\"user_type\":\"registered\",\"accept_rate\":93,\"profile_image\":\"https://i.stack.imgur.com/ZbEgH.jpg?s=256&g=1\",\"display_name\":\"BioGeek\",\"link\":\"https://stackoverflow.com/users/50065/biogeek\"},\"is_answered\":true,\"view_count\":106,\"bounty_amount\":200,\"bounty_closes_date\":1683883040,\"answer_count\":1,\"score\":2,\"last_activity_date\":1683331610,\"creation_date\":1683031857,\"last_edit_date\":1683278209,\"question_id\":76155032,\"content_license\":\"CC BY-SA 4.0\",\"link\":\"https://stackoverflow.com/questions/76155032/connectionerror-on-multi-docker-app-streamlit-fastapi-when-deployed-on-herok\",\"title\":\"ConnectionError on multi-Docker app (Streamlit + FastAPI) when deployed on Heroku\"}],\"has_more\":false,\"quota_max\":300,\"quota_remaining\":285}";
68 let deseralized: Response<Question> =
69 serde_json::from_str(&response).unwrap();
70 println!("{:#?}", deseralized);
71 assert_eq!(deseralized.has_more, false);
72 let question = deseralized.items.first().unwrap();
73 assert_eq!(question.question_id, 76124274);
74 assert_eq!(question.title, "How can I set ngrok on Vapor to work locally with an owned domain?");
75 assert_eq!(question.view_count, 52);
76 assert_eq!(question.answer_count, 2);
77 }
157}