infrahub 0.2.1

small graphql client for infrahub
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
//! client configuration
//!
//! build a [`ClientConfig`] with base url, token, and optional overrides.
//! pass it to [`crate::Client::new`] to create a client.

use crate::error::{Error, Result};
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use std::sync::Arc;
use std::time::Duration;
use url::Url;

/// configuration for the infrahub client
#[derive(Clone)]
pub struct ClientConfig {
    /// original base url input
    pub(crate) raw_base_url: String,

    /// base url of the infrahub instance (e.g., "<https://infrahub.example.com>")
    pub(crate) base_url: Url,

    /// whether the provided base url parsed successfully
    pub(crate) base_url_valid: bool,

    /// api authentication token
    pub(crate) token: String,

    /// default branch for graphql queries
    pub(crate) default_branch: Option<String>,

    /// request timeout duration
    pub(crate) timeout: Duration,

    /// user agent string
    pub(crate) user_agent: String,

    /// whether to verify ssl certificates
    pub(crate) verify_ssl: bool,

    /// additional headers to send with every request
    pub(crate) extra_headers: HeaderMap,

    /// prebuilt http client (takes precedence over http_client_builder)
    pub(crate) http_client: Option<reqwest::Client>,

    /// callback to customize the http client builder before building
    pub(crate) http_client_builder:
        Option<Arc<dyn Fn(reqwest::ClientBuilder) -> reqwest::ClientBuilder + Send + Sync>>,
}

impl ClientConfig {
    /// create a new client configuration
    ///
    /// # arguments
    ///
    /// * `base_url` - the base url of the infrahub instance (with or without trailing slash)
    /// * `token` - the api authentication token
    ///
    /// # example
    ///
    /// ```
    /// use infrahub::ClientConfig;
    ///
    /// let config = ClientConfig::new("https://infrahub.example.com", "your-token-here");
    /// ```
    pub fn new(base_url: impl AsRef<str>, token: impl Into<String>) -> Self {
        let base_url_str = base_url.as_ref();

        let normalized = base_url_str.trim_end_matches('/');

        let (base_url, base_url_valid) = match Url::parse(normalized)
            .or_else(|_| Url::parse(&format!("https://{}", normalized)))
        {
            Ok(url) => (url, true),
            Err(_) => (Url::parse("https://invalid.invalid").unwrap(), false),
        };

        Self {
            raw_base_url: base_url_str.to_string(),
            base_url,
            base_url_valid,
            token: token.into(),
            default_branch: None,
            timeout: Duration::from_secs(30),
            user_agent: format!("infrahub-rs/{} (Rust)", env!("CARGO_PKG_VERSION")),
            verify_ssl: true,
            extra_headers: HeaderMap::new(),
            http_client: None,
            http_client_builder: None,
        }
    }

    /// set the default branch for graphql queries
    pub fn with_default_branch(mut self, branch: impl Into<String>) -> Self {
        self.default_branch = Some(branch.into());
        self
    }

