Skip to content

Commit 631ed25

Browse files
committed
implement AppAuthClient for default HTTP clients
1 parent 0ad6c7f commit 631ed25

File tree

4 files changed

+84
-5
lines changed

4 files changed

+84
-5
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ The authentication type is designated by implementing a marker trait in
6969
addition to the base `HttpClient` trait: one of `NoauthClient`,
7070
`UserAuthClient`, `TeamAuthClient`, or `AppAuthClient`.
7171

72-
The default client has implementations of all of these (except for
73-
`AppAuthClient` currently). They all share a common implementation and differ
74-
only in which HTTP headers they add to the request.
72+
The default client has implementations of all of these. They all share a common
73+
implementation and differ only in which HTTP headers they add to the request.
7574

7675
[authentication types]: https://www.dropbox.com/developers/reference/auth-types
7776

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# v0.19.0-beta3
2+
xxxx-yy-zz
3+
* added implementations of AppAuthClient to default HTTP clients
4+
15
# v0.19.0-beta2
26
2024-11-05
37
* renamed sync_routes_default feature to sync_routes_in_root

src/default_async_client.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::str::FromStr;
1515
use std::sync::Arc;
1616
use bytes::Bytes;
1717
use futures::{FutureExt, TryFutureExt, TryStreamExt};
18-
use crate::async_client_trait::{HttpClient, HttpRequestResultRaw, NoauthClient, TeamAuthClient, UserAuthClient};
18+
use crate::async_client_trait::{AppAuthClient, HttpClient, HttpRequestResultRaw, NoauthClient, TeamAuthClient, UserAuthClient};
1919
use crate::client_trait_common::{HttpRequest, TeamSelect};
2020
use crate::default_client_common::impl_set_path_root;
2121
use crate::Error;
@@ -156,6 +156,44 @@ impl HttpClient for TeamAuthDefaultClient {
156156

157157
impl TeamAuthClient for TeamAuthDefaultClient {}
158158

159+
/// Default HTTP client using App authorization.
160+
#[derive(Debug)]
161+
pub struct AppAuthDefaultClient {
162+
inner: ReqwestClient,
163+
path_root: Option<String>,
164+
auth: String,
165+
}
166+
167+
impl AppAuthDefaultClient {
168+
/// Create a new App auth client using the given app key and secret, which can be found in the Dropbox app console.
169+
pub fn new(app_key: &str, app_secret: &str) -> Self {
170+
use base64::prelude::*;
171+
let encoded = BASE64_STANDARD.encode(format!("{app_key}:{app_secret}"));
172+
Self {
173+
inner: ReqwestClient::default(),
174+
path_root: None,
175+
auth: format!("Basic {encoded}"),
176+
}
177+
}
178+
179+
impl_set_path_root!(self);
180+
}
181+
182+
impl HttpClient for AppAuthDefaultClient {
183+
type Request = ReqwestRequest;
184+
185+
fn execute(&self, request: Self::Request, body: Bytes) -> impl Future<Output=Result<HttpRequestResultRaw, Error>> + Send {
186+
self.inner.execute(request, body)
187+
}
188+
189+
fn new_request(&self, url: &str) -> Self::Request {
190+
self.inner.new_request(url)
191+
.set_header("Authorization", &self.auth)
192+
}
193+
}
194+
195+
impl AppAuthClient for AppAuthDefaultClient {}
196+
159197
/// Default HTTP client for unauthenticated API calls.
160198
#[derive(Debug, Default)]
161199
pub struct NoauthDefaultClient {

src/default_client.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::fmt::Write;
1818
use std::str::FromStr;
1919
use std::sync::Arc;
2020
use futures::FutureExt;
21-
use crate::client_trait::{HttpClient, HttpRequestResultRaw, NoauthClient, TeamAuthClient, UserAuthClient};
21+
use crate::client_trait::{AppAuthClient, HttpClient, HttpRequestResultRaw, NoauthClient, TeamAuthClient, UserAuthClient};
2222
use crate::client_trait_common::{HttpRequest, TeamSelect};
2323
use crate::default_client_common::impl_set_path_root;
2424

@@ -145,6 +145,44 @@ impl HttpClient for TeamAuthDefaultClient {
145145

146146
impl TeamAuthClient for TeamAuthDefaultClient {}
147147

148+
/// Default HTTP client using App authorization.
149+
#[derive(Debug)]
150+
pub struct AppAuthDefaultClient {
151+
inner: UreqClient,
152+
path_root: Option<String>,
153+
auth: String,
154+
}
155+
156+
impl AppAuthDefaultClient {
157+
/// Create a new App auth client using the given app key and secret, which can be found in the Dropbox app console.
158+
pub fn new(app_key: &str, app_secret: &str) -> Self {
159+
use base64::prelude::*;
160+
let encoded = BASE64_STANDARD.encode(format!("{app_key}:{app_secret}"));
161+
Self {
162+
inner: UreqClient::default(),
163+
path_root: None,
164+
auth: format!("Basic {encoded}"),
165+
}
166+
}
167+
168+
impl_set_path_root!(self);
169+
}
170+
171+
impl HttpClient for AppAuthDefaultClient {
172+
type Request = UreqRequest;
173+
174+
fn execute(&self, request: Self::Request, body: &[u8]) -> Result<HttpRequestResultRaw, Error> {
175+
self.inner.execute(request, body)
176+
}
177+
178+
fn new_request(&self, url: &str) -> Self::Request {
179+
self.inner.new_request(url)
180+
.set_header("Authorization", &self.auth)
181+
}
182+
}
183+
184+
impl AppAuthClient for AppAuthDefaultClient {}
185+
148186
/// Default HTTP client for unauthenticated API calls.
149187
#[derive(Debug, Default)]
150188
pub struct NoauthDefaultClient {

0 commit comments

Comments
 (0)