xdk-rs 0.0.0

Async Rust client for the X (Twitter) API: OAuth1, OAuth2 PKCE, bearer tokens, media upload, streaming. xdk is X's SDK name; this is an independent project, not affiliated with X Corp.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! An in-process X API for tests that must not spend credits.
//!
//! [`MockX::start`] binds a local server that answers every endpoint the
//! shortcut layer declares with the fixture responses the response types are
//! validated against, so a `read_post` or `search_posts` against it
//! deserializes into the same `Post` a live call would. Every response
//! carries a rate-limit window, so [`Client::last_rate_limit`] reads
//! something too.
//!
//! Enable the feature in a test profile only, so a release build pulls none
//! of the mock's dependencies:
//!
//! ```toml
//! [dev-dependencies]
//! xdk-rs = { version = "0.1", features = ["testing"] }
//! ```
//!
//! ```rust,no_run
//! use xdk::testing::MockX;
//!
//! #[tokio::main]
//! async fn main() -> xdk::Result<()> {
//!     let mock = MockX::start().await;
//!     let client = mock.app_client()?;
//!
//!     let posts = client.search_posts("rust", 10).send().await?;
//!     assert_eq!(posts.data.len(), 2);
//!
//!     let seen = mock.requests().await;
//!     assert_eq!(seen[0].path, "/2/tweets/search/recent");
//!     Ok(())
//! }
//! ```
//!
//! A route the fixtures do not cover, or a failure to rehearse, is stubbed
//! with [`MockX::stub`]; a stub outranks the seeded route for the same path.
//! The fixture file also carries the two failure shapes the API documents,
//! `api_error` (`code` and `message`) and `api_problem` (a problem+json
//! body), so [`MockX::fixture`] hands a real failure body to a stub.

use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};

use serde_json::Value;
use wiremock::matchers::{method, path_regex};
use wiremock::{Mock, MockServer, ResponseTemplate};

use crate::api::auth_matrix::{Endpoint, endpoints};
use crate::api::{Client, ClientBuilder};
use crate::auth::OAuth2Credential;

/// The bearer token [`MockX::app_client`] sends; the mock accepts anything.
pub const MOCK_BEARER_TOKEN: &str = "mock-bearer-token";

const FIXTURES: &str = include_str!("../../tests/fixtures/openapi/example_responses.json");

/// The `x-rate-limit-*` window every seeded response reports.
const RATE_LIMIT: u32 = 450;
const RATE_LIMIT_REMAINING: u32 = 449;
const RATE_LIMIT_WINDOW_SECS: u64 = 900;

/// A seeded route: the declared endpoint, the fixture that answers it, and
/// the status it answers with. The method and the path pattern come from the
/// declaration, so no route spells a path.
struct Route {
    endpoint: Endpoint,
    fixture: &'static str,
    status: u16,
}

