xurl-rs 1.1.0

A fast, ergonomic CLI for the X (Twitter) API — OAuth1/2, Bearer, media upload, streaming
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
/// HTTP request building and execution for the X API.
///
/// Mirrors the Go `ApiClient` — builds requests with auth headers,
/// handles regular/streaming/multipart responses.
use std::io::{BufRead, BufReader};
use std::time::Duration;

use reqwest::blocking::{Client, multipart};

use crate::auth::Auth;
use crate::config::Config;
use crate::error::{Result, XurlError};

/// Common options for API requests.
#[derive(Debug, Clone, Default)]
pub struct RequestOptions {
    pub method: String,
    pub endpoint: String,
    pub headers: Vec<String>,
    pub data: String,
    pub auth_type: String,
    pub username: String,
    pub verbose: bool,
    pub trace: bool,
}

/// Options specific to multipart requests.
#[derive(Debug, Clone)]
pub struct MultipartOptions {
    pub request: RequestOptions,
    pub form_fields: std::collections::HashMap<String, String>,
    pub file_field: String,
    pub file_path: String,
    pub file_name: String,
    pub file_data: Vec<u8>,
}

/// Handles API requests with authentication.
pub struct ApiClient<'a> {
    base_url: String,
    client: Client,
    auth: &'a mut Auth,
}

impl<'a> ApiClient<'a> {
    /// Creates a new `ApiClient`.
    pub fn new(config: &Config, auth: &'a mut Auth) -> Self {
        let client = Client::builder()
            .timeout(Duration::from_secs(30))
            .build()
            .unwrap_or_else(|_| Client::new());

        Self {
            base_url: config.api_base_url.clone(),
            client,
            auth,
        }
    }

    /// Builds the full URL from an endpoint (public accessor for command layer).
    #[must_use]
    pub fn build_url_public(&self, endpoint: &str) -> String {
        self.build_url(endpoint)
    }

    /// Builds the full URL from an endpoint.
    fn build_url(&self, endpoint: &str) -> String {
        if endpoint.to_lowercase().starts_with("http") {
            return endpoint.to_string();
        }

        let mut url = self.base_url.clone();
        if !url.ends_with('/') {
            url.push('/');
        }
        if let Some(stripped) = endpoint.strip_prefix('/') {
            url.push_str(stripped);
        } else {
            url.push_str(endpoint);
        }
        url
    }

    /// Sends a regular API request and returns the JSON response.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP method is invalid, the request fails,
    /// or the API returns an error status (>= 400).
    pub fn send_request(&mut self, options: &RequestOptions) -> Result<serde_json::Value> {
        let method = options.method.to_uppercase();
        let method = if method.is_empty() { "GET" } else { &method };
        let url = self.build_url(&options.endpoint);

        // Build the request
        let req_method = reqwest::Method::from_bytes(method.as_bytes())
            .map_err(|_| XurlError::InvalidMethod(method.to_string()))?;

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

        // Add body for POST/PUT/PATCH
        if !options.data.is_empty() && (method == "POST" || method == "PUT" || method == "PATCH") {
            // Detect content type
            if serde_json::from_str::<serde_json::Value>(&options.data).is_ok() {
                builder = builder
                    .header("Content-Type", "application/json")
                    .body(options.data.clone());
            } else {
                builder = builder
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .body(options.data.clone());
            }
        }

        // Add custom headers
        for header in &options.headers {
            if let Some((key, value)) = header.split_once(':') {
                builder = builder.header(key.trim(), value.trim());
            }
        }

        // Add auth header
        if let Ok(auth_header) =
            self.get_auth_header(method, &url, &options.auth_type, &options.username)
        {
            builder = builder.header("Authorization", auth_header);
        }

        // Add common headers
        builder = builder.header("User-Agent", format!("xurl/{}", env!("CARGO_PKG_VERSION")));

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

        if options.verbose {
            eprintln!("\x1b[1;34m> {method}\x1b[0m {url}");
        }

        let resp = builder.send()?;

        if options.verbose {
            eprintln!("\x1b[1;31m< {}\x1b[0m", resp.status());
            for (key, value) in resp.headers() {
                eprintln!(
                    "\x1b[1;32m< {}\x1b[0m: {}",
                    key,
                    value.to_str().unwrap_or("")
                );
            }
            eprintln!();
        }

        let status = resp.status();
        let body = resp.text().unwrap_or_default();

        let json: serde_json::Value = if body.is_empty() {
            serde_json::json!({})
        } else if let Ok(v) = serde_json::from_str(&body) {
            v
        } else {
            if status.as_u16() >= 400 {
                return Err(XurlError::Http(format!("HTTP error: {status}")));
            }
            serde_json::json!({})
        };

        if status.as_u16() >= 400 {
            return Err(XurlError::api(json.to_string()));
        }

        Ok(json)
    }

    /// Sends a multipart request (used for media upload chunks).
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP method is invalid, file I/O fails,
    /// the request fails, or the API returns an error status (>= 400).
    pub fn send_multipart_request(
        &mut 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.endpoint);

