penknife-gist 0.2.1

GitHub Gist API client with retry, rate-limit, and pagination handling
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
use std::collections::HashMap;
use std::time::Duration;

use reqwest::header::{ACCEPT, AUTHORIZATION, USER_AGENT};
use reqwest::{Client, Response, StatusCode};

use crate::error::{GistError, Result};
use crate::types::*;

const API_BASE: &str = "https://api.github.com";
const MAX_RETRIES: u32 = 3;
/// Files larger than this can't be fetched via `raw_url`; GitHub requires
/// cloning the gist's git repo.
const RAW_FETCH_LIMIT: u64 = 10 * 1024 * 1024;
/// Per-request timeout. Without one, reqwest waits forever and a hung
/// connection silently strands the operation.
const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);

/// One page of gists plus pagination metadata.
#[derive(Debug)]
pub struct GistPage {
    pub gists: Vec<Gist>,
    /// Whether the response's Link header indicates more pages exist.
    pub has_next: bool,
}

/// Cheap to clone: `reqwest::Client` is `Arc`-backed and clones share one
/// connection pool, so a single `GistClient` reused across operations keeps
/// the TCP/TLS connection to the API warm instead of paying a fresh handshake
/// per request.
#[derive(Clone)]
pub struct GistClient {
    http: Client,
    token: String,
    base_url: String,
}

/// Install ring as the process-wide rustls crypto provider (idempotent).
/// reqwest is built with `rustls-no-provider` to keep the heavy aws-lc-rs C
/// library out of the dependency tree, so a provider must be installed before
/// the first client is built - covers both the app and tests.
fn ensure_crypto_provider() {
    use std::sync::Once;
    static ONCE: Once = Once::new();
    ONCE.call_once(|| {
        let _ = rustls::crypto::ring::default_provider().install_default();
    });
}

impl GistClient {
    pub fn new(token: String) -> Self {
        Self::with_base_url(token, API_BASE.to_string())
    }

    /// Construct a client against a non-default API base URL (mock servers
    /// in tests, GitHub Enterprise).
    pub fn with_base_url(token: String, base_url: String) -> Self {
        ensure_crypto_provider();
        Self {
            http: Client::builder()
                .timeout(REQUEST_TIMEOUT)
                .build()
                .unwrap_or_default(),
            token,
            base_url,
        }
    }

    fn request(&self, method: reqwest::Method, url: &str) -> reqwest::RequestBuilder {
        self.http
            .request(method, url)
            .header(AUTHORIZATION, format!("Bearer {}", self.token))
            .header(USER_AGENT, "penknife")
            .header(ACCEPT, "application/vnd.github+json")
    }

    /// Send a GET request with retries on transient failures and rate-limit awareness.
    /// Only safe for idempotent requests - do not use for POST/PATCH/DELETE.
    async fn get_with_retry(&self, url: &str) -> Result<Response> {
        let mut attempt = 0u32;
        loop {
            attempt += 1;
            let resp_result = self.request(reqwest::Method::GET, url).send().await;
            match resp_result {
                Ok(resp) => {
                    let status = resp.status();
                    if status.is_success() {
                        return Ok(resp);
                    }
                    if let Some(wait) = rate_limit_wait(&resp)
                        && attempt < MAX_RETRIES
                    {
                        tokio::time::sleep(wait).await;
                        continue;
                    }
                    if status.is_server_error() && attempt < MAX_RETRIES {
                        tokio::time::sleep(backoff(attempt)).await;
                        continue;
                    }
                    let msg = resp.text().await.unwrap_or_default();
                    return Err(GistError::Api {
                        status: status.as_u16(),
                        message: msg,
                    });
                }
                Err(e) if attempt < MAX_RETRIES => {
                    // Network-level failure - retry with backoff.
                    tokio::time::sleep(backoff(attempt)).await;
                    let _ = e;
                }
                Err(e) => return Err(e.into()),
            }
        }
    }

    /// List a single page of gists (100 per page) along with pagination metadata.
    pub async fn list_page(&self, page: u32) -> Result<GistPage> {
        self.list_page_since(page, None).await
    }

