[wip] impl Client for ureq::Agent
This commit is contained in:
parent
5b6fa3eaf7
commit
51ecab41bb
3 changed files with 108 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
use log::debug;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Hash)]
|
#[derive(Serialize, Deserialize, Debug, Hash)]
|
||||||
|
|
@ -9,6 +10,13 @@ pub struct Uri {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
pub fn with_protocol(protocol: impl Into<String>, base_url: impl Into<String>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
protocol: protocol.into(),
|
protocol: protocol.into(),
|
||||||
|
|
|
||||||
98
src/sharry/client.rs
Normal file
98
src/sharry/client.rs
Normal file
|
|
@ -0,0 +1,98 @@
|
||||||
|
use std::{error::Error, io};
|
||||||
|
|
||||||
|
use log::debug;
|
||||||
|
|
||||||
|
use super::api::{NewShareRequest, NewShareResponse, NotifyShareResponse, Uri};
|
||||||
|
|
||||||
|
pub trait Client {
|
||||||
|
fn sharry_share_create(
|
||||||
|
&self,
|
||||||
|
uri: &Uri,
|
||||||
|
alias_id: &str,
|
||||||
|
data: NewShareRequest,
|
||||||
|
) -> Result<String, ClientError>;
|
||||||
|
|
||||||
|
fn sharry_share_notify(
|
||||||
|
&self,
|
||||||
|
uri: &Uri,
|
||||||
|
alias_id: &str,
|
||||||
|
share_id: &str,
|
||||||
|
) -> Result<(), ClientError>;
|
||||||
|
|
||||||
|
// fn sharry_file_create(&self);
|
||||||
|
|
||||||
|
// fn sharry_file_patch(&self);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, thiserror::Error)]
|
||||||
|
pub enum ClientError {
|
||||||
|
#[error("file I/O error: {0}")]
|
||||||
|
FileIO(#[from] io::Error),
|
||||||
|
|
||||||
|
#[error("network request failed: {0}")]
|
||||||
|
Request(String),
|
||||||
|
|
||||||
|
#[error("response parsing failed: {0}")]
|
||||||
|
ResponseParsing(String),
|
||||||
|
//
|
||||||
|
// #[error("unexpected response status: {actual} (expected {expected})")]
|
||||||
|
// ResponseStatus { actual: u64, expected: u64 },
|
||||||
|
//
|
||||||
|
#[error("unexpected response content: {0}")]
|
||||||
|
ResponseContent(String),
|
||||||
|
//
|
||||||
|
// #[error("could not parse offset header")]
|
||||||
|
// ResponseOffset,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Client for ureq::Agent {
|
||||||
|
fn sharry_share_create(
|
||||||
|
&self,
|
||||||
|
uri: &Uri,
|
||||||
|
alias_id: &str,
|
||||||
|
data: NewShareRequest,
|
||||||
|
) -> Result<String, ClientError> {
|
||||||
|
let res = {
|
||||||
|
let endpoint = uri.get_endpoint("alias/upload/new");
|
||||||
|
|
||||||
|
self.post(endpoint)
|
||||||
|
.header("Sharry-Alias", alias_id)
|
||||||
|
.send_json(data)
|
||||||
|
.map_err(|e| ClientError::Request(e.to_string()))?
|
||||||
|
.body_mut()
|
||||||
|
.read_json::<NewShareResponse>()
|
||||||
|
.map_err(|e| ClientError::ResponseParsing(e.to_string()))?
|
||||||
|
};
|
||||||
|
|
||||||
|
debug!("response: {res:?}");
|
||||||
|
|
||||||
|
if res.success && (res.message == "Share created.") {
|
||||||
|
Ok(res.id)
|
||||||
|
} else {
|
||||||
|
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));
|
||||||
|
|
||||||
|
self.post(endpoint)
|
||||||
|
.header("Sharry-Alias", alias_id)
|
||||||
|
.send_empty()
|
||||||
|
.map_err(|e| ClientError::Request(e.to_string()))?
|
||||||
|
.body_mut()
|
||||||
|
.read_json::<NotifyShareResponse>()
|
||||||
|
.map_err(|e| ClientError::ResponseParsing(e.to_string()))?
|
||||||
|
};
|
||||||
|
|
||||||
|
debug!("response: {res:?}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,10 +2,12 @@
|
||||||
|
|
||||||
mod alias;
|
mod alias;
|
||||||
mod api;
|
mod api;
|
||||||
|
mod client;
|
||||||
mod file;
|
mod file;
|
||||||
mod share;
|
mod share;
|
||||||
|
|
||||||
pub use alias::Alias;
|
pub use alias::Alias;
|
||||||
pub use api::{NewShareRequest, Uri};
|
pub use api::{NewShareRequest, Uri};
|
||||||
|
// pub use client::SharryClient;
|
||||||
pub use file::{ChunkState, FileChecked, FileUploading, SharryFile, UploadError};
|
pub use file::{ChunkState, FileChecked, FileUploading, SharryFile, UploadError};
|
||||||
pub use share::Share;
|
pub use share::Share;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue