unkey 0.6.0

An asynchronous Rust SDK for the Unkey API.
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
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
use std::time::SystemTime;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::ErrorCode;
use super::Ratelimit;
use super::RatelimitState;
use super::Refill;
use super::UndefinedOr;

/// An update operation that can be performed.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum UpdateOp {
    /// Increment operation.
    Increment,

    /// Decrement operation.
    Decrement,

    /// Set operation.
    Set,
}

/// An outgoing verify key request.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VerifyKeyRequest {
    /// The api key to verify.
    pub key: String,

    /// The id of the api this key belongs to.
    pub api_id: String,
}

impl VerifyKeyRequest {
    /// Creates a new verify key request.
    ///
    /// # Arguments
    /// - `key`: The api key to verify.
    /// - `api_id`: The id of the api this key belongs to.
    ///
    /// # Returns
    /// The verify key request.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::VerifyKeyRequest;
    /// let r = VerifyKeyRequest::new("test", "api_123");
    ///
    /// assert_eq!(r.key, String::from("test"));
    /// assert_eq!(r.api_id, String::from("api_123"));
    /// ```
    #[must_use]
    pub fn new<T: Into<String>>(key: T, api_id: T) -> Self {
        Self {
            key: key.into(),
            api_id: api_id.into(),
        }
    }
}

/// An incoming verify key response.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VerifyKeyResponse {
    /// Whether or not the key is valid for any reason.
    ///
    /// e.g. ratelimited, no more remaining, expired, key not found.
    pub valid: bool,

    /// The keys unique id, if any.
    pub key_id: Option<String>,

    /// The name for this key, if any.
    pub name: Option<String>,

    /// The owner id for this key, if any.
    pub owner_id: Option<String>,

    /// The dynamic mapping of values associated with this key, if any.
    pub meta: Option<Value>,

    /// The number of verifications before this key becomes invalidated, if
    /// any limit was set on the key.
    pub remaining: Option<usize>,

    /// The code for the verification.
    pub code: ErrorCode,

    /// Whether or not the key is enabled.
    pub enabled: Option<bool>,

    /// The unix epoch in ms when this key expires, if it does.
    pub expires: Option<usize>,

    /// The state of the ratelimit set on this key, if any.
    pub ratelimit: Option<RatelimitState>,

    /// The refill state of this key, if any.
    pub refill: Option<Refill>,
}

/// An outgoing create key request.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateKeyRequest {
    /// The api id to create this key for.
    pub api_id: String,

    /// The optional owner id for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub owner_id: UndefinedOr<String>,

    /// The optional byte length for the key, defaults to 16.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub byte_length: UndefinedOr<usize>,

    /// The optional prefix for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub prefix: UndefinedOr<String>,

    /// The optional name for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub name: UndefinedOr<String>,

    /// The optional dynamic meta mapping for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub meta: UndefinedOr<Value>,

    /// The optional unix epoch in ms when the key should expire.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub expires: UndefinedOr<usize>,

    /// The optional number of uses remaining to set for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub remaining: UndefinedOr<usize>,

    /// The optional ratelimit to set for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub ratelimit: UndefinedOr<Ratelimit>,

    /// The keys refill state, if any.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub refill: UndefinedOr<Refill>,
}

impl CreateKeyRequest {
    /// Creates a new request for key creation.
    ///
    /// # Arguments
    /// - `api_id`: The api id to create this key for.
    ///
    /// # Returns
    /// The new create key request.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// # use unkey::models::UndefinedOr;
    /// let r = CreateKeyRequest::new("test");
    ///
    /// assert_eq!(r.api_id, String::from("test"));
    /// assert_eq!(r.owner_id, UndefinedOr::Undefined);
    /// assert_eq!(r.byte_length, UndefinedOr::Undefined);
    /// assert_eq!(r.prefix, UndefinedOr::Undefined);
    /// assert_eq!(r.name, UndefinedOr::Undefined);
    /// assert_eq!(r.meta, UndefinedOr::Undefined);
    /// assert_eq!(r.expires, UndefinedOr::Undefined);
    /// assert_eq!(r.remaining, UndefinedOr::Undefined);
    /// assert_eq!(r.ratelimit, UndefinedOr::Undefined);
    /// assert_eq!(r.refill, UndefinedOr::Undefined);
    /// ```
    #[must_use]
    pub fn new<T: Into<String>>(api_id: T) -> Self {
        Self {
            api_id: api_id.into(),
            owner_id: UndefinedOr::Undefined,
            byte_length: UndefinedOr::Undefined,
            prefix: UndefinedOr::Undefined,
            name: UndefinedOr::Undefined,
            meta: UndefinedOr::Undefined,
            expires: UndefinedOr::Undefined,
            remaining: UndefinedOr::Undefined,
            ratelimit: UndefinedOr::Undefined,
            refill: UndefinedOr::Undefined,
        }
    }

