-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30dc13a
commit 816e38c
Showing
8 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod auth; | ||
pub mod post; | ||
|
||
pub(crate) type DateTime = chrono::DateTime<chrono::Utc>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
pub mod posts; | ||
|
||
use anyhow::Result; | ||
use pxid::Pxid; | ||
use reqwest::{Client, Url}; | ||
|
||
pub struct PostClient { | ||
client: Client, | ||
domain: Url, | ||
} | ||
|
||
impl PostClient { | ||
pub fn new(domain: Url) -> Self { | ||
Self { | ||
domain, | ||
client: Client::new(), | ||
} | ||
} | ||
|
||
pub async fn posts( | ||
&self, | ||
after: Option<Pxid>, | ||
before: Option<Pxid>, | ||
first: Option<i64>, | ||
last: Option<i64>, | ||
) -> Result<posts::Posts> { | ||
posts::posts(self, after, before, first, last).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
query Posts($after: Pxid, $before: Pxid, $first: Int, $last: Int) { | ||
posts(after: $after, before: $before, first: $first, last: $last) { | ||
edges { | ||
node { | ||
id | ||
title | ||
content | ||
createdAt | ||
updatedAt | ||
author { | ||
id | ||
name | ||
surname | ||
username | ||
createdAt | ||
updatedAt | ||
avatar { | ||
id | ||
url | ||
} | ||
} | ||
} | ||
cursor | ||
} | ||
totalCount | ||
pageInfo { | ||
hasNextPage | ||
hasPreviousPage | ||
startCursor | ||
endCursor | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use anyhow::{anyhow, bail, Result}; | ||
use graphql_client::reqwest::post_graphql; | ||
use graphql_client::GraphQLQuery; | ||
use pxid::Pxid; | ||
|
||
use posts::{PostsPostsEdges, PostsPostsPageInfo, Variables}; | ||
|
||
use crate::{DateTime, GRAPHQL_PATH}; | ||
|
||
use super::PostClient; | ||
|
||
#[derive(GraphQLQuery)] | ||
#[graphql( | ||
response_derives = "Clone,Debug,Deserialize", | ||
schema_path = "schema.json", | ||
query_path = "src/modules/post/posts/Posts.gql" | ||
)] | ||
pub struct Posts { | ||
pub edges: Vec<PostsPostsEdges>, | ||
pub total_count: i64, | ||
pub page_info: PostsPostsPageInfo, | ||
} | ||
|
||
pub async fn posts( | ||
public_client: &PostClient, | ||
after: Option<Pxid>, | ||
before: Option<Pxid>, | ||
first: Option<i64>, | ||
last: Option<i64>, | ||
) -> Result<Posts> { | ||
let url = public_client.domain.join(GRAPHQL_PATH)?; | ||
|
||
let res = post_graphql::<Posts, _>( | ||
&public_client.client, | ||
url, | ||
Variables { | ||
after, | ||
before, | ||
first, | ||
last, | ||
}, | ||
) | ||
.await | ||
.map_err(|err| anyhow!("Failed to make request. {err}"))?; | ||
|
||
if let Some(ref data) = res.data { | ||
return Ok(Posts { | ||
edges: data.posts.edges.to_owned(), | ||
total_count: data.posts.total_count, | ||
page_info: data.posts.page_info.to_owned(), | ||
}); | ||
} | ||
|
||
bail!("Failed to get posts. err = {:#?}", res.errors.unwrap()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters