[wip] impl Client for ureq::Agent

- factored out `file` module
- renamed some stuff
- decoupled `sharry::client` from `file`
This commit is contained in:
Jörn-Michael Miehe 2025-06-10 18:20:52 +00:00
parent 69bef4e994
commit d607380659
9 changed files with 134 additions and 138 deletions

View file

@ -13,7 +13,8 @@ use serde::{Deserialize, Serialize};
use super::{
cli::Cli,
sharry::{Client, ClientError, FileChecked, FileUploading, SharryFile, Uri},
file::{self, FileTrait},
sharry::{self, Client, Uri},
};
#[derive(Serialize, Deserialize, Debug)]
@ -31,8 +32,8 @@ pub struct AppState {
#[derive(Serialize, Deserialize, Debug)]
enum FileState {
C(FileChecked),
U(FileUploading),
C(file::Checked),
U(file::Uploading),
}
impl FileState {
@ -49,9 +50,12 @@ impl FileState {
uri: &Uri,
alias_id: &str,
share_id: &str,
) -> Result<FileUploading, ClientError> {
) -> sharry::Result<file::Uploading> {
match self {
FileState::C(checked) => http.sharry_file_create(uri, alias_id, share_id, checked),
FileState::C(checked) => {
let endpoint = &uri.endpoint(format!("alias/upload/{}/files/tus", share_id));
checked.start_upload(http, endpoint, alias_id)
}
FileState::U(uploading) => Ok(uploading),
}
}
@ -99,12 +103,16 @@ impl AppState {
.ok()
}
pub fn from_args(args: &Cli, http: &impl Client) -> Result<Self, ClientError> {
pub fn from_args(args: &Cli, http: &impl Client) -> sharry::Result<Self> {
let file_name = Self::cache_file(args);
let uri = args.get_uri();
let alias_id = args.alias.clone();
let share_id = http.sharry_share_create(&uri, &alias_id, args.get_share_request())?;
let share_id = http.share_create(
&uri.endpoint("alias/upload/new"),
&alias_id,
args.get_share_request(),
)?;
let files: VecDeque<_> = args.files.clone().into_iter().map(FileState::C).collect();
@ -126,11 +134,9 @@ impl AppState {
&mut self,
http: &ureq::Agent,
chunk_size: usize,
) -> Result<Option<()>, UploadError> {
) -> sharry::Result<Option<()>> {
let uploading = if let Some(state) = self.files.pop_front() {
state
.start_upload(http, &self.uri, &self.alias_id, &self.share_id)
.unwrap() // HACK unwrap
state.start_upload(http, &self.uri, &self.alias_id, &self.share_id)?
} else {
return Ok(None);
};

View file

@ -5,7 +5,10 @@ use std::{
use clap::{Parser, builder::PossibleValuesParser};
use super::sharry::{FileChecked, NewShareRequest, Uri};
use super::{
file::Checked,
sharry::{NewShareRequest, Uri},
};
#[derive(Parser, Debug, Hash)]
#[command(version, about, long_about = None)]
@ -50,15 +53,15 @@ pub struct Cli {
/// Files to upload to the new share
#[arg(value_name = "FILE", required = true, value_parser = parse_sharry_file)]
pub files: Vec<FileChecked>,
pub files: Vec<Checked>,
}
fn parse_seconds(data: &str) -> Result<Duration, String> {
data.parse().or(Ok(0)).map(Duration::from_secs)
}
fn parse_sharry_file(data: &str) -> Result<FileChecked, String> {
FileChecked::new(data).map_err(|e| e.to_string())
fn parse_sharry_file(data: &str) -> Result<Checked, String> {
Checked::new(data).map_err(|e| e.to_string())
}
impl Cli {

View file

@ -5,15 +5,17 @@ use std::{
use serde::{Deserialize, Serialize};
use super::SharryFile;
use crate::sharry;
use super::{FileTrait, Uploading};
#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct FileChecked {
pub(super) path: PathBuf,
pub(super) size: u64,
pub struct Checked {
path: PathBuf,
size: u64,
}
impl FileChecked {
impl Checked {
pub fn new(value: impl AsRef<Path>) -> io::Result<Self> {
let meta = fs::metadata(&value)?;
if meta.is_file() {
@ -28,14 +30,25 @@ impl FileChecked {
))
}
}
pub fn start_upload(
self,
client: &impl sharry::Client,
endpoint: &str,
alias_id: &str,
) -> sharry::Result<Uploading> {
let patch_uri = client.file_create(endpoint, alias_id, self.get_name(), self.size)?;
Ok(Uploading::new(self.path, self.size, patch_uri))
}
}
impl<'t> SharryFile<'t> for FileChecked {
impl<'t> FileTrait<'t> for Checked {
/// get a reference to the file's name
///
/// Uses `SharryFile::extract_file_name`, which may **panic**!
fn get_name(&'t self) -> &'t str {
<Self as SharryFile>::extract_file_name(&self.path)
<Self as FileTrait>::extract_file_name(&self.path)
}
fn get_size(&self) -> u64 {

View file

@ -1,15 +1,12 @@
mod checked;
mod uploading;
use std::{
ffi::OsStr,
path::{Path, PathBuf},
};
use std::{ffi::OsStr, path::Path};
pub use checked::FileChecked;
pub use uploading::FileUploading;
pub use checked::Checked;
pub use uploading::Uploading;
pub trait SharryFile<'t> {
pub trait FileTrait<'t> {
/// extract the filename part of a `Path` reference
///
/// # Panics

View file

@ -6,17 +6,17 @@ use std::{
use serde::{Deserialize, Serialize};
use super::{FileChecked, SharryFile};
use super::FileTrait;
#[derive(Serialize, Deserialize, Debug)]
pub struct FileUploading {
pub struct Uploading {
path: PathBuf,
size: u64,
patch_uri: String,
offset: u64,
}
impl fmt::Display for FileUploading {
impl fmt::Display for Uploading {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
@ -28,11 +28,11 @@ impl fmt::Display for FileUploading {
}
}
impl FileUploading {
pub fn new(file: FileChecked, patch_uri: String) -> Self {
impl Uploading {
pub(super) fn new(path: PathBuf, size: u64, patch_uri: String) -> Self {
Self {
path: file.path,
size: file.size,
path,
size,
patch_uri,
offset: 0,
}
@ -74,12 +74,12 @@ impl FileUploading {
}
}
impl<'t> SharryFile<'t> for FileUploading {
impl<'t> FileTrait<'t> for Uploading {
/// get a reference to the file's name
///
/// Uses `SharryFile::extract_file_name`, which may **panic**!
fn get_name(&'t self) -> &'t str {
<Self as SharryFile>::extract_file_name(&self.path)
<Self as FileTrait>::extract_file_name(&self.path)
}
fn get_size(&self) -> u64 {

View file

@ -1,5 +1,6 @@
mod appstate;
mod cli;
mod file;
mod sharry;
use std::{
@ -69,7 +70,7 @@ fn main() {
actual: _,
expected: 403,
} => Some("Alias ID"),
ClientError::FileIO(_) => Some("URL"),
// ClientError::FileIO(_) => Some("URL"),
_ => None,
} {
info!("handling error: {e:?}");

View file

@ -10,19 +10,19 @@ pub struct Uri {
}
impl Uri {
pub(super) fn get_endpoint(&self, endpoint: impl fmt::Display + fmt::Debug) -> String {
let uri = format!("{}/{}", self, endpoint);
debug!("endpoint uri: {uri:?}");
uri
}
pub fn with_protocol(protocol: impl Into<String>, base_url: impl Into<String>) -> Self {
Self {
protocol: protocol.into(),
base_url: base_url.into(),
}
}
pub fn endpoint(&self, endpoint: impl fmt::Display) -> String {
let uri = format!("{}/{}", self, endpoint);
debug!("endpoint: {uri:?}");
uri
}
}
impl fmt::Display for Uri {

View file

@ -1,51 +1,32 @@
use std::{error::Error, fmt::Display, io};
use std::fmt;
use log::debug;
use log::{debug, trace};
use thiserror::Error;
use super::{
api::{NewShareRequest, NewShareResponse, NotifyShareResponse, Uri},
file::{FileChecked, FileUploading, SharryFile},
};
use super::api::{NewShareRequest, NewShareResponse, NotifyShareResponse};
pub type Result<T> = std::result::Result<T, ClientError>;
pub trait Client {
fn sharry_share_create(
&self,
uri: &Uri,
alias_id: &str,
data: NewShareRequest,
) -> Result<String, ClientError>;
fn share_create(&self, endpoint: &str, alias_id: &str, data: NewShareRequest)
-> Result<String>;
fn sharry_share_notify(
&self,
uri: &Uri,
alias_id: &str,
share_id: &str,
) -> Result<(), ClientError>;
fn share_notify(&self, endpoint: &str, alias_id: &str) -> Result<()>;
fn sharry_file_create(
fn file_create(
&self,
uri: &Uri,
endpoint: &str,
alias_id: &str,
share_id: &str,
file_name: &str,
file_size: u64,
) -> Result<String, ClientError>;
) -> Result<String>;
fn sharry_file_patch(
&self,
patch_uri: &str,
alias_id: &str,
offset: u64,
chunk: &[u8],
) -> Result<u64, ClientError>;
fn file_patch(&self, patch_uri: &str, alias_id: &str, offset: u64, chunk: &[u8])
-> Result<u64>;
}
#[derive(Debug, Error)]
pub enum ClientError {
#[error("file I/O error: {0}")]
FileIO(#[from] io::Error),
#[error("network request failed: {0}")]
Request(String),
@ -60,19 +41,15 @@ pub enum ClientError {
}
impl ClientError {
fn req_err(msg: impl Display) -> Self {
fn req_err(msg: impl fmt::Display) -> Self {
Self::Request(msg.to_string())
}
fn res_parse_err(msg: impl Display) -> Self {
fn res_parse_err(msg: impl fmt::Display) -> Self {
Self::ResponseParsing(msg.to_string())
}
fn res_content_err(msg: impl Display) -> Self {
Self::ResponseContent(msg.to_string())
}
fn res_check_status<T>(actual: T, expected: T) -> Result<(), Self>
fn res_check_status<T>(actual: T, expected: T) -> Result<()>
where
T: Into<u16> + Eq,
{
@ -88,75 +65,77 @@ impl ClientError {
}
impl Client for ureq::Agent {
fn sharry_share_create(
fn share_create(
&self,
uri: &Uri,
endpoint: &str,
alias_id: &str,
data: NewShareRequest,
) -> Result<String, ClientError> {
let res = {
let endpoint = uri.get_endpoint("alias/upload/new");
) -> Result<String> {
// let endpoint = uri.get_endpoint("alias/upload/new");
self.post(endpoint)
.header("Sharry-Alias", alias_id)
.send_json(data)
.map_err(|e| ClientError::req_err(e))?
.body_mut()
.read_json::<NewShareResponse>()
.map_err(|e| ClientError::res_parse_err(e))?
};
let mut res = self
.post(endpoint)
.header("Sharry-Alias", alias_id)
.send_json(data)
.map_err(ClientError::req_err)?;
debug!("response: {res:?}");
trace!("{endpoint:?} response: {res:?}");
ClientError::res_check_status(res.status(), ureq::http::StatusCode::OK)?;
let res = res
.body_mut()
.read_json::<NewShareResponse>()
.map_err(ClientError::res_parse_err)?;
debug!("{res:?}");
if res.success && (res.message == "Share created.") {
Ok(res.id)
} else {
Err(ClientError::res_content_err(format!("{res:?}")))
Err(ClientError::ResponseContent(format!("{res:?}")))
}
}
fn sharry_share_notify(
&self,
uri: &Uri,
alias_id: &str,
share_id: &str,
) -> Result<(), ClientError> {
let res = {
let endpoint = uri.get_endpoint(format!("alias/mail/notify/{}", share_id));
fn share_notify(&self, endpoint: &str, alias_id: &str) -> Result<()> {
// let endpoint = uri.get_endpoint(format!("alias/mail/notify/{}", share_id));
self.post(endpoint)
.header("Sharry-Alias", alias_id)
.send_empty()
.map_err(|e| ClientError::req_err(e))?
.body_mut()
.read_json::<NotifyShareResponse>()
.map_err(|e| ClientError::res_parse_err(e))?
};
let mut res = self
.post(endpoint)
.header("Sharry-Alias", alias_id)
.send_empty()
.map_err(|e| ClientError::req_err(e))?;
debug!("response: {res:?}");
trace!("{endpoint:?} response: {res:?}");
ClientError::res_check_status(res.status(), ureq::http::StatusCode::OK)?;
let res = res
.body_mut()
.read_json::<NotifyShareResponse>()
.map_err(|e| ClientError::res_parse_err(e))?;
debug!("{res:?}");
Ok(())
}
fn sharry_file_create(
fn file_create(
&self,
uri: &Uri,
endpoint: &str,
alias_id: &str,
share_id: &str,
file_name: &str,
file_size: u64,
) -> Result<String, ClientError> {
let res = {
let endpoint = uri.get_endpoint(format!("alias/upload/{}/files/tus", share_id));
) -> Result<String> {
// let endpoint = uri.get_endpoint(format!("alias/upload/{}/files/tus", share_id));
self.post(endpoint)
.header("Sharry-Alias", alias_id)
.header("Sharry-File-Name", file_name)
.header("Upload-Length", file_size)
.send_empty()
.map_err(ClientError::req_err)?
};
let res = self
.post(endpoint)
.header("Sharry-Alias", alias_id)
.header("Sharry-File-Name", file_name)
.header("Upload-Length", file_size)
.send_empty()
.map_err(ClientError::req_err)?;
trace!("{endpoint:?} response: {res:?}");
ClientError::res_check_status(res.status(), ureq::http::StatusCode::CREATED)?;
let location = (res.headers().get("Location"))
@ -165,18 +144,18 @@ impl Client for ureq::Agent {
.map_err(ClientError::res_parse_err)?
.to_string();
debug!("patch uri: {location}");
debug!("{location:?}");
Ok(location)
}
fn sharry_file_patch(
fn file_patch(
&self,
patch_uri: &str,
alias_id: &str,
offset: u64,
chunk: &[u8],
) -> Result<u64, ClientError> {
) -> Result<u64> {
let res = self
.patch(patch_uri)
.header("Sharry-Alias", alias_id)
@ -184,6 +163,7 @@ impl Client for ureq::Agent {
.send(chunk)
.map_err(ClientError::req_err)?;
trace!("{patch_uri:?} response: {res:?}");
ClientError::res_check_status(res.status(), ureq::http::StatusCode::NO_CONTENT)?;
let res_offset = (res.headers().get("Upload-Offset"))

View file

@ -1,9 +1,5 @@
#![allow(unused_imports)]
mod api;
mod client;
mod file;
pub use api::{NewShareRequest, Uri};
pub use client::{Client, ClientError};
pub use file::{FileChecked, FileUploading, SharryFile};
pub use client::{Client, ClientError, Result};