-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.rs
649 lines (576 loc) · 15.7 KB
/
client.rs
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//! Module to create a `Client` instance which is the main container for the API
//! This is the main client struct and the different configuration methods.
//!
//! The way to configure it is different from the Go way and more in line
//! with current Rust practices.
//!
//! The only mandatory argument is the API key so it is given to `new()` and
//! all the other methods are there for configuration everything you want to
//! change from the default.
//!
//! `NOTE` none of the fields are public except within the crate
//!
//! There are several classes of calls in the API:
//!
//! - entities
//! - protocols
//! - utility functions
//!
//! Each class is selected by a method on the `Client` struct such as `probe()` or `measurement()`.
//! Calling one of these methods sets up the context for further calls with `RequestBuilder`
//! (or plain `Request`).
//!
//! Errors are handled in two steps:
//! 1. if there is a Transport error (Unknown Host, Unreachable, etc.) call() will return an error
//! 2. if the API returns an error, we attempt to decode as an APIError. If not, everything is good.
//!
//! So every `call()` returns a `Result<something, APIError>`.
//!
//! We use [reqwest] as HTTP client. It has support for everything we need incl. proxy. We choose
//! to use the blocking client as most of the time this ought to be enough and it is easier.
//!
//! [reqwest]: https://crates.io/reqwest/
//!
// Standard library
use std::time::Duration;
// External crates
use anyhow::{anyhow, Result};
use clap::{crate_name, crate_version};
use reqwest::Url;
// Internal crates
use crate::option::Options;
use crate::request::RequestBuilder;
// ---------------------------------------------------------------------------
/// We target the v2 API (not sure if it needs to be public)
const ENDPOINT: &str = "https://atlas.ripe.net/api/v2";
// ---------------------------------------------------------------------------
/// Represents all possible INET Address Family values
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AF {
/// Only IPv4 target
V4,
/// Only IPv6 target
V6,
/// Both IPv4 & v6
V46,
}
/// Represents the different categories aka first level of requests (probes, credits, etc.
#[derive(Clone, Copy, Debug)]
pub enum Ctx {
None = 0,
Anchors,
AnchorMeasurements,
Credits,
Keys,
Measurements,
ParticipationRequests,
Probes,
}
impl Default for Ctx {
fn default() -> Self {
Ctx::None
}
}
// ---------------------------------------------------------------------------
/// This is the main `Client` struct. It holds all the parameters and the HTTP client handle.
/// When using `Client::new()`, you get all the defaults values, if you want to configure it,
/// please use `ClientBuilder` instead.
///
/// Examples:
/// ```no_run
/// # fn main() -> Result<(), atlas_rs::errors::APIError> {
/// use atlas_rs::client::Client;
/// use atlas_rs::param::Param;
/// use atlas_rs::core::probes::Probe;
///
/// let c = Client::new();
///
/// let p: Probe = c.probe().get(666)?;
/// # Ok(())
/// # }
/// ```
/// or
/// ```no_run
/// # fn main() -> Result<(), atlas_rs::errors::APIError> {
/// use atlas_rs::client::Client;
/// use atlas_rs::param::Param;
/// use atlas_rs::core::credits::Credits;
///
/// let c = Client::new();
///
/// let r: Credits = c.credits().info()?;
/// # Ok(())
/// # }
/// ```
///
#[derive(Clone, Debug)]
pub struct Client {
/// Mandatory
pub(crate) api_key: Option<String>,
/// Optional
pub(crate) endpoint: Url,
pub(crate) default_probe: u32,
pub(crate) area_type: String,
pub(crate) area_value: String,
pub(crate) is_oneoff: bool,
pub(crate) pool_size: usize,
pub(crate) want_af: AF,
pub(crate) verbose: bool,
pub(crate) tags: String,
/// Default options
pub(crate) opts: Options,
/// Internal state, http client
pub(crate) agent: Option<reqwest::blocking::Client>,
}
/// Default values for Client
///
impl Default for Client {
/// Defines all the default values
fn default() -> Self {
Client::new()
}
}
impl Client {
// ---------------------------------------------------------------------
// Public API
/// Creates a bare client with defaults except for the API key which limits to certain
/// RIPE Atlas calls.
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::Client;
///
/// let c = Client::new();
/// ```
///
pub fn new() -> Self {
let endp = reqwest::Url::parse(ENDPOINT).unwrap();
Client {
api_key: None,
endpoint: endp,
default_probe: 0,
area_type: "area".to_string(),
area_value: "WW".to_string(),
is_oneoff: true,
pool_size: 10,
want_af: AF::V46,
verbose: false,
tags: "".to_string(),
opts: Options::new(),
agent: None,
}
.httpclient()
}
/// Create a ClientBuilder struct and returns it for chained calls
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::Client;
///
/// let c = Client::builder();
/// ```
///
pub fn builder() -> ClientBuilder {
ClientBuilder::new()
}
// ---------------------------------------------------------------------
// Entities
//
#[inline]
pub fn anchors(&self) -> RequestBuilder {
self.route_to(Ctx::Anchors)
}
#[inline]
pub fn anchor_measurement(&self) -> RequestBuilder {
self.route_to(Ctx::AnchorMeasurements)
}
#[inline]
pub fn credits(&self) -> RequestBuilder {
self.route_to(Ctx::Credits)
}
#[inline]
pub fn keys(&self) -> RequestBuilder {
self.route_to(Ctx::Keys)
}
#[inline]
pub fn measurement(&self) -> RequestBuilder {
unimplemented!()
}
#[inline]
pub fn probe(&self) -> RequestBuilder {
self.route_to(Ctx::Probes)
}
// ---------------------------------------------------------------------
// Protocols
//
pub fn dns(&self) -> RequestBuilder {
unimplemented!()
}
pub fn http(&self) -> RequestBuilder {
unimplemented!()
}
pub fn ntp(&self) -> RequestBuilder {
unimplemented!()
}
pub fn ping(&self) -> RequestBuilder {
unimplemented!()
}
pub fn tlscert(&self) -> RequestBuilder {
unimplemented!()
}
pub fn traceroute(&self) -> RequestBuilder {
unimplemented!()
}
// ---------------------------------------------------------------------
// Helpers/shortcuts
// *placeholder*
// ---------------------------------------------------------------------
// Private functions
/// Create an instance of the HTTP client and attach it there. It is called as part of
/// `.build()`.
///
fn httpclient(mut self) -> Self {
let ag = format!("{}/{}", crate_name!(), crate_version!());
let agent = reqwest::blocking::ClientBuilder::new()
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(5))
.user_agent(&ag)
.build()
.unwrap();
self.agent = Some(agent);
self
}
/// Private routing function for first level (`probe()`, `keys()`, etc.)
///
fn route_to(&self, op: Ctx) -> RequestBuilder {
let url = self.endpoint.to_owned();
// Default HTTP operation is GET, some will be POST/DELETE but that is handled in the
// next call in the chain.
let r = reqwest::blocking::Request::new(reqwest::Method::GET, url);
// Enforce API key usage
if self.api_key.is_none() {
panic!("No API key defined");
}
let mut c = self.clone();
c.opts.merge(&self.opts);
// Ensure api-Key is filled in prior to the calls.
c.opts["key"] = self.api_key.as_ref().unwrap().clone();
RequestBuilder {
ctx: op,
paged: false,
c,
r,
}
}
}
// ---------------------------------------------------------------------------
/// `ClientBuilder` is the main struct to create and configure a `Client`. You have to close
/// the chain by calling `build()`.
///
/// Examples:
/// ```no_run
/// # fn main() -> Result<(), atlas_rs::errors::APIError> {
/// use atlas_rs::param::Param;
/// use atlas_rs::core::probes::Probe;
/// use atlas_rs::client::{AF, ClientBuilder};
///
/// let c = ClientBuilder::new()
/// .api_key("FOO")
/// .onoff(true)
/// .default_probe(666)
/// .want_af(AF::V4)
/// .build()?;
///
/// let p: Probe = c.probe().get(666)?;
/// # Ok(())
/// # }
/// ```
///
pub struct ClientBuilder {
cl: Client,
}
/// Default values for `ClientBuilder`
///
impl Default for ClientBuilder {
/// Defines all the default values
fn default() -> Self {
ClientBuilder::new()
}
}
/// Methods for `ClientBuilder`
///
impl ClientBuilder {
// ---------------------------------------------------------------------
// Public API
/// Create a new `ClientBuilder` instance
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
/// let c = ClientBuilder::new();
/// ```
///
pub fn new() -> Self {
ClientBuilder { cl: Client::new() }
}
/// Create the final Client after checking the API key has been changed
///
pub fn build(self) -> Result<Client> {
match &self.cl.api_key {
Some(_k) => Ok(self.cl.clone()),
None => Err(anyhow!("You must change the default key")),
}
}
/// Set the API key
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .api_key("FOO");
/// ```
///
pub fn api_key(mut self, key: &str) -> Self {
self.cl.api_key = Some(key.to_owned());
self
}
/// Sets the API endpoint
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .endpoint("https://example.com/v1")
/// # ;
/// ```
///
pub fn endpoint(mut self, v: &str) -> Self {
let endp = Url::parse(v).unwrap();
self.cl.endpoint = endp;
self
}
/// Sets the default probe ID
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .default_probe(666)
/// # ;
/// ```
///
pub fn default_probe(mut self, v: u32) -> Self {
self.cl.default_probe = v;
self
}
/// Limits the scope by specifying an area type
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .area_type("area")
/// # ;
/// ```
///
pub fn area_type(mut self, v: &str) -> Self {
self.cl.area_type = v.to_owned();
self
}
/// Limits the scope to this particular area value
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .area_value("WW")
/// # ;
/// ```
///
pub fn area_value(mut self, v: &str) -> Self {
self.cl.area_value = v.to_owned();
self
}
/// Sets the one-shot flag
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .onoff(true)
/// # ;
/// ```
///
pub fn onoff(mut self, v: bool) -> Self {
self.cl.is_oneoff = v;
self
}
/// Sets the pool size
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .pool_size(20)
/// # ;
/// ```
///
pub fn pool_size(mut self, v: usize) -> Self {
self.cl.pool_size = v;
self
}
/// Sets the verbose flag
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .verbose(true)
/// # ;
/// ```
///
pub fn verbose(mut self, v: bool) -> Self {
self.cl.verbose = v;
self
}
/// Sets the inet family, either v4 or v6 or both.
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::{AF, ClientBuilder};
///
/// let c = ClientBuilder::new()
/// .want_af(AF::V6)
/// # ;
/// ```
///
pub fn want_af(mut self, v: AF) -> Self {
self.cl.want_af = v;
self
}
/// Sets the tags to be sent with the requests
/// +tag / tag ==> tags_include
/// -tag / !tag ==> tags_exclude
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .tags("ftth !cable")
/// # ;
/// ```
///
pub fn tags<S: Into<String>>(mut self, v: S) -> Self {
self.cl.tags = v.into();
self
}
/// Add options
///
/// Example:
///
/// ```no_run
/// # use atlas_rs::option::Options;
/// # use atlas_rs::client::ClientBuilder;
///
/// let c = ClientBuilder::new()
/// .with(&Options::from([("is_anchor", "true")]))
/// # ;
/// ```
///
pub fn with(&self, opts: &Options) -> Self {
let mut cl = self.cl.clone();
cl.opts.merge(opts);
ClientBuilder { cl }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_client_new() {
let c = Client::new();
// Check all defaults
assert!(c.api_key.is_none());
assert_eq!(ENDPOINT.to_string(), c.endpoint.as_str());
assert_eq!(0, c.default_probe);
assert_eq!("area".to_string(), c.area_type);
assert_eq!("WW".to_string(), c.area_value);
assert!(c.is_oneoff);
assert_eq!(10, c.pool_size);
assert_eq!(AF::V46, c.want_af);
assert!(!c.verbose);
assert_eq!("".to_string(), c.tags);
assert!(c.agent.is_some());
}
#[test]
fn test_clientbuilder_new() {
let cb = ClientBuilder::new().api_key("key").build();
assert!(cb.is_ok());
let cb = cb.unwrap();
// Check all defaults
assert_eq!("key".to_string(), cb.api_key.unwrap());
assert_eq!(ENDPOINT, cb.endpoint.as_str());
assert_eq!(0, cb.default_probe);
assert_eq!("area".to_string(), cb.area_type);
assert_eq!("WW".to_string(), cb.area_value);
assert!(cb.is_oneoff);
assert_eq!(10, cb.pool_size);
assert_eq!(AF::V46, cb.want_af);
assert!(!cb.verbose);
assert_eq!("".to_string(), cb.tags);
assert!(!cb.opts.contains_key("key"));
assert!(cb.agent.is_some());
}
#[test]
fn test_with() {
let h = Options::from([("foo", "a"), ("bar", "b"), ("key", "FOO")]);
let c = ClientBuilder::new()
.api_key("key")
.with(&h)
.build()
.unwrap();
assert_eq!(h, c.opts);
}
#[test]
fn test_clientbuilder_error() {
let c = ClientBuilder::new().build();
assert!(c.is_err());
}
#[test]
fn test_clientbuilder_api_key() {
let c = ClientBuilder::new().api_key("FOO").build();
assert!(c.is_ok());
assert!(c.as_ref().unwrap().api_key.is_some());
let c = c.unwrap();
let key = c.api_key;
assert!(key.is_some());
assert_eq!("FOO", key.unwrap());
}
#[test]
fn test_onoff() {
let c = ClientBuilder::new().api_key("key").onoff(true).build();
assert!(c.unwrap().is_oneoff);
}
}