xdk-rs 0.2.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! HTTP transport for the three response shapes (JSON, multipart, and
//! streaming) with request-header assembly and wire diagnostics emitted as
//! `tracing` events.

use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use bytes::Bytes;
use futures_core::Stream;
use reqwest::multipart;

use crate::error::{Error, Result};

use super::{Client, MultipartOptions, RequestOptions};

/// Target of the wire diagnostics: one `DEBUG` event per request line
/// (`kind = "request"`, `method`, `url`), response status (`kind =
/// "status"`, `status`), response header (`kind = "header"`, `name`,
/// `value`), the end of a response (`kind = "end"`), and each header-override
/// note (`kind = "note"`, message), in the order a subscriber prints them.
pub const WIRE_TARGET: &str = "xdk::wire";

impl Client {
    /// Sends a regular API request and returns the JSON response.
    ///
    /// A success body that is not JSON is returned as a JSON string holding
    /// it, and an empty one as an empty object. A client told to wait on
    /// rate limits (`Config::rate_limit_max_wait`,
    /// [`ClientBuilder::wait_on_rate_limit`](super::ClientBuilder::wait_on_rate_limit))
    /// sleeps through a 429's reset and sends the request once more.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP method is invalid, the request or the
    /// read of its body fails, or the API returns an error status (>= 400).
    pub async fn send_request(&self, options: &RequestOptions) -> Result<serde_json::Value> {
        self.send_request_with(options, self.request_timeout())
            .await
    }

    /// [`Self::send_request`] with `timeout` bounding this one request in
    /// place of the client-level bound.
    pub(crate) async fn send_request_with(
        &self,
        options: &RequestOptions,
        timeout: std::time::Duration,
    ) -> Result<serde_json::Value> {
        match self.send_request_once(options, timeout).await {
            Err(error) => match self.rate_limit_pause(&error) {
                Some(pause) => {
                    tokio::time::sleep(pause).await;
                    self.send_request_once(options, timeout).await
                }
                None => Err(error),
            },
            sent => sent,
        }
    }

    /// One attempt at [`Self::send_request_with`].
    async fn send_request_once(
        &self,
        options: &RequestOptions,
        timeout: std::time::Duration,
    ) -> Result<serde_json::Value> {
        let method = options.method.to_uppercase();
        let method = if method.is_empty() { "GET" } else { &method };
        // Auth-matrix validation lives inside `get_auth_header`, reached
        // through `with_headers`, so each request performs one matrix
        // lookup, not two. The explicit-auth branch there rejects with
        // `Error::AuthMethodMismatch` before any header is produced;
        // `no_auth: true` short-circuits past the call site entirely.
        let url = self.build_url(&options.target)?;

        let req_method = reqwest::Method::from_bytes(method.as_bytes())
            .map_err(|_| Error::InvalidMethod(method.to_string()))?;

        let mut builder = self.http().request(req_method, &url).timeout(timeout);

        // Add body for POST/PUT/PATCH/DELETE; the spec gives some DELETE
        // endpoints a required body (`/2/media/subtitles`, `/2/connections`).
        let sends_body =
            !options.data.is_empty() && matches!(method, "POST" | "PUT" | "PATCH" | "DELETE");
        if sends_body {
            builder = with_body(builder, options);
        }
        let builder = self.with_headers(builder, options, sends_body).await?;
        trace_request(method, &url);

        let resp = builder.send().await?;
        self.record_rate_limit(resp.headers());
        trace_response(resp.status(), resp.headers());
        read_response(resp).await
    }

    /// Sends a multipart request (used for media upload chunks).
    ///
    /// The body and the rate-limit wait follow [`Self::send_request`].
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP method is invalid, file I/O fails,
    /// the request or the read of its body fails, or the API returns an
    /// error status (>= 400).
    pub async fn send_multipart_request(
        &self,
        options: &MultipartOptions,
    ) -> Result<serde_json::Value> {
        match self.send_multipart_once(options).await {
            Err(error) => match self.rate_limit_pause(&error) {
                Some(pause) => {
                    tokio::time::sleep(pause).await;
                    self.send_multipart_once(options).await
                }
                None => Err(error),
            },
            sent => sent,
        }
    }

    /// One attempt at [`Self::send_multipart_request`].
    async fn send_multipart_once(&self, options: &MultipartOptions) -> Result<serde_json::Value> {
        let method = options.request.method.to_uppercase();
        let method = if method.is_empty() { "POST" } else { &method };
        let url = self.build_url(&options.request.target)?;

        let req_method = reqwest::Method::from_bytes(method.as_bytes())
            .map_err(|_| Error::InvalidMethod(method.to_string()))?;

        let mut form = multipart::Form::new();

        // Add file from path or data
        if !options.file_field.is_empty() && !options.file_path.is_empty() {
            let part = multipart::Part::file(&options.file_path)
                .await
                .map_err(|e| Error::io(format!("error opening file: {e}")).with_source(e))?;
            form = form.part(options.file_field.clone(), part);
        } else if !options.file_field.is_empty() && !options.file_data.is_empty() {
            let part = multipart::Part::bytes(options.file_data.clone())
                .file_name(options.file_name.clone());
            form = form.part(options.file_field.clone(), part);
        }

        // Add form fields
        for (key, value) in &options.form_fields {
            form = form.text(key.clone(), value.clone());
        }

        let builder = self
            .http()
            .request(req_method, &url)
            .timeout(self.request_timeout())
            .multipart(form);
        // Multipart's Content-Type is owned by `reqwest`'s multipart builder
        // (it carries the boundary), so the client never sets one here.
        let builder = self.with_headers(builder, &options.request, false).await?;
        trace_request(method, &url);

        let resp = builder.send().await?;
        self.record_rate_limit(resp.headers());
        read_response(resp).await
    }

    /// Opens a streaming request and returns its lines as they arrive.
    ///
    /// The connection stays open until the returned stream is dropped or
    /// the server ends it; the caller decides what to print. Streaming
    /// requests carry no total timeout.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP method is invalid, the request fails,
    /// or the API returns an error status (>= 400).
    pub async fn stream_request(&self, options: &RequestOptions) -> Result<StreamLines> {
        let method = options.method.to_uppercase();
        let method = if method.is_empty() { "GET" } else { &method };
        // Streaming honours the same fail-fast rule as the other two paths:
        // an explicit `--auth X` against an endpoint that doesn't accept `X`
        // rejects in `with_headers` before `builder.send()` opens any socket.
        let url = self.build_url(&options.target)?;

        let req_method = reqwest::Method::from_bytes(method.as_bytes())
            .map_err(|_| Error::InvalidMethod(method.to_string()))?;

        let mut builder = self.http().request(req_method, &url);

        let sends_body = !options.data.is_empty();
        if sends_body {
            builder = with_body(builder, options);
        }
        let builder = self.with_headers(builder, options, sends_body).await?;
        trace_request(method, &url);

        let resp = builder.send().await?;
        self.record_rate_limit(resp.headers());
        trace_response(resp.status(), resp.headers());

        if resp.status().as_u16() >= 400 {
            return Err(api_error(resp).await?);
        }

        Ok(StreamLines {
            body: Box::pin(resp.bytes_stream()),
            buf: Vec::new(),
            done: false,
        })
    }

    /// Adds the headers every request path sends: the caller's own, then
    /// `Authorization`, `User-Agent`, and (when tracing) `X-B3-Flags`, each
    /// left out when the caller already supplied it.
    ///
    /// An auth failure (a mismatch, or no token for the resolved app) is
    /// returned rather than sending the request unauthenticated, which would
    /// surface as a confusing 401 from upstream. `sets_content_type` says
    /// whether the caller of this function set a `Content-Type` of its own,
    /// so the override note covers it.
    async fn with_headers(
        &self,
        mut builder: reqwest::RequestBuilder,
        options: &RequestOptions,
        sets_content_type: bool,
    ) -> Result<reqwest::RequestBuilder> {
        for header in &options.headers {
            if let Some((key, value)) = header.split_once(':') {
                builder = builder.header(key.trim(), value.trim());
            }
        }

        if !options.no_auth && !user_supplied_header(&options.headers, "Authorization") {
            let auth_header = self.get_auth_header(options).await?;
            builder = builder.header("Authorization", auth_header);
        }

        if !user_supplied_header(&options.headers, "User-Agent") {
            builder = builder.header("User-Agent", self.inner.user_agent.as_str());
        }

        if options.trace && !user_supplied_header(&options.headers, "X-B3-Flags") {
            builder = builder.header("X-B3-Flags", "1");
        }

        note_header_overrides(
            &options.headers,
            sets_content_type,
            !options.no_auth,
            options.trace,
        );
        Ok(builder)
    }

    /// How long to sleep before sending a rate-limited request once more:
    /// the time left until the reset the 429 itself named, when this client
    /// waits on rate limits and that time fits its bound.
    fn rate_limit_pause(&self, error: &Error) -> Option<Duration> {
        let max_wait = self.inner.rate_limit_max_wait?;
        let Error::Api {
            status: 429,
            reset_at: Some(reset_at),
            ..
        } = error
        else {
            return None;
        };
        let reset = UNIX_EPOCH + Duration::from_secs(*reset_at);
        let wait = reset.duration_since(SystemTime::now()).unwrap_or_default();
        (wait <= max_wait).then_some(wait)
    }
}

/// Attaches the request body, with the `Content-Type` its shape implies
/// unless the caller supplied one.
fn with_body(
    mut builder: reqwest::RequestBuilder,
    options: &RequestOptions,
) -> reqwest::RequestBuilder {
    if !user_supplied_header(&options.headers, "Content-Type") {
        let content_type = if serde_json::from_str::<serde_json::Value>(&options.data).is_ok() {
            "application/json"
        } else {
            "application/x-www-form-urlencoded"
        };
        builder = builder.header("Content-Type", content_type);
    }
    builder.body(options.data.clone())
}

/// Reads a finished response into the value or the error the caller gets.
///
/// Nothing received is discarded. A success body that is JSON is that value;
/// one that is not is a JSON string holding it, which raw mode prints as text
/// and a typed call refuses to decode; an empty one is an empty object, since
/// X answers several writes with no body at all.
async fn read_response(resp: reqwest::Response) -> Result<serde_json::Value> {
    if resp.status().as_u16() >= 400 {
        return Err(api_error(resp).await?);
    }
    let body = resp.text().await?;
    if body.is_empty() {
        return Ok(serde_json::json!({}));
    }
    Ok(serde_json::from_str(&body).unwrap_or(serde_json::Value::String(body)))
}

/// The [`Error::Api`] an error response becomes.
///
/// A JSON body is carried in its compact form and any other body as sent, so
/// an HTML error page reaches the message. A 429 carries the reset its own
/// headers name, never the window an earlier response reported.
///
/// # Errors
///
/// Returns the transport error when the body cannot be read.
async fn api_error(resp: reqwest::Response) -> Result<Error> {
    let status = resp.status().as_u16();
    let reset_at = (status == 429)
        .then(|| super::RateLimit::from_headers(resp.headers()))
        .flatten()
        .and_then(|window| window.reset_at);
    let body = resp.text().await?;
    let body = match serde_json::from_str::<serde_json::Value>(&body) {
        Ok(json) => json.to_string(),
        Err(_) if body.is_empty() => "{}".to_string(),
        Err(_) => body,
    };
    Ok(Error::Api {
        status,
        body,
        reset_at,
    })
}

type ByteStream = Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send>>;

/// Lines of a streaming response, delivered as they arrive.
///
/// A [`Stream`] of lines; [`Self::next_line`] is the same thing without a
/// stream combinator. Empty keep-alive lines are skipped. Dropping the
/// stream closes the connection.
pub struct StreamLines {
    body: ByteStream,
    buf: Vec<u8>,
    done: bool,
}

impl StreamLines {
    /// The next line, or `None` once the server has ended the stream.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] when the connection fails mid-stream.
    pub async fn next_line(&mut self) -> Result<Option<String>> {
        std::future::poll_fn(|cx| Pin::new(&mut *self).poll_next(cx))
            .await
            .transpose()
    }

    /// Removes and returns the first complete line in the buffer, without
    /// its `\n` and any `\r` before it.
    fn take_line(&mut self) -> Option<String> {
        let end = self.buf.iter().position(|&b| b == b'\n')?;
        let mut line: Vec<u8> = self.buf.drain(..=end).collect();
        line.pop();
        if line.last() == Some(&b'\r') {
            line.pop();
        }
        Some(String::from_utf8_lossy(&line).into_owned())
    }
}

impl std::fmt::Debug for StreamLines {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StreamLines").finish_non_exhaustive()
    }
}

