-
Notifications
You must be signed in to change notification settings - Fork 38
/
api.rs
125 lines (107 loc) · 3.75 KB
/
api.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
//! Contains various type definitions for API request and response types that leverage the Bindle
//! objects
use serde::{Deserialize, Serialize};
use crate::invoice::{Invoice, Label};
use crate::search::SearchOptions;
use crate::SignatureRole;
/// A custom type for responding to invoice creation requests. Because invoices can be created
/// before parcels are uploaded, this allows the API to inform the user if there are missing parcels
/// in the bindle spec
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct InvoiceCreateResponse {
pub invoice: Invoice,
pub missing: Option<Vec<Label>>,
}
/// A response to a missing parcels request. TOML doesn't support top level arrays, so they
/// must be embedded in a table
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct MissingParcelsResponse {
pub missing: Vec<Label>,
}
#[derive(Deserialize, Serialize)]
pub struct HealthResponse {
pub status: String,
pub version: String,
}
/// A string error message returned from the server
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorResponse {
pub error: String,
}
/// Available options for the query API
#[derive(Debug, Serialize, Deserialize, Default)]
#[serde(deny_unknown_fields)]
pub struct QueryOptions {
#[serde(alias = "q")]
pub query: Option<String>,
#[serde(alias = "v")]
pub version: Option<String>,
#[serde(alias = "o")]
pub offset: Option<u64>,
#[serde(alias = "l")]
pub limit: Option<u8>,
pub strict: Option<bool>,
pub yanked: Option<bool>,
}
impl From<QueryOptions> for SearchOptions {
fn from(qo: QueryOptions) -> Self {
let defaults = SearchOptions::default();
SearchOptions {
limit: qo.limit.unwrap_or(defaults.limit),
offset: qo.offset.unwrap_or(defaults.offset),
strict: qo.strict.unwrap_or(defaults.strict),
yanked: qo.yanked.unwrap_or(defaults.yanked),
}
}
}
/// Available query string options for the keyring API
#[derive(Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct KeyOptions {
#[serde(default)]
#[serde(deserialize_with = "parse_role_list")]
pub roles: Vec<SignatureRole>,
}
struct RoleVisitor(std::marker::PhantomData<fn() -> Vec<SignatureRole>>);
impl<'de> serde::de::Visitor<'de> for RoleVisitor {
type Value = Vec<SignatureRole>;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a comma delimited list of SignatureRoles")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let roles = v
.split(',')
.map(|s| {
s.parse::<SignatureRole>()
.map_err(|e| serde::de::Error::custom(e))
})
.collect::<Result<Vec<_>, _>>()?;
Ok(roles)
}
}
fn parse_role_list<'de, D>(deserializer: D) -> Result<Vec<SignatureRole>, D::Error>
where
D: serde::Deserializer<'de>,
{
let visitor = RoleVisitor(std::marker::PhantomData);
deserializer.deserialize_str(visitor)
}
// Keeping these types private for now until we stabilize exactly how we want to handle it
#[derive(Deserialize, Serialize, Debug)]
pub(crate) struct LoginParams {
pub provider: String,
}
/// Adds extra fields onto the device authorization response so we can pass back the client id that
/// should be used
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct DeviceAuthorizationExtraFields {
pub client_id: String,
pub token_url: String,
}
#[cfg(any(feature = "client", feature = "server"))]
impl oauth2::devicecode::ExtraDeviceAuthorizationFields for DeviceAuthorizationExtraFields {}