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