    /// set the request timeout
    ///
    /// default: 30 seconds
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// set a custom user agent string
    pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.user_agent = user_agent.into();
        self
    }

    /// disable ssl certificate verification (not recommended for production)
    ///
    /// default: enabled
    pub fn with_ssl_verification(mut self, verify: bool) -> Self {
        self.verify_ssl = verify;
        self
    }

    /// add a header to every request
    pub fn with_header(mut self, name: HeaderName, value: HeaderValue) -> Self {
        self.extra_headers.insert(name, value);
        self
    }

    /// add a set of headers to every request
    pub fn with_headers(mut self, headers: HeaderMap) -> Self {
        self.extra_headers.extend(headers);
        self
    }

    /// access extra headers configured on this client
    pub fn extra_headers(&self) -> &HeaderMap {
        &self.extra_headers
    }

    /// inject a prebuilt http client.
    ///
    /// when set, this client is used as-is and takes precedence over
    /// `with_http_client_builder`. all transport configuration — auth headers,
    /// tls, timeouts, ssl verification, user agent — comes from the prebuilt
    /// client; the corresponding `ClientConfig` fields are ignored.
    ///
    /// because auth is managed by the caller, an empty token is accepted when
    /// this option is set.
    pub fn with_http_client(mut self, http_client: reqwest::Client) -> Self {
        self.http_client = Some(http_client);
        self
    }

    /// customize the http client builder before the client is created.
    ///
    /// the callback receives a builder that already has the auth header,
    /// extra headers, user agent, timeout, and ssl settings applied.
    /// use this to add proxy config, custom tls roots, or other transport
    /// settings without reimplementing the defaults.
    ///
    /// ignored if `with_http_client` is also set.
    pub fn with_http_client_builder<F>(mut self, f: F) -> Self
    where
        F: Fn(reqwest::ClientBuilder) -> reqwest::ClientBuilder + Send + Sync + 'static,
    {
        self.http_client_builder = Some(Arc::new(f));
        self
    }

    /// validate the configuration
    pub(crate) fn validate(&self) -> Result<()> {
        if !self.base_url_valid {
            return Err(Error::Config(format!(
                "invalid base url: {}",
                self.raw_base_url
            )));
        }

        if self.base_url.scheme() != "http" && self.base_url.scheme() != "https" {
            return Err(Error::Config(format!(
                "invalid url scheme: {}. must be http or https",
                self.base_url.scheme()
            )));
        }

        // token is only required when the client is not managing its own transport
        if self.http_client.is_none() && self.token.is_empty() {
            return Err(Error::Config("api token cannot be empty".to_string()));
        }

        Ok(())
    }

    /// resolve the effective branch: use the explicit argument if non-empty,
    /// fall back to `default_branch`, or return `None`.
    fn resolve_branch(&self, branch: Option<&str>) -> Option<String> {
        branch
            .map(|b| b.to_string())
            .or_else(|| self.default_branch.clone())
            .filter(|b| !b.is_empty())
    }

    /// parse a url by appending a path to the base url
    fn base_url_with_path(&self, path: &str) -> Result<Url> {
        let base = self.base_url.as_str().trim_end_matches('/');
        Ok(Url::parse(&format!("{}{}", base, path))?)
    }

    /// build the graphql url for a branch (or default branch if none provided)
    pub(crate) fn graphql_url(&self, branch: Option<&str>) -> Result<Url> {
        let mut url = self.base_url_with_path("/graphql")?;
        if let Some(branch) = self.resolve_branch(branch) {
            url.path_segments_mut()
                .expect("HTTP URL supports path segments")
                .push(&branch);
        }
        Ok(url)
    }

    /// build a file download url by node id
    pub(crate) fn file_url(&self, node_id: &str, branch: Option<&str>) -> Result<Url> {
        let mut url = self.base_url_with_path("/api/files")?;
        url.path_segments_mut()
            .expect("HTTP URL supports path segments")
            .push(node_id);
        if let Some(branch) = self.resolve_branch(branch) {
            url.query_pairs_mut().append_pair("branch", &branch);
        }
        Ok(url)
    }

    /// build a file download url by human-friendly id
    pub(crate) fn file_by_hfid_url(
        &self,
        kind: &str,
        hfid: &[&str],
        branch: Option<&str>,
    ) -> Result<Url> {
        let mut url = self.base_url_with_path("/api/files/by-hfid")?;
        {
            let mut segments = url
                .path_segments_mut()
                .expect("HTTP URL supports path segments");
            segments.push(kind);
            for segment in hfid {
                segments.push(segment);
            }
        }
        if let Some(branch) = self.resolve_branch(branch) {
            url.query_pairs_mut().append_pair("branch", &branch);
        }
        Ok(url)
    }

    /// build a file download url by storage id
    pub(crate) fn file_by_storage_id_url(
        &self,
        storage_id: &str,
        branch: Option<&str>,
    ) -> Result<Url> {
        let mut url = self.base_url_with_path("/api/files/by-storage-id")?;
        url.path_segments_mut()
            .expect("HTTP URL supports path segments")
            .push(storage_id);
        if let Some(branch) = self.resolve_branch(branch) {
            url.query_pairs_mut().append_pair("branch", &branch);
        }
        Ok(url)
    }

    /// build the schema url for a branch (or default branch if none provided)
    pub(crate) fn schema_url(&self, branch: Option<&str>) -> Result<Url> {
        let mut url = self.base_url_with_path("/schema.graphql")?;
        if let Some(branch) = self.resolve_branch(branch) {
            url.query_pairs_mut().append_pair("branch", &branch);
        }
        Ok(url)
    }
}