    /// Like [`list_page`], but optionally constrained to gists updated after
    /// `since` via GitHub's `since=` filter. Used for incremental hydration:
    /// pass the last-walk timestamp to fetch only the delta instead of every
    /// gist. `since` is formatted as RFC3339 (`…Z`), which the API expects.
    pub async fn list_page_since(
        &self,
        page: u32,
        since: Option<chrono::DateTime<chrono::Utc>>,
    ) -> Result<GistPage> {
        let mut url = format!("{}/gists?per_page=100&page={page}", self.base_url);
        if let Some(ts) = since {
            url.push_str("&since=");
            url.push_str(&ts.format("%Y-%m-%dT%H:%M:%SZ").to_string());
        }
        let resp = self.get_with_retry(&url).await?;
        let has_next = resp
            .headers()
            .get("link")
            .and_then(|h| h.to_str().ok())
            .map(link_header_has_next)
            .unwrap_or(false);
        let gists: Vec<Gist> = resp.json().await?;
        Ok(GistPage { gists, has_next })
    }

    /// List all gists for the authenticated user, paginating automatically.
    pub async fn list_all(&self) -> Result<Vec<Gist>> {
        let mut all = Vec::new();
        let mut page = 1u32;

        loop {
            let result = self.list_page(page).await?;
            all.extend(result.gists);
            if !result.has_next {
                break;
            }
            page += 1;
        }

        Ok(all)
    }

    /// Get a single gist by ID (includes file content).
    pub async fn get(&self, id: &str) -> Result<Gist> {
        let url = format!("{}/gists/{id}", self.base_url);
        let resp = self.get_with_retry(&url).await?;
        Ok(resp.json().await?)
    }

    /// Full content of one file in a gist, following `raw_url` when the API
    /// response omitted or truncated the body (the Gists API truncates file
    /// content at 1MB; list responses omit it entirely). Returns `Ok(None)`
    /// if the gist has no file by that name. Files beyond the 10MB raw-fetch
    /// limit return `GistError::TooLarge` rather than silently-partial data.
    pub async fn file_content(&self, gist: &Gist, filename: &str) -> Result<Option<String>> {
        let Some(file) = gist.files.get(filename) else {
            return Ok(None);
        };
        if let Some(content) = &file.content
            && !file.truncated
        {
            return Ok(Some(content.clone()));
        }
        if file.size > RAW_FETCH_LIMIT {
            return Err(GistError::TooLarge {
                filename: filename.to_string(),
                size: file.size,
            });
        }
        let Some(raw_url) = file.raw_url.as_deref() else {
            return Err(GistError::Api {
                status: 0,
                message: format!("file {filename} has no content and no raw_url"),
            });
        };
        let resp = self.get_with_retry(raw_url).await?;
        Ok(Some(resp.text().await?))
    }

    /// Create a new private gist with a single file.
    pub async fn create(&self, filename: &str, content: &str, description: &str) -> Result<Gist> {
        let mut files = HashMap::new();
        files.insert(
            filename.to_string(),
            GistFileContent {
                content: content.to_string(),
            },
        );
        let body = CreateGistRequest {
            description: description.to_string(),
            public: false,
            files,
        };
        let url = format!("{}/gists", self.base_url);
        let resp = self
            .request(reqwest::Method::POST, &url)
            .json(&body)
            .send()
            .await?;
        check_response(resp).await
    }

    /// Update an existing gist's file content.
    pub async fn update(&self, id: &str, filename: &str, content: &str) -> Result<Gist> {
        let mut files = HashMap::new();
        files.insert(
            filename.to_string(),
            GistFileContent {
                content: content.to_string(),
            },
        );
        let body = UpdateGistRequest {
            description: None,
            files,
        };
        let url = format!("{}/gists/{id}", self.base_url);
        let resp = self
            .request(reqwest::Method::PATCH, &url)
            .json(&body)
            .send()
            .await?;
        check_response(resp).await
    }

