light weight graphql query to fetch data to construct post URL

This commit is contained in:
realaravinth
2021-11-25 12:35:47 +05:30
parent 32934c8353
commit 9c5342a053
4 changed files with 55 additions and 6 deletions

View File

@@ -26,7 +26,7 @@ use sled::{Db, Tree};
use crate::proxy::StringUtils;
use crate::SETTINGS;
const POST_CACHE_VERSION: usize = 2;
const POST_CACHE_VERSION: usize = 3;
const GIST_CACHE_VERSION: usize = 1;
#[derive(Clone)]
@@ -87,6 +87,20 @@ impl GistFile {
}
}
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "schemas/schema.graphql",
query_path = "schemas/query.graphql",
response_derives = "Debug, Serialize, Deserialize, Clone"
)]
pub struct GetPostLight;
#[derive(Debug, Clone)]
pub struct PostUrl {
pub slug: String,
pub username: String,
}
impl Data {
pub fn new() -> AppData {
let path = Path::new(SETTINGS.cache.as_ref().unwrap()).join("posts_cache");
@@ -152,6 +166,31 @@ impl Data {
}
}
pub async fn get_post_light(&self, id: &str) -> PostUrl {
match self.posts.get(id) {
Ok(Some(v)) => {
let cached: PostResp = bincode::deserialize(&v[..]).unwrap();
PostUrl {
slug: cached.unique_slug,
username: cached.creator.username,
}
}
_ => {
let vars = get_post_light::Variables { id: id.to_owned() };
const URL: &str = "https://medium.com/_/graphql";
let res = post_graphql::<GetPostLight, _>(&self.client, URL, vars)
.await
.unwrap();
let res = res.data.expect("missing response data").post.unwrap();
PostUrl {
slug: res.unique_slug,
username: res.creator.username,
}
}
}
}
pub async fn get_gist(&self, id: String) -> (String, GistContent) {
match self.gists.get(&id) {
Ok(Some(v)) => (id, bincode::deserialize(&v[..]).unwrap()),