impl Stream for StreamLines {
    type Item = Result<String>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        loop {
            if let Some(line) = self.take_line() {
                if line.is_empty() {
                    continue;
                }
                return Poll::Ready(Some(Ok(line)));
            }
            if self.done {
                let rest = std::mem::take(&mut self.buf);
                let line = String::from_utf8_lossy(&rest);
                let line = line.trim_end_matches('\r').to_string();
                return Poll::Ready((!line.is_empty()).then_some(Ok(line)));
            }
            match self.body.as_mut().poll_next(cx) {
                Poll::Pending => return Poll::Pending,
                Poll::Ready(None) => self.done = true,
                Poll::Ready(Some(Ok(chunk))) => self.buf.extend_from_slice(&chunk),
                Poll::Ready(Some(Err(e))) => {
                    self.done = true;
                    return Poll::Ready(Some(Err(Error::io(e.to_string()).with_source(e))));
                }
            }
        }
    }
}

/// Returns `true` when any `"Name: Value"` entry in `headers` matches `name`
/// on a case-insensitive (ASCII) compare of the trimmed key.
///
/// Used by the three send paths to suppress the client's own append of any header
/// the caller already supplied. `reqwest::RequestBuilder::header` uses
/// `HeaderMap::append`, so without this guard a header the caller passed
/// (Authorization, User-Agent, Content-Type, X-B3-Flags) would go out
/// alongside the client's own value. HTTP allows multi-valued headers in the
/// abstract; many servers (and HTTP signatures like Authorization) treat
/// duplicates as undefined.
fn user_supplied_header(headers: &[String], name: &str) -> bool {
    headers
        .iter()
        .filter_map(|h| h.split_once(':'))
        .any(|(key, _)| key.trim().eq_ignore_ascii_case(name))
}

