thoth_client/
work.rs

1use std::fmt;
2
3use chrono::naive::NaiveDate;
4use graphql_client::{GraphQLQuery, Response};
5use thoth_api::errors::ThothError;
6use uuid::Uuid;
7
8#[derive(GraphQLQuery)]
9#[graphql(
10    schema_path = "assets/schema.json",
11    query_path = "assets/work_query.graphql",
12    response_derives = "Debug"
13)]
14pub struct WorkQuery;
15
16pub async fn get_work(
17    work_id: Uuid,
18    thoth_url: String,
19) -> Result<work_query::WorkQueryWork, ThothError> {
20    let request_body = WorkQuery::build_query(work_query::Variables { work_id });
21    let client = reqwest::Client::new();
22    let res = client.post(&thoth_url).json(&request_body).send().await?;
23    let response_body: Response<work_query::ResponseData> = res.json().await?;
24    match response_body.data {
25        Some(data) => {
26            if let Some(errors) = response_body.errors {
27                println!("there are errors:");
28                for error in &errors {
29                    println!("{:?}", error);
30                }
31            }
32            Ok(data.work)
33        }
34        _ => Err(ThothError::InternalError("Query failed".to_string())),
35    }
36}
37
38impl fmt::Display for work_query::LanguageCode {
39    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40        write!(f, "{:?}", self)
41    }
42}
43
44#[derive(GraphQLQuery)]
45#[graphql(
46    schema_path = "assets/schema.json",
47    query_path = "assets/works_query.graphql",
48    response_derives = "Debug"
49)]
50pub struct WorksQuery;
51
52pub async fn get_works(thoth_url: String) -> Result<Vec<works_query::WorksQueryWorks>, ThothError> {
53    let request_body = WorksQuery::build_query(works_query::Variables);
54    let client = reqwest::Client::new();
55    let res = client.post(&thoth_url).json(&request_body).send().await?;
56    let response_body: Response<works_query::ResponseData> = res.json().await?;
57    match response_body.data {
58        Some(data) => {
59            if let Some(errors) = response_body.errors {
60                println!("there are errors:");
61                for error in &errors {
62                    println!("{:?}", error);
63                }
64            }
65            Ok(data.works)
66        }
67        _ => Err(ThothError::InternalError("Query failed".to_string())),
68    }
69}