    /// Sets the owner id for the new key.
    ///
    /// # Arguments
    /// - `owner_id`: The owner id to set.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// let r = CreateKeyRequest::new("test").set_owner_id("jonxslays");
    ///
    /// assert_eq!(r.owner_id.inner().unwrap(), &String::from("jonxslays"));
    /// ```
    #[must_use]
    pub fn set_owner_id<T: Into<String>>(mut self, owner_id: T) -> Self {
        self.owner_id = UndefinedOr::Value(owner_id.into());
        self
    }

    /// Sets the byte length for the new key.
    ///
    /// # Arguments
    /// - `byte_length`: The byte length to set.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// let r = CreateKeyRequest::new("test").set_byte_length(32);
    ///
    /// assert_eq!(r.byte_length.inner().unwrap(), &32);
    /// ```
    #[must_use]
    pub fn set_byte_length(mut self, byte_length: usize) -> Self {
        self.byte_length = UndefinedOr::Value(byte_length);
        self
    }

    /// Sets the prefix for the new key.
    ///
    /// # Arguments
    /// - `prefix`: The prefix to set.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// let r = CreateKeyRequest::new("test").set_prefix("dev");
    ///
    /// assert_eq!(r.prefix.inner().unwrap(), &String::from("dev"));
    /// ```
    #[must_use]
    pub fn set_prefix<T: Into<String>>(mut self, prefix: T) -> Self {
        self.prefix = UndefinedOr::Value(prefix.into());
        self
    }

    /// Sets the name for the new key.
    ///
    /// # Arguments
    /// - `name`: The name to set.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// let r = CreateKeyRequest::new("test").set_name("example_key");
    ///
    /// assert_eq!(r.name.inner().unwrap(), &String::from("example_key"));
    /// ```
    #[must_use]
    pub fn set_name<T: Into<String>>(mut self, name: T) -> Self {
        self.name = UndefinedOr::Value(name.into());
        self
    }

    /// Sets the dynamic meta mapping for the new key.
    ///
    /// # Arguments
    /// - `meta`: The meta to set.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// # use serde_json::json;
    /// let r = CreateKeyRequest::new("test").set_meta(json!({"test": 1}));
    ///
    /// assert_eq!(r.meta.inner().unwrap(), &json!({"test": 1}));
    /// ```
    #[must_use]
    pub fn set_meta(mut self, meta: Value) -> Self {
        self.meta = UndefinedOr::Value(meta);
        self
    }

    /// Sets when this key expires.
    ///
    /// # Arguments
    /// - `expires`: The number of milliseconds in the future this key should
    ///   expire at.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// # use std::time::SystemTime;
    /// let now = SystemTime::now()
    ///    .duration_since(std::time::UNIX_EPOCH)
    ///    .unwrap()
    ///    .as_millis() as usize;
    ///
    /// let r = CreateKeyRequest::new("test").set_expires(1000 * 60 * 10);
    ///
    /// // 10 minutes in the future +- 1 second
    /// let expiration = now + 1000 * 60 * 10;
    /// let range = expiration..expiration+2;
    /// assert!(range.contains(r.expires.inner().unwrap()));
    /// ```
    #[must_use]
    pub fn set_expires(mut self, expires: usize) -> Self {
        let duration = SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_else(|e| {
                eprintln!("Error fetching duration since unix epoch: {e}");
                std::process::exit(1);
            });

        let expires = duration.as_millis() as usize + expires;
        self.expires = UndefinedOr::Value(expires);
        self
    }

