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
//! A random.org api bindings.
//!
//! The randomness comes from atmospheric noise, which for many purposes is better than the
//! pseudo-random number algorithms typically used in computer programs. To use the library you
//! must have an api key which you may get [from here](https://api.random.org/api-keys/beta).
//!
//! # Usage
//!
//! ```rust,no_run
//! extern crate randomorg;
//!
//! fn main() {
//!     use randomorg::Random;
//!     let r = Random::new("API KEY HERE");
//!     println!("Result: {:?}", r.generate_integers(-100, 100, 15, true));
//!     let random_data = r.request_integers().min(0).max(100).limit(5).collect::<Vec<i32>>();
//!     println!("Random integers: {:?}", random_data);
//! }
//! ```
//!
//! # Warning
//!
//! However the library provides a thread-safe object, the **random.org** service does not allow
//! to use their API from multithreaded-apps. The service processes incoming calls according the
//! `request_id` parameter, it must be different for concurrent calls, while this library always
//! provides `request_id` equal to `1`. So when you use the library from multiple threads the
//! library will send multiple requests simultaneously with the same `request_id` field and the
//! **random.org** service will refuse to process them.

#![deny(clippy::all)]
#![deny(clippy::pedantic)]
#![deny(missing_docs)]
#![deny(warnings)]

mod date_de;
mod error;
mod methods;
mod model;
mod params;
#[cfg(feature = "rng")]
pub mod rand;
mod request_builders;
mod requests;
mod results;
pub mod version;

use methods::Method;
pub use model::{AllowedCharacters, ApiKey, ApiKeyStatus, Request, RequestId, Response};
pub use request_builders::{
    RequestBlobs, RequestDecimalFractions, RequestGaussians, RequestIntegers, RequestStrings,
    RequestUUIDs,
};
use requests::{
    EmptyRequest, GenerateBlobsRequest, GenerateDecimalFractionsRequest, GenerateGaussiansRequest,
    GenerateIntegersRequest, GenerateStringsRequest, GenerateUUIDsRequest,
};
pub use results::{
    GenerateBlobsResult, GenerateDecimalFractionsResult, GenerateGaussiansResult,
    GenerateIntegersResult, GenerateStringsResult, GenerateUUIDsResult, GetUsageResult, RandomData,
    RandomResult,
};

pub use error::{Error, ErrorCode, ResponseError, Result};

const API_INVOKE: &str = "https://api.random.org/json-rpc/2/invoke";

/// Macro only for internal use with the `Random` object (relies on its fields).
/// Performs a request.
macro_rules! make_request {
    ($api:ident, $body:expr) => {{
        $api.client.post(API_INVOKE).json($body).send()
    }};
}

/// Macro only for internal use with the `Random` object (relies on it's fields)
/// Parses a service data from the request's response.
macro_rules! request {
    ($api:ident, $request:ident) => {
        Ok(make_request!($api, &$request)?.json()?)
    };
}

/// A random.org api client.
#[derive(Debug, Clone)]
pub struct Random {
    client: reqwest::blocking::Client,
    api_key: ApiKey,
}

impl Random {
    /// Creates new random.org client.
    ///
    /// # Usage
    ///
    /// ```rust
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    /// }
    /// ```
    pub fn new<S: Into<String>>(api_key: S) -> Random {
        Random {
            client: reqwest::blocking::Client::new(),
            api_key: ApiKey(api_key.into()),
        }
    }

    /// Create a request object for generating random integers
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     let random_data = r.request_integers().min(0).max(100).limit(5).collect::<Vec<i32>>();
    ///     println!("Random integers: {:?}", random_data);
    /// }
    /// ```
    pub fn request_integers(&self) -> RequestIntegers {
        RequestIntegers::new(self)
    }

    /// Create a request object for generating random decimal fractions
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     let random_data = r.request_decimal_fractions().limit(5)
    ///                                                    .decimal_places(4)
    ///                                                    .collect::<Vec<f32>>();
    ///     println!("Random decimal fractions: {:?}", random_data);
    /// }
    /// ```
    pub fn request_decimal_fractions(&self) -> RequestDecimalFractions {
        RequestDecimalFractions::new(self)
    }

    /// Create a request object for generating random gaussians
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     let random_data = r.request_gaussians().limit(5)
    ///                                            .collect::<Vec<f32>>();
    ///     println!("Random gaussians: {:?}", random_data);
    /// }
    /// ```
    pub fn request_gaussians(&self) -> RequestGaussians {
        RequestGaussians::new(self)
    }

    /// Create a request object for generating random strings
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     let random_data = r.request_strings().limit(5)
    ///                                          .collect::<Vec<String>>();
    ///     println!("Random strings: {:?}", random_data);
    /// }
    /// ```
    pub fn request_strings(&self) -> RequestStrings {
        RequestStrings::new(self)
    }

    /// Create a request object for generating random UUIDs
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     let random_data = r.request_uuids().limit(5)
    ///                                        .collect::<Vec<String>>();
    ///     println!("Random strings: {:?}", random_data);
    /// }
    /// ```
    pub fn request_uuids(&self) -> RequestUUIDs {
        RequestUUIDs::new(self)
    }

    /// Create a request object for generating random blobs
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     let random_data = r.request_blobs().limit(5)
    ///                                        .collect::<Vec<String>>();
    ///     println!("Random strings: {:?}", random_data);
    /// }
    /// ```
    pub fn request_blobs(&self) -> RequestBlobs {
        RequestBlobs::new(self)
    }