impl std::fmt::Debug for ClientConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ClientConfig")
            .field("base_url", &self.base_url)
            .field("timeout", &self.timeout)
            .field("user_agent", &self.user_agent)
            .field("verify_ssl", &self.verify_ssl)
            .field("extra_headers", &self.extra_headers.len())
            .field("default_branch", &self.default_branch)
            .field("http_client", &self.http_client.is_some())
            .field("http_client_builder", &self.http_client_builder.is_some())
            .field("token", &"<redacted>")
            .finish()
    }
}

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

    #[test]
    fn test_new_config() {
        let config = ClientConfig::new("https://infrahub.example.com", "test-token");
        assert_eq!(
            config.base_url.as_str().trim_end_matches('/'),
            "https://infrahub.example.com"
        );
        assert_eq!(config.token, "test-token");
        assert_eq!(config.timeout, Duration::from_secs(30));
    }

    #[test]
    fn test_graphql_url_default() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");
        let url = config.graphql_url(None).unwrap();
        assert_eq!(url.as_str(), "https://infrahub.example.com/graphql");
    }

    #[test]
    fn test_graphql_url_branch() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");
        let url = config.graphql_url(Some("test")).unwrap();
        assert_eq!(url.as_str(), "https://infrahub.example.com/graphql/test");
    }

    #[test]
    fn test_schema_url_branch() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");
        let url = config.schema_url(Some("test")).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/schema.graphql?branch=test"
        );
    }

    #[test]
    fn test_schema_url_default() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");
        let url = config.schema_url(None).unwrap();
        assert_eq!(url.as_str(), "https://infrahub.example.com/schema.graphql");
    }

    #[test]
    fn test_validation() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");
        assert!(config.validate().is_ok());

        let empty_token = ClientConfig::new("https://infrahub.example.com", "");
        assert!(empty_token.validate().is_err());

        // empty token is allowed when a prebuilt client handles auth
        let empty_token_prebuilt = ClientConfig::new("https://infrahub.example.com", "")
            .with_http_client(reqwest::Client::new());
        assert!(empty_token_prebuilt.validate().is_ok());
    }

    #[test]
    fn test_validation_invalid_url() {
        let mut config = ClientConfig::new("https://infrahub.example.com", "token");
        config.base_url_valid = false;
        let err = config.validate().unwrap_err();
        assert!(matches!(err, Error::Config(_)));
    }

    #[test]
    fn test_validation_invalid_scheme() {
        let config = ClientConfig::new("ftp://example.com", "token");
        let err = config.validate().unwrap_err();
        assert!(matches!(err, Error::Config(_)));
    }

    #[test]
    fn test_builder_helpers() {
        let mut headers = HeaderMap::new();
        headers.insert(
            HeaderName::from_static("x-test"),
            HeaderValue::from_static("value"),
        );

        let config = ClientConfig::new("https://infrahub.example.com", "token")
            .with_default_branch("main")
            .with_timeout(Duration::from_secs(5))
            .with_user_agent("infrahub-test")
            .with_ssl_verification(false)
            .with_headers(headers.clone())
            .with_header(
                HeaderName::from_static("x-other"),
                HeaderValue::from_static("other"),
            );

        assert_eq!(config.default_branch.as_deref(), Some("main"));
        assert_eq!(config.timeout, Duration::from_secs(5));
        assert_eq!(config.user_agent, "infrahub-test");
        assert!(!config.verify_ssl);
        assert_eq!(config.extra_headers.get("x-test").unwrap(), "value");
        assert_eq!(config.extra_headers.get("x-other").unwrap(), "other");
        assert_eq!(config.extra_headers(), &config.extra_headers);
    }

    #[test]
    fn test_with_http_client() {
        let prebuilt = reqwest::Client::new();
        let config =
            ClientConfig::new("https://infrahub.example.com", "token").with_http_client(prebuilt);
        assert!(config.http_client.is_some());
        assert!(config.http_client_builder.is_none());
    }

    #[test]
    fn test_with_http_client_builder() {
        let config = ClientConfig::new("https://infrahub.example.com", "token")
            .with_http_client_builder(|b| b.connection_verbose(true));
        assert!(config.http_client.is_none());
        assert!(config.http_client_builder.is_some());
    }

    #[test]
    fn test_file_url() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");
        let url = config.file_url("abc-123", None).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/abc-123"
        );

        let url = config.file_url("abc-123", Some("dev")).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/abc-123?branch=dev"
        );
    }

    #[test]
    fn test_file_by_hfid_url() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");
        let url = config
            .file_by_hfid_url("MyFile", &["value1", "value2"], None)
            .unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/by-hfid/MyFile/value1/value2"
        );
    }

    #[test]
    fn test_file_by_storage_id_url() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");
        let url = config.file_by_storage_id_url("store-456", None).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/by-storage-id/store-456"
        );
    }

    #[test]
    fn test_branch_with_special_chars_is_encoded() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");

        // ampersand in branch name would inject a second query param without encoding
        let url = config.file_url("abc-123", Some("feat&evil=1")).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/abc-123?branch=feat%26evil%3D1"
        );

        let url = config
            .file_by_hfid_url("MyFile", &["v1"], Some("has spaces"))
            .unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/by-hfid/MyFile/v1?branch=has+spaces"
        );

        let url = config
            .file_by_storage_id_url("store-1", Some("a=b&c=d"))
            .unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/by-storage-id/store-1?branch=a%3Db%26c%3Dd"
        );

        let url = config.schema_url(Some("release/1.0&drop")).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/schema.graphql?branch=release%2F1.0%26drop"
        );
    }

    #[test]
    fn test_path_segments_with_special_chars_are_encoded() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");

        // branch with slash in graphql_url path
        let url = config.graphql_url(Some("release/1.0")).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/graphql/release%2F1.0"
        );

        // branch with hash would truncate the URL without encoding
        let url = config.graphql_url(Some("fix#123")).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/graphql/fix%23123"
        );

        // node_id with special characters
        let url = config.file_url("id/with#special", None).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/id%2Fwith%23special"
        );

        // kind and hfid with special characters
        let url = config
            .file_by_hfid_url("My/Kind", &["val/1", "val#2"], None)
            .unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/by-hfid/My%2FKind/val%2F1/val%232"
        );

        // storage_id with special characters
        let url = config.file_by_storage_id_url("store/id#1", None).unwrap();
        assert_eq!(
            url.as_str(),
            "https://infrahub.example.com/api/files/by-storage-id/store%2Fid%231"
        );
    }

    #[test]
    fn test_debug_reflects_http_client_fields() {
        let config = ClientConfig::new("https://infrahub.example.com", "token");
        let debug = format!("{config:?}");
        assert!(debug.contains("http_client: false"));
        assert!(debug.contains("http_client_builder: false"));
        assert!(debug.contains("\"<redacted>\""));

        let config = config.with_http_client(reqwest::Client::new());
        let debug = format!("{config:?}");
        assert!(debug.contains("http_client: true"));
    }
}