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
use reqwest::{IntoUrl, StatusCode, Url};
use serde_derive::Serialize;
use uuid::Uuid;
use crate::consts::DEFAULT_SERVER;
use crate::{Error, Result};
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct ValidateParams<'a> {
access_token: Option<&'a str>,
client_token: Option<Uuid>,
}
pub struct ValidateBuilder<'a> {
params: ValidateParams<'a>,
server: Url,
endpoint: &'a str,
}
impl Default for ValidateParams<'_> {
fn default() -> ValidateParams<'static> {
ValidateParams {
access_token: None,
client_token: None,
}
}
}
impl Default for ValidateBuilder<'_> {
fn default() -> ValidateBuilder<'static> {
ValidateBuilder {
params: ValidateParams::default(),
server: (*DEFAULT_SERVER).clone(),
endpoint: "/validate",
}
}
}
impl<'a> ValidateBuilder<'a> {
pub fn new() -> ValidateBuilder<'a> {
ValidateBuilder::default()
}
pub fn client_token(&mut self, client_token: Uuid) -> &mut ValidateBuilder<'a> {
self.params.client_token = Some(client_token);
self
}
pub fn access_token(&mut self, access_token: &'a str) -> &mut ValidateBuilder<'a> {
self.params.access_token = Some(access_token);
self
}
pub fn server<T: IntoUrl>(&mut self, server: T) -> Result<&mut ValidateBuilder<'a>> {
self.server = server.into_url()?;
Ok(self)
}
pub fn endpoint(&mut self, endpoint: &'a str) -> &mut ValidateBuilder<'a> {
self.endpoint = endpoint;
self
}
pub async fn request(&mut self) -> Result<()> {
if self.params.access_token.is_none() {
return Err(Error::MissingField("access_token"));
}
if self.params.client_token.is_none() {
return Err(Error::MissingField("client_token"));
}
let client = reqwest::Client::new();
let response = client
.post(self.server.join(self.endpoint)?)
.json(&self.params)
.send()
.await?;
match response.status() {
StatusCode::NO_CONTENT => Ok(()),
_ => Err(Error::from_response(response).await),
}
}
}