/// One entry per declared endpoint, in declaration order.
const ROUTES: &[Route] = &[
    Route {
        endpoint: endpoints::CREATE_POST,
        fixture: "post_single",
        status: 201,
    },
    Route {
        endpoint: endpoints::READ_POST,
        fixture: "post_single",
        status: 200,
    },
    Route {
        endpoint: endpoints::DELETE_POST,
        fixture: "action_deleted",
        status: 200,
    },
    Route {
        endpoint: endpoints::SEARCH_POSTS,
        fixture: "post_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_ME,
        fixture: "user_single",
        status: 200,
    },
    Route {
        endpoint: endpoints::LOOKUP_USER,
        fixture: "user_single",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_TIMELINE,
        fixture: "post_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_MENTIONS,
        fixture: "post_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_FOLLOWERS,
        fixture: "user_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_LIKED_POSTS,
        fixture: "post_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::LIKE_POST,
        fixture: "action_liked",
        status: 200,
    },
    Route {
        endpoint: endpoints::UNLIKE_POST,
        fixture: "action_liked",
        status: 200,
    },
    Route {
        endpoint: endpoints::REPOST,
        fixture: "action_retweeted",
        status: 200,
    },
    Route {
        endpoint: endpoints::UNREPOST,
        fixture: "action_retweeted",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_BOOKMARKS,
        fixture: "post_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::BOOKMARK,
        fixture: "action_bookmarked",
        status: 200,
    },
    Route {
        endpoint: endpoints::UNBOOKMARK,
        fixture: "action_bookmarked",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_FOLLOWING,
        fixture: "user_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::FOLLOW_USER,
        fixture: "action_following",
        status: 200,
    },
    Route {
        endpoint: endpoints::UNFOLLOW_USER,
        fixture: "action_following",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_MUTED,
        fixture: "user_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::MUTE_USER,
        fixture: "action_muting",
        status: 200,
    },
    Route {
        endpoint: endpoints::UNMUTE_USER,
        fixture: "action_muting",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_BLOCKED,
        fixture: "user_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::BLOCK_USER,
        fixture: "action_blocking",
        status: 200,
    },
    Route {
        endpoint: endpoints::UNBLOCK_USER,
        fixture: "action_blocking",
        status: 200,
    },
    Route {
        endpoint: endpoints::SEND_DM,
        fixture: "dm_sent",
        status: 201,
    },
    Route {
        endpoint: endpoints::GET_DM_EVENTS,
        fixture: "dm_event_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_USAGE,
        fixture: "usage",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_USAGE_CREDITS,
        fixture: "usage_credits",
        status: 200,
    },
    Route {
        endpoint: endpoints::MEDIA_UPLOAD,
        fixture: "media_upload_init",
        status: 200,
    },
    Route {
        endpoint: endpoints::MEDIA_UPLOAD_STATUS,
        fixture: "media_upload_status",
        status: 200,
    },
    Route {
        endpoint: endpoints::MEDIA_UPLOAD_INITIALIZE,
        fixture: "media_upload_init",
        status: 200,
    },
    Route {
        endpoint: endpoints::MEDIA_UPLOAD_APPEND,
        fixture: "media_upload_append",
        status: 200,
    },
    Route {
        endpoint: endpoints::MEDIA_UPLOAD_FINALIZE,
        fixture: "media_upload_status",
        status: 200,
    },
    Route {
        endpoint: endpoints::GET_CHAT_MODERATORS,
        fixture: "user_list",
        status: 200,
    },
    Route {
        endpoint: endpoints::ADD_CHAT_MODERATOR,
        fixture: "chat_moderators",
        status: 200,
    },
    Route {
        endpoint: endpoints::REMOVE_CHAT_MODERATOR,
        fixture: "chat_moderators",
        status: 200,
    },
];

/// The regex that matches the paths `path` renders to: a `{username}`
/// segment widens to anything but a slash, every other parameter to digits.
fn pattern(path: &str) -> String {
    let widened: Vec<String> = path
        .split('/')
        .map(|segment| match segment {
            "{username}" => "[^/]+".to_string(),
            s if s.starts_with('{') && s.ends_with('}') => "[0-9]+".to_string(),
            s => s.to_string(),
        })
        .collect();
    format!("^{}$", widened.join("/"))
}

fn fixtures() -> &'static Value {
    static PARSED: OnceLock<Value> = OnceLock::new();
    PARSED.get_or_init(|| {
        serde_json::from_str(FIXTURES).expect("the bundled fixture file is valid JSON")
    })
}

/// A request the mock received, as a test asserts on it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Received {
    /// The HTTP method.
    pub method: String,
    /// The path, without the query string.
    pub path: String,
    /// The query string, without the leading `?`.
    pub query: Option<String>,
    /// Every header, in arrival order; the `authorization` header is among them.
    pub headers: Vec<(String, String)>,
    /// The raw body.
    pub body: Vec<u8>,
}

