feat(nextcloud): support exponential backoff with jitter
This commit is contained in:
parent
4cdfdaec6f
commit
5d62b7edd0
2 changed files with 51 additions and 67 deletions
|
@ -5,21 +5,18 @@ use kxio::{
|
|||
net::{Net, ReqBuilder},
|
||||
};
|
||||
use reqwest::multipart;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::nextcloud::model::{
|
||||
Label, NextcloudCardDescription, NextcloudCardTitle, NextcloudLabelColour, NextcloudLabelTitle,
|
||||
NextcloudStackTitle,
|
||||
};
|
||||
use crate::{
|
||||
api_result::APIResult,
|
||||
f,
|
||||
nextcloud::model::{
|
||||
Attachment, Board, Card, NextcloudBoardId, NextcloudCardId, NextcloudHostname,
|
||||
NextcloudLabelId, NextcloudPassword, NextcloudStackId, NextcloudUsername, Stack,
|
||||
Attachment, Board, Card, Label, NextcloudBoardId, NextcloudCardDescription,
|
||||
NextcloudCardId, NextcloudCardTitle, NextcloudHostname, NextcloudLabelColour,
|
||||
NextcloudLabelId, NextcloudLabelTitle, NextcloudPassword, NextcloudStackId,
|
||||
NextcloudStackTitle, NextcloudUsername, Stack,
|
||||
},
|
||||
FullCtx,
|
||||
with_exponential_backoff, FullCtx,
|
||||
};
|
||||
|
||||
pub(crate) struct DeckClient<'ctx> {
|
||||
|
@ -53,13 +50,17 @@ impl<'ctx> DeckClient<'ctx> {
|
|||
url: impl Into<String>,
|
||||
custom: fn(&Net, String) -> ReqBuilder,
|
||||
) -> APIResult<T> {
|
||||
let url = url.into();
|
||||
APIResult::new(
|
||||
custom(&self.ctx.net, self.url(url))
|
||||
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
||||
.header("accept", "application/json")
|
||||
.header("content-type", "application/json")
|
||||
.send()
|
||||
.await,
|
||||
with_exponential_backoff!(
|
||||
&self.ctx,
|
||||
custom(&self.ctx.net, self.url(url.clone()))
|
||||
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
||||
.header("accept", "application/json")
|
||||
.header("content-type", "application/json")
|
||||
.send()
|
||||
.await
|
||||
),
|
||||
&self.ctx.prt,
|
||||
)
|
||||
.await
|
||||
|
@ -71,49 +72,19 @@ impl<'ctx> DeckClient<'ctx> {
|
|||
body: impl Into<Bytes>,
|
||||
custom: fn(&Net, String) -> ReqBuilder,
|
||||
) -> APIResult<T> {
|
||||
let url = self.url(url.into());
|
||||
let body = body.into();
|
||||
APIResult::new(
|
||||
custom(&self.ctx.net, self.url(url))
|
||||
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
||||
.header("accept", "application/json")
|
||||
.header("content-type", "application/json")
|
||||
.body(body)
|
||||
.send()
|
||||
.await,
|
||||
&self.ctx.prt,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn request_with_form<T>(
|
||||
&self,
|
||||
path: String,
|
||||
// form_data: HashMap<String, String>,
|
||||
form: multipart::Form,
|
||||
) -> APIResult<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
// let form: multipart::Form = multipart::Form::new();
|
||||
// let full_path = self.ctx.fs.base().join(form_data.get("file").expect("file"));
|
||||
// e!(self.ctx.prt, "Uploading file: {}", full_path.display());
|
||||
// let form = form.file("file", Path::new(&full_path))
|
||||
// .await
|
||||
// .expect("read file");
|
||||
let request_builder = self
|
||||
.ctx
|
||||
.net
|
||||
.client()
|
||||
.post(self.url(&path))
|
||||
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
||||
.header("accept", "application/json")
|
||||
// .form(&form_data);
|
||||
.multipart(form);
|
||||
// let data = request_builder.multipart();
|
||||
APIResult::new(
|
||||
match self.ctx.net.send(request_builder).await {
|
||||
Ok(response) => Ok(response),
|
||||
Err(err) => Err(err),
|
||||
},
|
||||
with_exponential_backoff!(
|
||||
&self.ctx,
|
||||
custom(&self.ctx.net, url.clone())
|
||||
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
||||
.header("accept", "application/json")
|
||||
.header("content-type", "application/json")
|
||||
.body(body.clone())
|
||||
.send()
|
||||
.await
|
||||
),
|
||||
&self.ctx.prt,
|
||||
)
|
||||
.await
|
||||
|
@ -240,16 +211,30 @@ impl<'ctx> DeckClient<'ctx> {
|
|||
card_id: NextcloudCardId,
|
||||
file: &FileHandle,
|
||||
) -> APIResult<Attachment> {
|
||||
let form: multipart::Form = multipart::Form::new();
|
||||
let form = form.text("type", "file");
|
||||
let form = form
|
||||
.file("file", file.as_pathbuf())
|
||||
.await
|
||||
.expect("read file");
|
||||
self.request_with_form(
|
||||
// f!("apps/deck/cards/{card_id}/attachment"),
|
||||
f!("boards/{board_id}/stacks/{stack_id}/cards/{card_id}/attachments"),
|
||||
form,
|
||||
APIResult::new(
|
||||
with_exponential_backoff!(&self.ctx, {
|
||||
let form: multipart::Form = multipart::Form::new();
|
||||
let form = form.text("type", "file");
|
||||
let form = form
|
||||
.file("file", file.as_pathbuf())
|
||||
.await
|
||||
.expect("read file");
|
||||
self.ctx
|
||||
.net
|
||||
.send(
|
||||
self.ctx
|
||||
.net
|
||||
.client()
|
||||
.post(self.url(f!(
|
||||
"boards/{board_id}/stacks/{stack_id}/cards/{card_id}/attachments"
|
||||
)))
|
||||
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
|
||||
.header("accept", "application/json")
|
||||
.multipart(form),
|
||||
)
|
||||
.await
|
||||
}),
|
||||
&self.ctx.prt,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ use crate::{
|
|||
|
||||
pub(crate) struct TrelloClient<'ctx> {
|
||||
ctx: &'ctx FullCtx,
|
||||
// 300 requests per 10 seconds for each API key and 100 requests per 10 second interval for each token
|
||||
}
|
||||
|
||||
impl<'ctx> TrelloClient<'ctx> {
|
||||
|
|
Loading…
Reference in a new issue