    /// Sets the remaining uses for the new key.
    ///
    /// # Arguments
    /// - `remaining`: The remaining uses to set.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// let r = CreateKeyRequest::new("test").set_remaining(100);
    ///
    /// assert_eq!(r.remaining.inner().unwrap(), &100);
    /// ```
    #[must_use]
    pub fn set_remaining(mut self, remaining: usize) -> Self {
        self.remaining = UndefinedOr::Value(remaining);
        self
    }

    /// Sets the ratelimit for the new key.
    ///
    /// # Arguments
    /// - `ratelimit`: The ratelimit uses to set.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// # use unkey::models::Ratelimit;
    /// # use unkey::models::RatelimitType;
    /// let ratelimit = Ratelimit::new(
    ///     RatelimitType::Fast,
    ///     10,
    ///     10000,
    ///     100
    /// );
    ///
    /// let r = CreateKeyRequest::new("test").set_ratelimit(ratelimit.clone());
    ///
    /// assert_eq!(r.ratelimit.inner().unwrap(), &ratelimit);
    /// ```
    #[must_use]
    pub fn set_ratelimit(mut self, ratelimit: Ratelimit) -> Self {
        self.ratelimit = UndefinedOr::Value(ratelimit);
        self
    }

    /// Sets the refill for the new key.
    ///
    /// # Arguments
    /// - `refill`: The refill to set.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::CreateKeyRequest;
    /// # use unkey::models::Refill;
    /// # use unkey::models::RefillInterval;
    /// let refill = Refill::new(100, RefillInterval::Daily);
    ///
    /// let r = CreateKeyRequest::new("test").set_refill(refill.clone());
    ///
    /// assert_eq!(r.refill.inner().unwrap(), &refill);
    /// ```
    #[must_use]
    pub fn set_refill(mut self, refill: Refill) -> Self {
        self.refill = UndefinedOr::Value(refill);
        self
    }
}

/// An incoming create key response.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateKeyResponse {
    /// The unique id of this key.
    pub key_id: String,

    /// The newly created api key.
    pub key: String,
}

/// An individual api key, as the unkey api sees it.
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ApiKey {
    /// The unique id of this key.
    pub id: String,

    /// The optional name for the key.
    pub name: Option<String>,

    /// The id of the api this key belongs to.
    pub api_id: String,

    /// The id of the workspace this key belongs to.
    pub workspace_id: String,

    /// The keys prefix.
    pub start: String,

    /// The owner id of the key, if one was set.
    pub owner_id: Option<String>,

    /// The dynamic metadata associated with the key, if any.
    pub meta: Option<Value>,

    /// The keys creation time in ms since the unix epoch.
    pub created_at: usize,

    /// The unix epoch in ms when this key expires, if it does.
    pub expires: Option<usize>,

    /// The number of uses remaining for this key, if any.
    ///
    /// *Note*: If `None`, the key has unlimited uses remaining.
    pub remaining: Option<usize>,

    /// The ratelimit imposed on this key, if any.
    pub ratelimit: Option<Ratelimit>,

    /// The refill state of this key, if any.
    pub refill: Option<Refill>,
}

/// An outgoing revoke key request.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RevokeKeyRequest {
    /// The unique id of the key to revoke.
    pub key_id: String,
}

impl RevokeKeyRequest {
    /// Creates a new revoke key request.
    ///
    /// # Arguments
    /// - `key_id`: The id of the key to revoke.
    ///
    /// # Returns
    /// The revoke key request.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::RevokeKeyRequest;
    /// let r = RevokeKeyRequest::new("test_ABC123");
    ///
    /// assert_eq!(r.key_id, String::from("test_ABC123"));
    /// ```
    #[must_use]
    #[rustfmt::skip]
    pub fn new<T: Into<String>>(key_id: T) -> Self {
        Self { key_id: key_id.into() }
    }
}