    /// Rename a file within a gist. GitHub's PATCH /gists/:id endpoint
    /// accepts a `{"files": {"<old>": {"filename": "<new>"}}}` shape - note
    /// the old name is the map *key*, the new name is in the value.
    pub async fn rename_file(&self, id: &str, old_name: &str, new_name: &str) -> Result<Gist> {
        let body = serde_json::json!({
            "files": {
                old_name: { "filename": new_name }
            }
        });
        let url = format!("{}/gists/{id}", self.base_url);
        let resp = self
            .request(reqwest::Method::PATCH, &url)
            .json(&body)
            .send()
            .await?;
        check_response(resp).await
    }

    /// Delete a gist.
    pub async fn delete(&self, id: &str) -> Result<()> {
        let url = format!("{}/gists/{id}", self.base_url);
        let resp = self.request(reqwest::Method::DELETE, &url).send().await?;
        let status = resp.status();
        if status == StatusCode::NO_CONTENT {
            return Ok(());
        }
        if !status.is_success() {
            let msg = resp.text().await.unwrap_or_default();
            return Err(GistError::Api {
                status: status.as_u16(),
                message: msg,
            });
        }
        Ok(())
    }
}

async fn check_response(resp: reqwest::Response) -> Result<Gist> {
    let status = resp.status();
    if !status.is_success() {
        let msg = resp.text().await.unwrap_or_default();
        return Err(GistError::Api {
            status: status.as_u16(),
            message: msg,
        });
    }
    Ok(resp.json().await?)
}

/// Compute exponential backoff delay for the given attempt number (1-based).
fn backoff(attempt: u32) -> Duration {
    Duration::from_millis(500u64.saturating_mul(1u64 << attempt.min(6)))
}

/// Parse a GitHub `Link` header and return true if it contains a `rel="next"`.
fn link_header_has_next(link_header: &str) -> bool {
    link_header
        .split(',')
        .any(|part| part.contains("rel=\"next\""))
}