    /// This method generates true random integers within a user-defined range.
    ///
    /// * [Official documentation](https://api.random.org/json-rpc/2/basic#generateIntegers)
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     println!("Result: {:?}", r.generate_integers(-100, 100, 15, true));
    /// }
    /// ```
    ///
    /// # Constraints
    /// * `min` must be within [-1e9; 1e9] range
    /// * `max` must be within [-1e9; 1e9] range
    /// * `limit` must be within [1; 1e4] range
    pub fn generate_integers(
        &self,
        min: i32,
        max: i32,
        limit: u16,
        replacement: bool,
    ) -> Result<Response<GenerateIntegersResult>> {
        let request =
            GenerateIntegersRequest::new(self.api_key.clone(), min, max, limit, replacement);
        request!(self, request)
    }

    /// This method generates true random decimal fractions from a uniform distribution across
    /// the [0,1] interval with a user-defined number of decimal places.
    ///
    /// * [Official documentation](https://api.random.org/json-rpc/2/basic#generateDecimalFractions)
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     println!("Result: {:?}", r.generate_decimal_fractions(10, 8));
    /// }
    /// ```
    ///
    /// # Constraints
    /// * `limit` must be within [1; 1e4] range
    /// * `decimal_places` must be within [1; 20] range
    pub fn generate_decimal_fractions(
        &self,
        limit: u16,
        decimal_places: u8,
    ) -> Result<Response<GenerateDecimalFractionsResult>> {
        let request =
            GenerateDecimalFractionsRequest::new(self.api_key.clone(), limit, decimal_places);
        request!(self, request)
    }

    /// This method generates true random numbers from a Gaussian distribution (also known as a
    /// normal distribution). The form uses a Box-Muller Transform to generate the Gaussian
    /// distribution from uniformly distributed numbers.
    ///
    /// * [Official documentation](https://api.random.org/json-rpc/2/basic#generateGaussians)
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     println!("Result: {:?}", r.generate_gaussians(2000, 1100, 100, 4));
    /// }
    /// ```
    ///
    /// # Constraints
    /// * `limit` must be within [1; 1e4] range
    /// * `mean` must be within [-1e6; 1e6] range
    /// * `standard_deviation` must be within [-1e6; 1e6] range
    /// * `significant_digits` must be within [2; 20] range
    pub fn generate_gaussians(
        &self,
        limit: u16,
        mean: i32,
        standard_deviation: i32,
        significant_digits: u8,
    ) -> Result<Response<GenerateGaussiansResult>> {
        let request = GenerateGaussiansRequest::new(
            self.api_key.clone(),
            limit,
            mean,
            standard_deviation,
            significant_digits,
        );
        request!(self, request)
    }

    /// This method generates true random strings.
    ///
    /// * [Official documentation](https://api.random.org/json-rpc/2/basic#generateStrings)
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::{ Random, AllowedCharacters };
    ///     use std::collections::BTreeSet;
    ///     let chars = "abcde".chars().collect::<BTreeSet<char>>();
    ///     let r = Random::new("API KEY HERE");
    ///     println!("Result: {:?}", r.generate_strings(5, 10, AllowedCharacters(chars)));
    /// }
    /// ```
    ///
    /// # Constraints
    /// * `limit` must be within [1; 1e4] range
    /// * `length` must be within [1; 20] range
    /// * `characters` must contain maximum 80 characters.
    pub fn generate_strings(
        &self,
        limit: u16,
        length: u8,
        characters: AllowedCharacters,
    ) -> Result<Response<GenerateStringsResult>> {
        let request = GenerateStringsRequest::new(self.api_key.clone(), limit, length, characters);
        request!(self, request)
    }

    /// This method generates version 4 true random Universally Unique IDentifiers (UUIDs) in
    /// accordance with section 4.4 of RFC 4122.
    ///
    /// * [Official documentation](https://api.random.org/json-rpc/2/basic#generateUUIDs)
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     println!("Result: {:?}", r.generate_uuids(5));
    /// }
    /// ```
    ///
    /// # Constraints
    /// * `limit` must be within [1; 1e3] range
    pub fn generate_uuids(&self, limit: u16) -> Result<Response<GenerateUUIDsResult>> {
        let request = GenerateUUIDsRequest::new(self.api_key.clone(), limit);
        request!(self, request)
    }

    /// This method generates Binary Large OBjects (BLOBs) containing true random data.
    ///
    /// * [Official documentation](https://api.random.org/json-rpc/2/basic#generateBlobs)
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     println!("Result: {:?}", r.generate_blobs(5, 16));
    /// }
    /// ```
    ///
    /// # Constraints
    /// * `limit` must be within [1; 100] range
    /// * `size` must be within [1, 1048576] range
    pub fn generate_blobs(&self, limit: u16, size: u32) -> Result<Response<GenerateBlobsResult>> {
        let request = GenerateBlobsRequest::new(self.api_key.clone(), limit, size);
        request!(self, request)
    }

    /// Returns information related to the usage of a given API key.
    ///
    /// * [Official documentation](https://api.random.org/json-rpc/2/basic#getUsage)
    ///
    /// # Usage
    ///
    /// ```rust,no_run
    /// extern crate randomorg;
    ///
    /// fn main() {
    ///     use randomorg::Random;
    ///     let r = Random::new("API KEY HERE");
    ///     println!("Result: {:?}", r.get_usage());
    /// }
    /// ```
    pub fn get_usage(&self) -> Result<Response<GetUsageResult>> {
        let request = EmptyRequest::new(Method::GetUsage, self.api_key.clone());
        request!(self, request)
    }
}

#[cfg(test)]
mod tests {
    fn assert_sync_and_send<T: Sync + Send>() {}

    #[test]
    fn test_sync_and_send() {
        assert_sync_and_send::<crate::Random>();
    }
}