/// An outgoing update key request.
///
/// ## Note
/// All optional values are initialized to the [`UndefinedOr::Undefined`] state.
/// Upon calling the `set_x` method, you may set the value to `Some(_)` or
/// `None`. Setting the value to `None` indicates you would like to remove any
/// value that is currently set for that field on the key.
///
/// e.g. The key you are updating currently has a ratelimit and you call
/// `set_ratelimit(None)` on the update key request. The key will no longer
/// have a ratelimit.
#[derive(Debug, Default, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateKeyRequest {
    /// The id of the key to update.
    pub key_id: String,

    /// The optional new owner id for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub owner_id: UndefinedOr<String>,

    /// The optional new name for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub name: UndefinedOr<String>,

    /// The optional new dynamic meta mapping for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub meta: UndefinedOr<Value>,

    /// The optional new unix epoch in ms when the key should expire.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub expires: UndefinedOr<usize>,

    /// The optional new number of uses remaining to set for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub remaining: UndefinedOr<usize>,

    /// The optional new ratelimit to set for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub ratelimit: UndefinedOr<Ratelimit>,

    /// The optional new refill to set for the key.
    #[serde(skip_serializing_if = "UndefinedOr::is_undefined")]
    pub refill: UndefinedOr<Refill>,
}

impl UpdateKeyRequest {
    /// Creates a new update key request.
    ///
    /// # Arguments
    /// - `key_id`: The id of the key to update.
    ///
    /// # Returns
    /// The new update key request.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::UpdateKeyRequest;
    /// # use unkey::models::UndefinedOr;
    /// let r = UpdateKeyRequest::new("test_123");
    ///
    /// assert_eq!(r.key_id, String::from("test_123"));
    /// assert_eq!(r.owner_id, UndefinedOr::Undefined);
    /// assert_eq!(r.name, UndefinedOr::Undefined);
    /// assert_eq!(r.meta, UndefinedOr::Undefined);
    /// assert_eq!(r.expires, UndefinedOr::Undefined);
    /// assert_eq!(r.remaining, UndefinedOr::Undefined);
    /// assert_eq!(r.ratelimit, UndefinedOr::Undefined);
    /// assert_eq!(r.refill, UndefinedOr::Undefined);
    /// ```
    #[must_use]
    pub fn new<T: Into<String>>(key_id: T) -> Self {
        Self {
            key_id: key_id.into(),
            ..Default::default()
        }
    }

    /// Sets or unsets the owner id for the key.
    ///
    /// # Arguments
    /// - `owner_id`: The owner id to set or unset.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::UpdateKeyRequest;
    /// # use unkey::models::UndefinedOr;
    /// let r = UpdateKeyRequest::new("test");
    ///
    /// assert_eq!(r.owner_id, UndefinedOr::Undefined);
    /// assert_eq!(r.owner_id.inner(), None);
    ///
    /// let r = r.set_owner_id(Some("jonxslays"));
    ///
    /// assert_eq!(r.owner_id, UndefinedOr::Value(String::from("jonxslays")));
    /// assert_eq!(r.owner_id.inner(), Some(&String::from("jonxslays")));
    ///
    /// let r = r.set_owner_id(None);
    ///
    /// assert_eq!(r.owner_id, UndefinedOr::Null);
    /// assert_eq!(r.owner_id.inner(), None);
    /// ```
    #[must_use]
    pub fn set_owner_id(mut self, owner_id: Option<&str>) -> Self {
        self.owner_id = match owner_id {
            Some(id) => Some(id.into()).into(),
            None => None.into(),
        };

        self
    }

    /// Sets or unsets the name for the key.
    ///
    /// # Arguments
    /// - `name`: The name to set or unset.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::UpdateKeyRequest;
    /// # use unkey::models::UndefinedOr;
    /// let r = UpdateKeyRequest::new("test");
    ///
    /// assert_eq!(r.name, UndefinedOr::Undefined);
    /// assert_eq!(r.name.inner(), None);
    ///
    /// let r = r.set_name(Some("test_key"));
    ///
    /// assert_eq!(r.name, UndefinedOr::Value(String::from("test_key")));
    /// assert_eq!(r.name.inner(), Some(&String::from("test_key")));
    ///
    /// let r = r.set_name(None);
    ///
    /// assert_eq!(r.name, UndefinedOr::Null);
    /// assert_eq!(r.name.inner(), None);
    /// ```
    #[must_use]
    pub fn set_name(mut self, name: Option<&str>) -> Self {
        self.name = match name {
            Some(n) => Some(n.into()).into(),
            None => None.into(),
        };

        self
    }

