dropbox_sdk/client_trait.rs
1// Copyright (c) 2019-2025 Dropbox, Inc.
2
3//! Everything needed to implement your HTTP client.
4
5use std::io::Read;
6use std::sync::Arc;
7pub use crate::client_trait_common::{HttpRequest, TeamSelect};
8use crate::Error;
9
10/// The base HTTP synchronous client trait.
11pub trait HttpClient: Sync {
12 /// The concrete type of request supported by the client.
13 type Request: HttpRequest + Send;
14
15 /// Make a HTTP request.
16 fn execute(
17 &self,
18 request: Self::Request,
19 body: &[u8],
20 ) -> Result<HttpRequestResultRaw, Error>;
21
22 /// Create a new request instance for the given URL. It should be a POST request.
23 fn new_request(&self, url: &str) -> Self::Request;
24
25 /// Attempt to update the current authentication token. The previously fetched token is given
26 /// as a way to avoid repeat updates in case of a race. If the update is successful, return
27 /// `true` and the current request will be retried with a newly-fetched token. Return `false` if
28 /// authentication is not supported, or return an error if the update operation fails.
29 fn update_token(&self, _old_token: Arc<String>) -> Result<bool, Error> {
30 Ok(false)
31 }
32
33 /// The client's current authentication token, if any.
34 fn token(&self) -> Option<Arc<String>> {
35 None
36 }
37
38 /// The currently set path root, if any.
39 fn path_root(&self) -> Option<&str> {
40 None
41 }
42
43 /// The alternate user or team context currently set, if any.
44 fn team_select(&self) -> Option<&TeamSelect> {
45 None
46 }
47}
48
49/// Marker trait to indicate that a HTTP client supports unauthenticated routes.
50pub trait NoauthClient: HttpClient {}
51
52/// Marker trait to indicate that a HTTP client supports User authentication.
53/// Team authentication works by adding a `Authorization: Bearer <TOKEN>` header.
54pub trait UserAuthClient: HttpClient {}
55
56/// Marker trait to indicate that a HTTP client supports Team authentication.
57/// Team authentication works by adding a `Authorization: Bearer <TOKEN>` header, and optionally a
58/// `Dropbox-API-Select-Admin` or `Dropbox-API-Select-User` header.
59pub trait TeamAuthClient: HttpClient {}
60
61/// Marker trait to indicate that a HTTP client supports App authentication.
62/// App authentication works by adding a `Authorization: Basic <base64(APP_KEY:APP_SECRET)>` header
63/// to the HTTP request.
64pub trait AppAuthClient: HttpClient {}
65
66/// The raw response from the server, including a sync streaming response body.
67pub struct HttpRequestResultRaw {
68 /// HTTP response code.
69 pub status: u16,
70
71 /// The value of the `Dropbox-API-Result` header, if present.
72 pub result_header: Option<String>,
73
74 /// The value of the `Content-Length` header in the response, if present.
75 pub content_length: Option<u64>,
76
77 /// The response body stream.
78 pub body: Box<dyn Read + Send>,
79}
80
81/// The response from the server, parsed into a given type, including a body stream if it is from
82/// a Download style request.
83pub struct HttpRequestResult<T> {
84 /// The API result, parsed into the given type.
85 pub result: T,
86
87 /// The value of the `Content-Length` header in the response, if any. Only expected to not be
88 /// `None` if `body` is also not `None`.
89 pub content_length: Option<u64>,
90
91 /// The response body stream, if any. Only expected to not be `None` for
92 /// [`Style::Download`](crate::client_trait_common::Style::Download) endpoints.
93 pub body: Option<Box<dyn Read>>,
94}
95