Skip to content

Commit e4efe92

Browse files
authored
Simplify repo structure (#16)
1 parent 5a1b746 commit e4efe92

File tree

6 files changed

+101
-107
lines changed

6 files changed

+101
-107
lines changed

src/clients.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,65 @@
11
use std::fmt;
2+
#[cfg(test)]
3+
use std::fs::File;
4+
#[cfg(test)]
5+
use std::path::PathBuf;
26

3-
use crate::model::metadata::Metadata;
7+
use reqwest::Url;
8+
use serde::de::DeserializeOwned;
49

5-
#[cfg(test)]
6-
pub(crate) mod mock_tiled_client;
7-
pub(crate) mod tiled_client;
10+
use crate::model::metadata::Metadata;
811

912
pub trait Client {
1013
fn metadata(&self) -> impl Future<Output = Result<Metadata, ClientError>> + Send;
1114
}
1215

1316
pub type ClientResult<T> = Result<T, ClientError>;
1417

18+
pub struct TiledClient {
19+
pub address: Url,
20+
}
21+
22+
impl TiledClient {
23+
async fn request<T: DeserializeOwned>(&self, endpoint: &str) -> ClientResult<T> {
24+
println!("Requesting data from tiled");
25+
26+
let url = self.address.join(endpoint)?;
27+
28+
let response = reqwest::get(url).await?;
29+
let json = response.json().await?;
30+
31+
Ok(serde_json::from_value(json)?)
32+
}
33+
}
34+
impl Client for TiledClient {
35+
async fn metadata(&self) -> ClientResult<Metadata> {
36+
self.request::<Metadata>("/api/v1/").await
37+
}
38+
}
39+
40+
#[cfg(test)]
41+
pub struct MockTiledClient {
42+
pub dir_path: PathBuf,
43+
}
44+
45+
#[cfg(test)]
46+
impl MockTiledClient {
47+
async fn deserialize_from_file<T: DeserializeOwned>(&self, filename: &str) -> ClientResult<T> {
48+
println!("Requesting data from mock");
49+
50+
let path = self.dir_path.join(filename);
51+
let file = File::open(&path)?;
52+
53+
Ok(serde_json::from_reader(file)?)
54+
}
55+
}
56+
#[cfg(test)]
57+
impl Client for MockTiledClient {
58+
async fn metadata(&self) -> ClientResult<Metadata> {
59+
self.deserialize_from_file("tiled_metadata.json").await
60+
}
61+
}
62+
1563
#[derive(Debug)]
1664
pub enum ClientError {
1765
Parse(url::ParseError),

src/clients/mock_tiled_client.rs

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/clients/tiled_client.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/handlers.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,47 @@
1-
pub mod graphql;
1+
use async_graphql::http::GraphiQLSource;
2+
use async_graphql::*;
3+
use async_graphql_axum::{GraphQLRequest, GraphQLResponse};
4+
use axum::Extension;
5+
use axum::response::{Html, IntoResponse};
6+
7+
use crate::clients::Client;
8+
use crate::model::TiledQuery;
9+
10+
pub async fn graphql_handler<T: Client + Send + Sync + 'static>(
11+
schema: Extension<Schema<TiledQuery<T>, EmptyMutation, EmptySubscription>>,
12+
req: GraphQLRequest,
13+
) -> GraphQLResponse {
14+
let query = req.into_inner().query;
15+
16+
schema.execute(query).await.into()
17+
}
18+
19+
pub async fn graphiql_handler() -> impl IntoResponse {
20+
Html(GraphiQLSource::build().endpoint("/graphql").finish())
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use async_graphql::{EmptyMutation, EmptySubscription, Schema};
26+
27+
use crate::TiledQuery;
28+
use crate::clients::MockTiledClient;
29+
30+
#[tokio::test]
31+
async fn test_api_version_query() {
32+
let schema = Schema::build(
33+
TiledQuery(MockTiledClient {
34+
dir_path: "./resources".into(),
35+
}),
36+
EmptyMutation,
37+
EmptySubscription,
38+
)
39+
.finish();
40+
41+
let response = schema.execute("{metadata { apiVersion } }").await;
42+
43+
println!("{:?}", response.data.to_string());
44+
45+
assert!(response.data.to_string() == "{metadata: {apiVersion: 0}}")
46+
}
47+
}

src/handlers/graphql.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ mod model;
1212

1313
use cli::{Cli, Commands};
1414

15-
use crate::clients::tiled_client::TiledClient;
15+
use crate::clients::TiledClient;
1616
use crate::config::GlazedConfig;
17-
use crate::handlers::graphql::{graphiql_handler, graphql_handler};
17+
use crate::handlers::{graphiql_handler, graphql_handler};
1818
use crate::model::TiledQuery;
1919

2020
#[tokio::main]

0 commit comments

Comments
 (0)