    /// Sets or unsets the dynamic meta mapping for the key.
    ///
    /// # Arguments
    /// - `meta`: The meta to set or unset.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::UpdateKeyRequest;
    /// # use unkey::models::UndefinedOr;
    /// # use serde_json::json;
    /// let r = UpdateKeyRequest::new("test");
    ///
    /// assert_eq!(r.meta, UndefinedOr::Undefined);
    /// assert_eq!(r.meta.inner(), None);
    ///
    /// let r = r.set_meta(Some(json!({"test": 69})));
    ///
    /// assert_eq!(r.meta, UndefinedOr::Value(json!({"test": 69})));
    /// assert_eq!(r.meta.inner(), Some(&json!({"test": 69})));
    ///
    /// let r = r.set_meta(None);
    ///
    /// assert_eq!(r.meta, UndefinedOr::Null);
    /// assert_eq!(r.meta.inner(), None);
    /// ```
    #[must_use]
    pub fn set_meta(mut self, meta: Option<Value>) -> Self {
        self.meta = match meta {
            Some(m) => Some(m).into(),
            None => None.into(),
        };

        self
    }

    /// Sets or unsets the unix epoch in ms indicating when this key expires.
    ///
    /// # Arguments
    /// - `expires`: The expiration epoch to set or unset.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::UpdateKeyRequest;
    /// # use unkey::models::UndefinedOr;
    /// let r = UpdateKeyRequest::new("test");
    ///
    /// assert_eq!(r.expires, UndefinedOr::Undefined);
    /// assert_eq!(r.expires.inner(), None);
    ///
    /// let r = r.set_expires(Some(42));
    ///
    /// assert_eq!(r.expires, UndefinedOr::Value(42));
    /// assert_eq!(r.expires.inner(), Some(&42));
    ///
    /// let r = r.set_expires(None);
    ///
    /// assert_eq!(r.expires, UndefinedOr::Null);
    /// assert_eq!(r.expires.inner(), None);
    /// ```
    #[must_use]
    pub fn set_expires(mut self, expires: Option<usize>) -> Self {
        self.expires = expires.into();
        self
    }

    /// Sets or unsets the remaining uses for the key.
    ///
    /// # Arguments
    /// - `remaining`: The number of remaining uses to set or unset.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::UpdateKeyRequest;
    /// # use unkey::models::UndefinedOr;
    /// let r = UpdateKeyRequest::new("test");
    ///
    /// assert_eq!(r.remaining, UndefinedOr::Undefined);
    /// assert_eq!(r.remaining.inner(), None);
    ///
    /// let r = r.set_remaining(Some(420));
    ///
    /// assert_eq!(r.remaining, UndefinedOr::Value(420));
    /// assert_eq!(r.remaining.inner(), Some(&420));
    ///
    /// let r = r.set_remaining(None);
    ///
    /// assert_eq!(r.remaining, UndefinedOr::Null);
    /// assert_eq!(r.remaining.inner(), None);
    /// ```
    #[must_use]
    pub fn set_remaining(mut self, remaining: Option<usize>) -> Self {
        self.remaining = remaining.into();
        self
    }

