client is now not static anymore
continuous-integration/drone/push Build is passing Details

This commit is contained in:
LordMZTE 2021-05-27 18:01:30 +02:00
parent 3c395a34a7
commit df7d39971b
14 changed files with 236 additions and 227 deletions

View File

@ -26,6 +26,7 @@ jm_client_core = { path = "../jm_client_core" }
log = "0.4.11"
opener = "0.4.1"
reqwest = { version = "0.10", features = ["stream"] }
serde_json = "1.0.60"
structopt = "0.3.21"
term-table = "1.3.0"
term_size = "0.3.2"

View File

@ -1,16 +1,15 @@
use crate::table::{self, IntoTableRow};
use jm_client_core::util::api;
use reqwest::Client;
use crate::table::{self, AsTableRow};
use jm_client_core::JMClient;
pub async fn run(http: &Client) -> anyhow::Result<()> {
pub async fn run(client: &JMClient) -> anyhow::Result<()> {
// clone required, because for sorting the immutable reference will not work
let mut cats = api::cats(http).await?.clone();
let mut cats = client.get_cats().await?.as_ref().clone();
cats.sort_by(|a, b| a.id.cmp(&b.id));
let mut table = table::list_table();
for cat in &cats {
table.add_row(cat.into_table_row());
table.add_row(cat.as_table_row());
}
println!("{}", table.render());

View File

@ -1,15 +1,17 @@
use anyhow::{Context, Result};
use reqwest::Client;
use std::{
io::Write,
process::{Command, Stdio},
};
use crate::table::{self, IntoTableRow};
use jm_client_core::util::{self, api, MemeSorting};
use crate::{
table::{self, AsTableRow},
util,
};
use jm_client_core::{util::MemeSorting, JMClient};
pub async fn run(
http: &Client,
client: &JMClient,
cat: Option<String>,
user: Option<String>,
sorting: Option<MemeSorting>,
@ -18,28 +20,28 @@ pub async fn run(
// This needs to be done so both users, memes and categories will be requested
// at once
let (memes, ..) = tokio::try_join!(
api::memes(
http,
cat.as_ref().map(String::from),
user.as_ref().map(String::from)
),
async { client.get_memes().await.map_err(|e| e.into()) },
async {
if let Some(c) = cat.as_ref() {
util::assert_category_exists(http, c).await
util::assert_category_exists(client, c).await
} else {
Ok(())
}
},
async {
if let Some(u) = user.as_ref() {
util::assert_user_exists(http, u).await
util::assert_user_exists(client, u).await
} else {
Ok(())
}
},
)?;
let mut memes = memes.iter().collect::<Vec<_>>();
let mut memes = memes
.iter()
.filter(|m| cat.as_ref().map(|c| &m.category == c).unwrap_or(true))
.filter(|m| user.as_ref().map(|u| &m.user == u).unwrap_or(true))
.collect::<Vec<_>>();
if let Some(s) = sorting {
s.sort_with(&mut memes);
@ -48,7 +50,7 @@ pub async fn run(
let mut table = table::list_table();
for m in memes.iter() {
table.add_row(m.into_table_row());
table.add_row(m.as_table_row());
}
let table_str = table.render();

View File

@ -1,13 +1,15 @@
use anyhow::{Context, Result};
use log::info;
use reqwest::Client;
use std::io::Write;
use crate::table::{self, IntoTableRow};
use jm_client_core::util::{self, api};
use crate::{
table::{self, AsTableRow},
util,
};
use jm_client_core::JMClient;
pub async fn run(
http: &Client,
client: &JMClient,
query: String,
user: Option<String>,
category: Option<String>,
@ -15,21 +17,17 @@ pub async fn run(
cat: bool,
) -> Result<()> {
let (memes, ..) = tokio::try_join!(
api::memes(
http,
category.as_ref().map(String::clone),
user.as_ref().map(String::clone),
),
async { client.get_memes().await.map_err(|e| e.into()) },
async {
if let Some(u) = user.as_ref() {
util::assert_user_exists(http, u).await
util::assert_user_exists(client, u).await
} else {
Ok(())
}
},
async {
if let Some(c) = category.as_ref() {
util::assert_category_exists(http, c).await
util::assert_category_exists(client, c).await
} else {
Ok(())
}
@ -40,7 +38,12 @@ pub async fn run(
info!("Starting search with query '{}'", query);
for meme in memes.iter() {
let memes = memes
.iter()
.filter(|m| category.as_ref().map(|c| &m.category == c).unwrap_or(true))
.filter(|m| user.as_ref().map(|u| &m.user == u).unwrap_or(true));
for meme in memes {
let file_name = meme.file_name()?;
if let Some(score) = fuzzy_matcher::clangd::fuzzy_match(file_name, &query) {
info!("Found matching meme '{}' with score {}", file_name, score);
@ -54,7 +57,7 @@ pub async fn run(
let mut table = table::list_table();
for m in matches {
table.add_row(m.0.into_table_row());
table.add_row(m.0.as_table_row());
}
table.render().into_bytes()
},
@ -68,7 +71,7 @@ pub async fn run(
.into_bytes(),
(_, true) => {
let url = &matches.first().context("No results found")?.0.link;
http.get(url).send().await?.bytes().await?.to_vec()
client.http.get(url).send().await?.bytes().await?.to_vec()
},
};

View File

@ -1,25 +1,25 @@
use crate::util::open_link;
use crate::util;
use anyhow::Result;
use jm_client_core::{api::UpResp, util};
use jm_client_core::{api::UpResp, JMClient};
use log::info;
use reqwest::{
multipart::{Form, Part},
Body,
Client,
};
use tokio::{fs::File, io::reader_stream};
pub async fn run(
http: &Client,
client: &JMClient,
token: String,
path: String,
name: String,
category: String,
open: bool,
) -> Result<()> {
util::assert_category_exists(http, &category).await?;
util::assert_category_exists(client, &category).await?;
let res = http
let res = client
.http
.post("https://data.tilera.xyz/api/jensmemes/upload")
.multipart(
Form::new()
@ -38,7 +38,8 @@ pub async fn run(
.await?;
let status = res.status();
let res = util::api::try_deserialize_api_reponse::<UpResp>(&res.bytes().await?)?;
// TODO move into JMClient
let res = serde_json::from_slice::<UpResp>(&res.bytes().await?)?;
println!("Server responded with code {}", status);
@ -48,7 +49,7 @@ pub async fn run(
for f in res.files {
if open {
open_link(&f).await?;
util::open_link(&f).await?;
} else {
println!("{}", f);
}

View File

@ -1,14 +1,13 @@
use crate::table::{self, IntoTableRow};
use crate::table::{self, AsTableRow};
use anyhow::Result;
use jm_client_core::util::api;
use reqwest::Client;
use jm_client_core::JMClient;
pub async fn run(http: &Client) -> Result<()> {
let users = api::users(http).await?;
pub async fn run(client: &JMClient) -> Result<()> {
let users = client.get_users().await?;
let mut table = table::list_table();
for u in users {
table.add_row(u.into_table_row())
for u in &*users {
table.add_row(u.as_table_row())
}
println!("{}", table.render());

View File

@ -1,6 +1,5 @@
use anyhow::Result;
use jm_client_core::util::MemeSorting;
use reqwest::Client;
use jm_client_core::{util::MemeSorting, JMClient};
use structopt::StructOpt;
mod commands;
@ -92,7 +91,7 @@ async fn main() -> Result<()> {
env_logger::init();
let Opts { cmd } = Opts::from_args();
let http = Client::new();
let client = JMClient::new();
match cmd {
Cmd::Up {
@ -103,23 +102,23 @@ async fn main() -> Result<()> {
open,
} => {
let name = name.unwrap_or_else(|| file.clone());
commands::up::run(&http, token, file, name, category, open).await?;
commands::up::run(&client, token, file, name, category, open).await?;
},
Cmd::Cats => commands::cats::run(&http).await?,
Cmd::Cats => commands::cats::run(&client).await?,
Cmd::Search {
query,
user,
category,
firsturl,
cat,
} => commands::search::run(&http, query, user, category, firsturl, cat).await?,
} => commands::search::run(&client, query, user, category, firsturl, cat).await?,
Cmd::List {
category,
user,
sort,
fzf,
} => commands::list::run(&http, category, user, sort, fzf).await?,
Cmd::Users => commands::users::run(&http).await?,
} => commands::list::run(&client, category, user, sort, fzf).await?,
Cmd::Users => commands::users::run(&client).await?,
}
Ok(())

View File

@ -12,18 +12,18 @@ pub fn list_table<'a>() -> Table<'a> {
.build()
}
pub trait IntoTableRow {
fn into_table_row(&self) -> term_table::row::Row;
pub trait AsTableRow {
fn as_table_row(&self) -> term_table::row::Row;
}
impl IntoTableRow for Category {
fn into_table_row(&self) -> Row<'_> {
impl AsTableRow for Category {
fn as_table_row(&self) -> Row<'_> {
Row::new(vec![TableCell::new(&self.id), TableCell::new(&self.name)])
}
}
impl IntoTableRow for Meme {
fn into_table_row(&self) -> Row<'_> {
impl AsTableRow for Meme {
fn as_table_row(&self) -> Row<'_> {
Row::new(vec![
TableCell::new(&self.link),
TableCell::new(&self.category),
@ -33,8 +33,8 @@ impl IntoTableRow for Meme {
}
}
impl IntoTableRow for User {
fn into_table_row(&self) -> Row<'_> {
impl AsTableRow for User {
fn as_table_row(&self) -> Row<'_> {
Row::new(vec![
TableCell::new(&self.name),
TableCell::new(&self.get_id().map(String::as_ref).unwrap_or("[No ID]")),

View File

@ -1,5 +1,26 @@
use anyhow::bail;
use jm_client_core::JMClient;
use tokio::process::Command;
pub const NO_SUCH_CATEGORY_ERROR: &str = "The given Category does not exist!";
pub const NO_SUCH_USER_ERROR: &str = "The given User does not exist!";
pub async fn assert_category_exists(client: &JMClient, cat: &str) -> anyhow::Result<()> {
if !client.get_cats().await?.iter().any(|c| c.id == cat) {
bail!(NO_SUCH_CATEGORY_ERROR);
}
Ok(())
}
pub async fn assert_user_exists(client: &JMClient, user: &str) -> anyhow::Result<()> {
if !client.get_users().await?.iter().any(|u| u.name == user) {
bail!(NO_SUCH_USER_ERROR);
}
Ok(())
}
pub async fn open_link(url: &str) -> anyhow::Result<()> {
match std::env::var_os("BROWSER") {
Some(browser) => {

View File

@ -11,8 +11,7 @@ use druid::{
WidgetExt,
WindowDesc,
};
use jm_client_core::api::Meme;
use reqwest::Client;
use jm_client_core::{api::Meme, JMClient};
pub(crate) mod util;
@ -20,8 +19,9 @@ const LIST_COLS: &[(&str, f64)] = &[("Link", 1000.), ("User", 50.)];
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let http = Client::new();
let mut memes = jm_client_core::util::api::memes(&http, None, None)
let client = JMClient::new();
let mut memes = client
.get_memes()
.await?
.iter()
.map(|m| EqData(m.clone()))

View File

@ -1,5 +1,9 @@
use anyhow::{anyhow, Result};
use serde::{Deserialize, Deserializer, de::{self, Visitor}};
use serde::{
de::{self, Visitor},
Deserialize,
Deserializer,
};
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct UpResp {
@ -60,8 +64,8 @@ impl User {
pub fn get_id(&self) -> Option<&String> {
self.id
.as_ref()
.or(self.tokenhash.as_ref())
.or(self.userdir.as_ref())
.or_else(|| self.tokenhash.as_ref())
.or_else(|| self.userdir.as_ref())
}
}
@ -92,7 +96,8 @@ where
where
E: de::Error,
{
v.parse().map_err(|_| de::Error::invalid_value(de::Unexpected::Str(v), &self))
v.parse()
.map_err(|_| de::Error::invalid_value(de::Unexpected::Str(v), &self))
}
}

View File

@ -0,0 +1,130 @@
use crate::api::{Category, CatsResp, Meme, MemesResp, User, UsersResp};
use log::info;
use once_cell::sync::OnceCell;
use reqwest::Url;
use serde::de::DeserializeOwned;
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::Mutex;
#[derive(Debug)]
pub struct JMClient {
pub http: reqwest::Client,
cache: Mutex<Cache>,
endpoint: Url,
}
macro_rules! init_cache {
($cell:expr, $init_fn:expr) => {{
let cell = &$cell;
Arc::clone(match cell.get() {
Some(x) => x,
None => {
let x = Arc::new($init_fn);
cell.get_or_init(|| x)
},
})
}};
}
impl JMClient {
pub fn new() -> Self {
Self::builder().build()
}
pub fn builder() -> JMClientBuilder {
JMClientBuilder::default()
}
async fn get_api_json<R: DeserializeOwned, T, F: FnOnce(R) -> T>(
&self,
cache: &OnceCell<Arc<T>>,
endpoint: &str,
res_to_data: F,
) -> Result<Arc<T>, JMClientError> {
Ok(init_cache!(cache, {
info!("Requesting {} from server", endpoint);
let url = self.endpoint.join(endpoint)?;
let res = self.http.get(url).send().await?;
let res = serde_json::from_slice(&res.bytes().await?)?;
res_to_data(res)
}))
}
pub async fn get_cats(&self) -> Result<Arc<Vec<Category>>, JMClientError> {
self.get_api_json(
&self.cache.lock().await.cats,
"categories",
|r: CatsResp| r.categories,
)
.await
}
pub async fn get_memes(&self) -> Result<Arc<Vec<Meme>>, JMClientError> {
self.get_api_json(&self.cache.lock().await.memes, "memes", |r: MemesResp| {
r.memes
})
.await
}
pub async fn get_users(&self) -> Result<Arc<Vec<User>>, JMClientError> {
self.get_api_json(&self.cache.lock().await.users, "users", |r: UsersResp| {
r.users
})
.await
}
}
impl Default for JMClient {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Error)]
pub enum JMClientError {
#[error("Error making http request: {0}")]
Http(#[from] reqwest::Error),
#[error("Error deserializing JensMemes response: {0}")]
Deserialize(#[from] serde_json::Error),
#[error("Failed parsing URL to make request to JensMemes: {0}")]
UrlParse(#[from] url::ParseError),
}
#[derive(Debug, Default)]
struct Cache {
users: OnceCell<Arc<Vec<User>>>,
cats: OnceCell<Arc<Vec<Category>>>,
memes: OnceCell<Arc<Vec<Meme>>>,
}
#[derive(Debug, Default)]
pub struct JMClientBuilder {
client: Option<reqwest::Client>,
endpoint: Option<Url>,
}
impl JMClientBuilder {
pub fn build(self) -> JMClient {
JMClient {
http: self.client.unwrap_or_else(reqwest::Client::new),
endpoint: self.endpoint.unwrap_or_else(|| {
// unwrapping is fine here, as the hardcoded input is known to work.
Url::parse(crate::util::consts::API_ENDPOINT).unwrap()
}),
cache: Mutex::new(Cache::default()),
}
}
pub fn client(mut self, client: reqwest::Client) -> Self {
self.client = Some(client);
self
}
pub fn endpoint(mut self, endpoint: impl Into<Url>) -> Self {
self.endpoint = Some(endpoint.into());
self
}
}

View File

@ -1,2 +1,5 @@
pub mod api;
pub mod client;
pub mod util;
pub use client::JMClient;

View File

@ -1,150 +1,9 @@
use crate::api::Meme;
use anyhow::bail;
use reqwest::Client;
use std::str::FromStr;
#[macro_export]
macro_rules! init_once_cell {
($cell:ident, $init_fn:expr) => {
match $cell.get() {
Some(x) => x,
None => {
let x = $init_fn;
$cell.get_or_init(|| x)
},
}
};
}
pub mod consts {
pub const API_ENDPOINT: &str = "https://api.tilera.xyz/jensmemes/v1";
pub const NO_SUCH_CATEGORY_ERROR: &str = "The given Category does not exist!";
pub const NO_SUCH_USER_ERROR: &str = "The given User does not exist!";
}
/// ways to communicyte with the JM API
pub mod api {
use super::consts;
use crate::api::{Category, CatsResp, Meme, MemesResp, User, UsersResp};
use anyhow::Result;
use lazy_static::lazy_static;
use log::info;
use once_cell::sync::OnceCell;
use reqwest::Client;
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};
use thiserror::Error;
use url::Url;
// cached api responses
static USERS: OnceCell<Vec<User>> = OnceCell::new();
static CATS: OnceCell<Vec<Category>> = OnceCell::new();
lazy_static! {
// is this type long enough yet?
static ref MEMES: Mutex<HashMap<(Option<String>, Option<String>), Arc<Vec<Meme>>>> =
Mutex::new(HashMap::new());
}
#[derive(Error, Debug)]
#[error("Error Deserializing JSON api response: \n{json}\nError: {error}")]
pub struct ApiDeserializeError {
pub json: String,
#[source]
pub error: serde_json::Error,
}
pub async fn cats(http: &Client) -> Result<&Vec<Category>> {
Ok(init_once_cell!(CATS, {
info!("Requesting categories from server");
let res = http
.get(&format!("{}/categories", consts::API_ENDPOINT))
.send()
.await?;
let cats = try_deserialize_api_reponse::<CatsResp>(&res.bytes().await?)?;
cats.categories.into_iter().collect()
}))
}
pub async fn memes<'a>(
http: &Client,
cat_filter: Option<String>,
usr_filter: Option<String>,
) -> Result<Arc<Vec<Meme>>> {
let filters = (cat_filter, usr_filter);
if let Some(m) = MEMES.lock().unwrap().get(&filters) {
return Ok(m.clone());
}
let mut url = Url::options().parse(&format!("{}/memes", consts::API_ENDPOINT))?;
let mut pairs = url.query_pairs_mut();
if let Some(cat) = filters.0.as_ref() {
pairs.append_pair("category", cat);
}
if let Some(usr) = filters.1.as_ref() {
pairs.append_pair("user", usr);
}
// drop required in order to move the URL into the request
drop(pairs);
info!("Requesting memes from server");
let res = http.get(url).send().await?;
let memes = try_deserialize_api_reponse::<MemesResp>(&res.bytes().await?)?;
Ok(MEMES
.lock()
.unwrap()
.entry(filters)
.or_insert(Arc::new(memes.memes))
.clone())
}
pub async fn users(http: &Client) -> Result<&Vec<User>> {
Ok(init_once_cell!(USERS, {
info!("Requesting users from server");
let res = http
.get(&format!("{}/users", consts::API_ENDPOINT))
.send()
.await?;
let users = try_deserialize_api_reponse::<UsersResp>(&res.bytes().await?)?;
users.users
}))
}
/// tries to deserialize `json` as `T`, and if it fails converts the error
/// to a `ApiDeserializeError`
pub fn try_deserialize_api_reponse<'a, T: serde::Deserialize<'a>>(json: &'a [u8]) -> Result<T> {
let result = serde_json::from_slice::<T>(&json);
result.map_err(|error| match std::str::from_utf8(json) {
Err(e) => e.into(),
Ok(json) => ApiDeserializeError {
json: json.into(),
error,
}
.into(),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn api_deserialize_error() {
let incorrect_json = r#"
{
"foo": "bar"
}
"#;
let result = try_deserialize_api_reponse::<UsersResp>(incorrect_json.as_bytes());
println!("{}", result.unwrap_err());
}
}
pub const API_ENDPOINT: &str = "https://api.tilera.xyz/jensmemes/v1/";
}
pub enum MemeSorting {
@ -191,16 +50,3 @@ impl FromStr for MemeSorting {
}
}
}
pub async fn assert_user_exists(http: &Client, user: &str) -> anyhow::Result<()> {
if !api::users(http).await?.iter().any(|u| u.name == user) {
bail!(consts::NO_SUCH_USER_ERROR);
}
Ok(())
}
pub async fn assert_category_exists(http: &Client, cat: &str) -> anyhow::Result<()> {
if !api::cats(http).await?.iter().any(|c| c.id == cat) {
bail!(consts::NO_SUCH_CATEGORY_ERROR);
}
Ok(())
}