/// Inspect a response for rate-limit signals and return the duration to wait
/// before retrying, if any. Honors `Retry-After` (429) and
/// `X-RateLimit-Remaining: 0` + `X-RateLimit-Reset`.
fn rate_limit_wait(resp: &Response) -> Option<Duration> {
    let status = resp.status();
    let headers = resp.headers();

    // Explicit Retry-After header (in seconds), commonly on 429.
    if let Some(v) = headers
        .get("retry-after")
        .and_then(|h| h.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok())
    {
        return Some(Duration::from_secs(v.min(60)));
    }

    // Primary rate limit exhausted: 403/429 with X-RateLimit-Remaining: 0
    let remaining = headers
        .get("x-ratelimit-remaining")
        .and_then(|h| h.to_str().ok())
        .and_then(|s| s.parse::<u64>().ok());
    if (status == StatusCode::FORBIDDEN || status == StatusCode::TOO_MANY_REQUESTS)
        && remaining == Some(0)
        && let Some(reset) = headers
            .get("x-ratelimit-reset")
            .and_then(|h| h.to_str().ok())
            .and_then(|s| s.parse::<i64>().ok())
    {
        let now = chrono::Utc::now().timestamp();
        let wait = (reset - now).clamp(0, 60) as u64;
        return Some(Duration::from_secs(wait));
    }

    None
}

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

    #[test]
    fn link_header_detects_next() {
        let h = "<https://api.github.com/gists?page=2>; rel=\"next\", \
                 <https://api.github.com/gists?page=10>; rel=\"last\"";
        assert!(link_header_has_next(h));
    }

    #[test]
    fn link_header_no_next_on_last_page() {
        let h = "<https://api.github.com/gists?page=1>; rel=\"first\", \
                 <https://api.github.com/gists?page=9>; rel=\"prev\"";
        assert!(!link_header_has_next(h));
    }

    #[test]
    fn link_header_empty_means_no_next() {
        assert!(!link_header_has_next(""));
    }

    #[test]
    fn backoff_grows_with_attempt() {
        assert!(backoff(2) > backoff(1));
        assert!(backoff(3) > backoff(2));
    }

    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn gist_json(
        id: &str,
        filename: &str,
        content: Option<&str>,
        truncated: bool,
        size: u64,
        raw_url: Option<&str>,
    ) -> serde_json::Value {
        serde_json::json!({
            "id": id,
            "html_url": format!("https://gist.github.com/u/{id}"),
            "description": null,
            "public": false,
            "files": {
                filename: {
                    "filename": filename,
                    "size": size,
                    "raw_url": raw_url,
                    "content": content,
                    "truncated": truncated,
                }
            },
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": "2024-06-01T00:00:00Z",
        })
    }

    fn gist_from_json(v: serde_json::Value) -> Gist {
        serde_json::from_value(v).unwrap()
    }

    #[tokio::test]
    async fn file_content_returns_inline_content_without_fetching() {
        // Unreachable base URL - any HTTP request would error out.
        let client = GistClient::with_base_url("t".into(), "http://127.0.0.1:1".into());
        let gist = gist_from_json(gist_json("g1", "a.md", Some("hello"), false, 5, None));
        let got = client.file_content(&gist, "a.md").await.unwrap();
        assert_eq!(got.as_deref(), Some("hello"));
    }

    #[tokio::test]
    async fn file_content_returns_none_for_missing_file() {
        let client = GistClient::with_base_url("t".into(), "http://127.0.0.1:1".into());
        let gist = gist_from_json(gist_json("g1", "a.md", Some("hello"), false, 5, None));
        assert!(
            client
                .file_content(&gist, "other.md")
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn file_content_follows_raw_url_when_truncated() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/raw/a.md"))
            .respond_with(ResponseTemplate::new(200).set_body_string("full body"))
            .mount(&server)
            .await;
        let client = GistClient::with_base_url("t".into(), server.uri());
        let raw = format!("{}/raw/a.md", server.uri());
        let gist = gist_from_json(gist_json(
            "g1",
            "a.md",
            Some("trunc"),
            true,
            2_000_000,
            Some(&raw),
        ));
        let got = client.file_content(&gist, "a.md").await.unwrap();
        assert_eq!(got.as_deref(), Some("full body"));
    }

    #[tokio::test]
    async fn file_content_rejects_files_beyond_raw_fetch_limit() {
        let client = GistClient::with_base_url("t".into(), "http://127.0.0.1:1".into());
        let gist = gist_from_json(gist_json(
            "g1",
            "a.md",
            None,
            true,
            RAW_FETCH_LIMIT + 1,
            Some("http://127.0.0.1:1/raw"),
        ));
        let err = client.file_content(&gist, "a.md").await.unwrap_err();
        assert!(matches!(err, GistError::TooLarge { size, .. } if size == RAW_FETCH_LIMIT + 1));
    }

    #[tokio::test]
    async fn list_page_since_sends_since_query_param() {
        use wiremock::matchers::query_param;
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/gists"))
            .and(query_param("since", "2024-06-01T00:00:00Z"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_json(vec![gist_json("g1", "a.md", None, false, 1, None)]),
            )
            .mount(&server)
            .await;
        let client = GistClient::with_base_url("t".into(), server.uri());
        let since = chrono::DateTime::parse_from_rfc3339("2024-06-01T00:00:00Z")
            .unwrap()
            .with_timezone(&chrono::Utc);
        // The mock only matches when the since param is present and exact; a
        // missing/wrong param would 404 and surface as an error here.
        let page = client.list_page_since(1, Some(since)).await.unwrap();
        assert_eq!(page.gists.len(), 1);
    }

    #[tokio::test]
    async fn get_retries_server_errors_then_succeeds() {
        let server = MockServer::start().await;
        // First request 500s; the retry (mounted-mock now exhausted) gets 200.
        Mock::given(method("GET"))
            .and(path("/gists/g1"))
            .respond_with(ResponseTemplate::new(500))
            .up_to_n_times(1)
            .mount(&server)
            .await;
        Mock::given(method("GET"))
            .and(path("/gists/g1"))
            .respond_with(ResponseTemplate::new(200).set_body_json(gist_json(
                "g1",
                "a.md",
                Some("x"),
                false,
                1,
                None,
            )))
            .mount(&server)
            .await;
        let client = GistClient::with_base_url("t".into(), server.uri());
        let gist = client.get("g1").await.unwrap();
        assert_eq!(gist.id, "g1");
    }
}