    /// Sets or unsets the ratelimit for the key.
    ///
    /// # Arguments
    /// - `ratelimit`: The ratelimit to set or unset.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::UpdateKeyRequest;
    /// # use unkey::models::Ratelimit;
    /// # use unkey::models::RatelimitType;
    /// # use unkey::models::UndefinedOr;
    /// let r = UpdateKeyRequest::new("test");
    ///
    /// assert_eq!(r.ratelimit, UndefinedOr::Undefined);
    /// assert_eq!(r.ratelimit.inner(), None);
    ///
    /// let ratelimit = Ratelimit::new(
    ///     RatelimitType::Fast,
    ///     10,
    ///     10000,
    ///     100
    /// );
    ///
    /// let r = r.set_ratelimit(Some(ratelimit.clone()));
    ///
    /// assert_eq!(r.ratelimit, UndefinedOr::Value(ratelimit.clone()));
    /// assert_eq!(r.ratelimit.inner(), Some(&ratelimit));
    ///
    /// let r = r.set_ratelimit(None);
    ///
    /// assert_eq!(r.ratelimit, UndefinedOr::Null);
    /// assert_eq!(r.ratelimit.inner(), None);
    /// ```
    #[must_use]
    pub fn set_ratelimit(mut self, ratelimit: Option<Ratelimit>) -> Self {
        self.ratelimit = ratelimit.into();
        self
    }

    /// Sets or unsets the refill for the key.
    ///
    /// # Arguments
    /// - `refill`: The refill to set or unset.
    ///
    /// # Returns
    /// Self for chained calls.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::UpdateKeyRequest;
    /// # use unkey::models::Refill;
    /// # use unkey::models::RefillInterval;
    /// # use unkey::models::UndefinedOr;
    /// let r = UpdateKeyRequest::new("test");
    ///
    /// assert_eq!(r.ratelimit, UndefinedOr::Undefined);
    /// assert_eq!(r.ratelimit.inner(), None);
    ///
    /// let refill = Refill::new(100, RefillInterval::Daily);
    ///
    /// let r = r.set_refill(Some(refill.clone()));
    ///
    /// assert_eq!(r.refill, UndefinedOr::Value(refill.clone()));
    /// assert_eq!(r.refill.inner(), Some(&refill));
    ///
    /// let r = r.set_refill(None);
    ///
    /// assert_eq!(r.refill, UndefinedOr::Null);
    /// assert_eq!(r.refill.inner(), None);
    /// ```
    #[must_use]
    pub fn set_refill(mut self, refill: Option<Refill>) -> Self {
        self.refill = refill.into();
        self
    }
}

/// An outgoing get key request.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GetKeyRequest {
    /// The unique id of the key to get.
    pub key_id: String,
}

impl GetKeyRequest {
    /// Creates a new get key request.
    ///
    /// # Arguments
    /// - `key_id`: The id of the key to get.
    ///
    /// # Returns
    /// The get key request.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::GetKeyRequest;
    /// let r = GetKeyRequest::new("test_ABC123");
    ///
    /// assert_eq!(r.key_id, String::from("test_ABC123"));
    /// ```
    #[must_use]
    #[rustfmt::skip]
    pub fn new<T: Into<String>>(key_id: T) -> Self {
        Self { key_id: key_id.into() }
    }
}

/// An outgoing update remaining request.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateRemainingRequest {
    /// The unique id of the key to updating remaining for.
    pub key_id: String,

    /// The value to perform the operation on.
    pub value: Option<usize>,

    /// The update operation to perform.
    pub op: UpdateOp,
}

impl UpdateRemainingRequest {
    /// Creates a new update remaining request.
    ///
    /// # Arguments
    /// - `key_id`: The id of the key to update remaining for.
    /// - `value`: The value to perform the operation on.
    /// - `op`: The update operation to perform.
    ///
    /// # Returns
    /// The update remaining request.
    ///
    /// # Example
    /// ```
    /// # use unkey::models::UpdateRemainingRequest;
    /// # use unkey::models::UpdateOp;
    /// let r = UpdateRemainingRequest::new("test_ABC123", Some(100), UpdateOp::Set);
    ///
    /// assert_eq!(r.key_id, String::from("test_ABC123"));
    /// assert_eq!(r.value.unwrap(), 100);
    /// assert_eq!(r.op, UpdateOp::Set);
    /// ```
    #[must_use]
    #[rustfmt::skip]
    pub fn new<T: Into<String>>(key_id: T, value: Option<usize>, op: UpdateOp) -> Self {
        Self { key_id: key_id.into(), value, op }
    }
}

/// An incoming update remaining request.
#[derive(Debug, Clone, Deserialize)]
pub struct UpdateRemainingResponse {
    /// The number of remaining verifications for the key.
    pub remaining: usize,
}