        let req_method = reqwest::Method::from_bytes(method.as_bytes())
            .map_err(|_| XurlError::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)
                .map_err(|e| XurlError::Io(format!("error opening file: {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 mut builder = self.client.request(req_method, &url).multipart(form);

        // Add custom headers
        for header in &options.request.headers {
            if let Some((key, value)) = header.split_once(':') {
                builder = builder.header(key.trim(), value.trim());
            }
        }

        // Add auth header
        if let Ok(auth_header) = self.get_auth_header(
            method,
            &url,
            &options.request.auth_type,
            &options.request.username,
        ) {
            builder = builder.header("Authorization", auth_header);
        }

        builder = builder.header("User-Agent", format!("xurl/{}", env!("CARGO_PKG_VERSION")));

        if options.request.trace {
            builder = builder.header("X-B3-Flags", "1");
        }

        if options.request.verbose {
            eprintln!("\x1b[1;34m> {method}\x1b[0m {url}");
        }

        let resp = builder.send()?;
        let status = resp.status();
        let body = resp.text().unwrap_or_default();

        let json: serde_json::Value = if body.is_empty() {
            serde_json::json!({})
        } else {
            serde_json::from_str(&body).unwrap_or(serde_json::json!({}))
        };

        if status.as_u16() >= 400 {
            return Err(XurlError::api(json.to_string()));
        }

        Ok(json)
    }

    /// Sends a streaming request — reads lines until EOF.
    ///
    /// Note: The binary uses `stream_request_with_output` in `cli::commands`
    /// for output-format awareness. This method is retained for library usage
    /// and tests.
    ///
    /// # Errors
    ///
    /// Returns an error if the HTTP method is invalid, the request fails,
    /// the API returns an error status (>= 400), or a read error occurs.
    #[allow(dead_code)] // Public library API — used by consumers and integration tests
    pub fn stream_request(&mut self, options: &RequestOptions) -> Result<()> {
        let method = options.method.to_uppercase();
        let method = if method.is_empty() { "GET" } else { &method };
        let url = self.build_url(&options.endpoint);

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

        let mut builder = Client::builder()
            .timeout(None)
            .build()
            .unwrap_or_else(|_| Client::new())
            .request(req_method, &url);

        if !options.data.is_empty() {
            if serde_json::from_str::<serde_json::Value>(&options.data).is_ok() {
                builder = builder
                    .header("Content-Type", "application/json")
                    .body(options.data.clone());
            } else {
                builder = builder
                    .header("Content-Type", "application/x-www-form-urlencoded")
                    .body(options.data.clone());
            }
        }

        for header in &options.headers {
            if let Some((key, value)) = header.split_once(':') {
                builder = builder.header(key.trim(), value.trim());
            }
        }

        if let Ok(auth_header) =
            self.get_auth_header(method, &url, &options.auth_type, &options.username)
        {
            builder = builder.header("Authorization", auth_header);
        }

        builder = builder.header("User-Agent", format!("xurl/{}", env!("CARGO_PKG_VERSION")));

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

        if options.verbose {
            eprintln!("\x1b[1;34m> {method}\x1b[0m {url}");
        }

        eprintln!("Connecting to streaming endpoint: {}", options.endpoint);

        let resp = builder.send()?;

        if options.verbose {
            eprintln!("\x1b[1;31m< {}\x1b[0m", resp.status());
            for (key, value) in resp.headers() {
                eprintln!(
                    "\x1b[1;32m< {}\x1b[0m: {}",
                    key,
                    value.to_str().unwrap_or("")
                );
            }
            eprintln!();
        }

        if resp.status().as_u16() >= 400 {
            let body = resp.text().unwrap_or_default();
            if let Ok(json) = serde_json::from_str::<serde_json::Value>(&body) {
                return Err(XurlError::api(json.to_string()));
            }
            return Err(XurlError::api(body));
        }

        eprintln!("--- Streaming response started ---");
        eprintln!("--- Press Ctrl+C to stop ---");

        let reader = BufReader::with_capacity(1024 * 1024, resp);
        for line in reader.lines() {
            match line {
                Ok(line) => {
                    if line.is_empty() {
                        continue;
                    }
                    println!("{line}");
                }
                Err(e) => {
                    return Err(XurlError::Io(e.to_string()));
                }
            }
        }

        eprintln!("--- End of stream ---");
        Ok(())
    }

    /// Gets the authorization header for a request (public accessor for command layer).
    ///
    /// # Errors
    ///
    /// Returns an error if no valid auth method is found.
    pub fn get_auth_header_public(
        &mut self,
        method: &str,
        url: &str,
        auth_type: &str,
        username: &str,
    ) -> Result<String> {
        self.get_auth_header(method, url, auth_type, username)
    }

    /// Gets the authorization header for a request.
    fn get_auth_header(
        &mut self,
        method: &str,
        url: &str,
        auth_type: &str,
        username: &str,
    ) -> Result<String> {
        if !auth_type.is_empty() {
            return match auth_type.to_lowercase().as_str() {
                "oauth1" => self.auth.get_oauth1_header(method, url, None),
                "oauth2" => self.auth.get_oauth2_header(username),
                "app" => self.auth.get_bearer_token_header(),
                _ => Err(XurlError::auth(format!("invalid auth type: {auth_type}"))),
            };
        }

        // Try OAuth2 first — propagate errors if a token exists
        if self.auth.token_store.get_first_oauth2_token().is_some() {
            return self.auth.get_oauth2_header(username);
        }

        // Try OAuth1 — propagate errors if a token exists
        if self.auth.token_store.get_oauth1_tokens().is_some() {
            return self.auth.get_oauth1_header(method, url, None);
        }

        // Try Bearer
        if self.auth.token_store.has_bearer_token() {
            return self.auth.get_bearer_token_header();
        }

        Err(XurlError::auth(
            "NoAuthMethod: no authentication method available",
        ))
    }
}