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
#[cfg(feature = "serdejson")]
extern crate serde;
#[cfg(feature = "serdejson")]
extern crate serde_json;
#[cfg(feature = "serdejson")]
#[cfg(test)]
#[macro_use]
extern crate serde_derive;
extern crate base64;
#[macro_use]
extern crate hyper;
extern crate iron;
use std::collections::BTreeSet;
use std::fmt;
use std::error;
pub mod base64_format;
pub use base64_format::ByteArray;
pub mod nullable_format;
pub use nullable_format::Nullable;
#[derive(Clone, Debug, PartialEq)]
pub struct Authorization {
pub subject: String,
pub scopes: Option<BTreeSet<String>>,
}
impl iron::typemap::Key for Authorization {
type Value = Authorization;
}
#[derive(Clone, Debug, PartialEq)]
pub enum AuthData {
Basic(hyper::header::Basic),
Bearer(hyper::header::Bearer),
ApiKey(String),
}
impl iron::typemap::Key for AuthData {
type Value = AuthData;
}
#[derive(Clone, Debug, Default)]
pub struct Context {
pub x_span_id: Option<String>,
pub authorization: Option<Authorization>,
pub auth_data: Option<AuthData>,
}
impl Context {
pub fn new() -> Context {
Context::default()
}
pub fn new_with_span_id<S: Into<String>>(x_span_id: S) -> Context {
Context {
x_span_id: Some(x_span_id.into()),
..Context::default()
}
}
pub fn auth_basic(&mut self, username: &str, password: &str) {
self.auth_data = Some(AuthData::Basic(hyper::header::Basic {
username: username.to_owned(),
password: Some(password.to_owned()),
}));
}
pub fn auth_bearer(&mut self, token: &str) {
self.auth_data = Some(AuthData::Bearer(
hyper::header::Bearer { token: token.to_owned() },
));
}
pub fn auth_apikey(&mut self, apikey: &str) {
self.auth_data = Some(AuthData::ApiKey(apikey.to_owned()));
}
}
header! {
(XSpanId, "X-Span-ID") => [String]
}
#[derive(Clone, Debug)]
pub struct ApiError(pub String);
impl fmt::Display for ApiError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(self as &fmt::Debug).fmt(f)
}
}
impl error::Error for ApiError {
fn description(&self) -> &str {
"Failed to produce a valid response."
}
}
impl<'a> From<&'a str> for ApiError {
fn from(e: &str) -> Self {
ApiError(e.to_string())
}
}
impl From<String> for ApiError {
fn from(e: String) -> Self {
ApiError(e)
}
}
#[cfg(feature = "serdejson")]
impl From<serde_json::Error> for ApiError {
fn from(e: serde_json::Error) -> Self {
ApiError(format!("Response body did not match the schema: {}", e))
}
}