/// Emits one wire note for each client-added header that was suppressed because
/// the caller already supplied it via `options.headers`.
///
/// The four client-added headers are `Content-Type` (only when there's a body
/// to send), `Authorization` (only when `no_auth` is false), `User-Agent`
/// (always), and `X-B3-Flags` (only when `trace` is true). The corresponding
/// `would_*` booleans gate which headers are eligible for a note at this
/// call site; `send_multipart_request` passes `would_set_content_type: false`
/// because reqwest's multipart builder owns the Content-Type.
fn note_header_overrides(
    headers: &[String],
    would_set_content_type: bool,
    would_set_auth: bool,
    would_set_trace: bool,
) {
    let candidates: [(&str, bool); 4] = [
        ("Content-Type", would_set_content_type),
        ("Authorization", would_set_auth),
        ("User-Agent", true),
        ("X-B3-Flags", would_set_trace),
    ];
    for (name, xurl_wanted) in candidates {
        if xurl_wanted && user_supplied_header(headers, name) {
            tracing::debug!(
                target: WIRE_TARGET,
                kind = "note",
                header = name,
                "user-supplied header detected; the default is not appended"
            );
        }
    }
}

/// Emits the request line as one wire event.
fn trace_request(method: &str, url: &str) {
    tracing::debug!(target: WIRE_TARGET, kind = "request", method, url);
}