/// A local X API that answers from the crate's fixtures.
///
/// Dropping it stops the server.
pub struct MockX {
    server: MockServer,
}

impl std::fmt::Debug for MockX {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MockX")
            .field("base_url", &self.server.uri())
            .finish()
    }
}

impl MockX {
    /// Binds a server on a free local port and seeds every fixture route.
    pub async fn start() -> Self {
        let server = MockServer::start().await;
        let reset_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs() + RATE_LIMIT_WINDOW_SECS)
            .unwrap_or(RATE_LIMIT_WINDOW_SECS);
        for route in ROUTES {
            let template = ResponseTemplate::new(route.status)
                .set_body_json(fixtures()[route.fixture].clone())
                .insert_header("x-rate-limit-limit", RATE_LIMIT.to_string().as_str())
                .insert_header(
                    "x-rate-limit-remaining",
                    RATE_LIMIT_REMAINING.to_string().as_str(),
                )
                .insert_header("x-rate-limit-reset", reset_at.to_string().as_str());
            Mock::given(method(route.endpoint.method))
                .and(path_regex(pattern(route.endpoint.path)))
                .respond_with(template)
                .mount(&server)
                .await;
        }
        Self { server }
    }

    /// The origin to point a client at, `http://127.0.0.1:<port>`.
    #[must_use]
    pub fn base_url(&self) -> String {
        self.server.uri()
    }

    /// A client builder already pointed at the mock; add the credential.
    pub fn builder(&self) -> ClientBuilder {
        Client::builder().base_url(self.base_url())
    }

    /// A bearer-token client: app-only endpoints such as search and lookups,
    /// refused by the auth matrix for user-context endpoints exactly as a live
    /// bearer would be.
    ///
    /// # Errors
    ///
    /// Returns [`crate::Error::Http`] when the HTTP client cannot be built.
    pub fn app_client(&self) -> crate::Result<Client> {
        self.builder().bearer(MOCK_BEARER_TOKEN).build()
    }

    /// A client holding an `OAuth2` user credential and a bearer token, so
    /// every shortcut passes the auth matrix. The credential never expires,
    /// so no refresh is attempted.
    ///
    /// # Errors
    ///
    /// Returns [`crate::Error::Http`] when the HTTP client cannot be built.
    pub fn user_client(&self) -> crate::Result<Client> {
        self.builder()
            .bearer(MOCK_BEARER_TOKEN)
            .oauth2(OAuth2Credential {
                client_id: "mock-client-id".to_string(),
                client_secret: "mock-client-secret".to_string(),
                access_token: "mock-access-token".to_string(),
                refresh_token: None,
                expires_at: None,
            })
            .build()
    }

    /// Answers `method` requests whose path matches the regex `path_pattern`
    /// with `status` and the JSON `body`, ahead of any seeded route.
    pub async fn stub(&self, method_name: &str, path_pattern: &str, status: u16, body: Value) {
        Mock::given(method(method_name))
            .and(path_regex(path_pattern))
            .respond_with(ResponseTemplate::new(status).set_body_json(body))
            .with_priority(1)
            .mount(&self.server)
            .await;
    }

    /// The seeded body for a fixture name such as `post_single`, or `None`.
    #[must_use]
    pub fn fixture(name: &str) -> Option<Value> {
        fixtures().get(name).cloned()
    }

    /// Every request received so far, in arrival order.
    pub async fn requests(&self) -> Vec<Received> {
        self.server
            .received_requests()
            .await
            .unwrap_or_default()
            .into_iter()
            .map(|req| Received {
                method: req.method.to_string(),
                path: req.url.path().to_string(),
                query: req.url.query().map(str::to_string),
                headers: req
                    .headers
                    .iter()
                    .map(|(name, value)| {
                        (
                            name.to_string(),
                            String::from_utf8_lossy(value.as_bytes()).into_owned(),
                        )
                    })
                    .collect(),
                body: req.body,
            })
            .collect()
    }
}