/// Emits the response status, each header, and the end marker as wire
/// events, in the order `xr --verbose` prints them.
fn trace_response(status: reqwest::StatusCode, headers: &reqwest::header::HeaderMap) {
    tracing::debug!(target: WIRE_TARGET, kind = "status", status = %status);
    for (key, value) in headers {
        tracing::debug!(
            target: WIRE_TARGET,
            kind = "header",
            name = %key,
            value = value.to_str().unwrap_or("")
        );
    }
    tracing::debug!(target: WIRE_TARGET, kind = "end");
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── user_supplied_header tests ───────────────────────────────

    #[test]
    fn user_supplied_header_detects_canonical_case() {
        let headers = vec!["Authorization: Bearer foo".to_string()];
        assert!(user_supplied_header(&headers, "Authorization"));
    }

    #[test]
    fn user_supplied_header_is_case_insensitive_on_input_key() {
        for raw in [
            "authorization: Bearer foo",
            "AUTHORIZATION: Bearer foo",
            "aUtHoRiZaTiOn: Bearer foo",
        ] {
            assert!(
                user_supplied_header(&[raw.to_string()], "Authorization"),
                "did not detect: {raw}"
            );
        }
    }

    #[test]
    fn user_supplied_header_is_case_insensitive_on_query_name() {
        let headers = vec!["Authorization: Bearer foo".to_string()];
        for query in ["authorization", "AUTHORIZATION", "aUtHoRiZaTiOn"] {
            assert!(
                user_supplied_header(&headers, query),
                "did not detect with query: {query}"
            );
        }
    }

    #[test]
    fn user_supplied_header_ignores_surrounding_whitespace_on_key() {
        let headers = vec!["  Authorization  : Bearer foo".to_string()];
        assert!(user_supplied_header(&headers, "Authorization"));
    }

    #[test]
    fn user_supplied_header_false_for_empty_list() {
        let headers: Vec<String> = Vec::new();
        assert!(!user_supplied_header(&headers, "Authorization"));
        assert!(!user_supplied_header(&headers, "User-Agent"));
    }

    #[test]
    fn user_supplied_header_does_not_match_substring_keys() {
        let headers = vec![
            "Cookie: session=abc".to_string(),
            "X-Authorization-Hint: ignored".to_string(),
        ];
        assert!(!user_supplied_header(&headers, "Authorization"));
    }

    #[test]
    fn user_supplied_header_false_for_unparseable_entry() {
        let headers = vec!["malformed-no-colon".to_string()];
        assert!(!user_supplied_header(&headers, "Authorization"));
    }

    #[test]
    fn user_supplied_header_detects_each_xurl_added_header() {
        let headers = vec![
            "Content-Type: application/xml".to_string(),
            "Authorization: Bearer foo".to_string(),
            "User-Agent: custom/1.0".to_string(),
            "X-B3-Flags: 0".to_string(),
        ];
        assert!(user_supplied_header(&headers, "Content-Type"));
        assert!(user_supplied_header(&headers, "Authorization"));
        assert!(user_supplied_header(&headers, "User-Agent"));
        assert!(user_supplied_header(&headers, "X-B3-Flags"));
    }
}