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
use hyper::header::{Accept, UserAgent, Authorization, Headers, qitem};
use hyper::mime::Mime;
use hyper::client::Client;
use hyper::status::StatusCode;
use hyper::net::HttpsConnector;
use hyper_rustls::TlsClient;
use types::*;
use error::*;
use std::io::Read;
#[derive(Debug,Copy,Clone)]
pub enum MediaType {
Json,
RawJson,
TextJson,
HtmlJson,
FullJson,
Diff,
Patch,
Raw,
Sha,
Html,
Base64
}
pub fn media_to_mime(media: MediaType) -> Result<Mime> {
match media {
MediaType::Json => Ok(try_mime!("application/vnd.github.v3+json".parse())),
MediaType::RawJson => Ok(try_mime!("application/vnd.github.v3.raw+json".parse())),
MediaType::TextJson => Ok(try_mime!("application/vnd.github.v3.text+json".parse())),
MediaType::HtmlJson => Ok(try_mime!("application/vnd.github.v3+html+json".parse())),
MediaType::FullJson => Ok(try_mime!("application/vnd.github.v3.full+json".parse())),
MediaType::Diff => Ok(try_mime!("application/vnd.github.v3.diff".parse())),
MediaType::Patch => Ok(try_mime!("application/vnd.github.v3.patch".parse())),
MediaType::Raw => Ok(try_mime!("application/vnd.github.v3.raw".parse())),
MediaType::Sha => Ok(try_mime!("application/vnd.github.v3.sha".parse())),
MediaType::Html => Ok(try_mime!("application/vnd.github.v3.html".parse())),
MediaType::Base64 => Ok(try_mime!("application/vnd.github.v3.base64".parse())),
}
}
pub fn default_headers(auth_token: AccessToken) -> Result<Headers> {
let mime: Mime = try_mime!("application/vnd.github.v3+json".parse());
let token = String::from("token ") + &auth_token;
let mut headers = Headers::new();
headers.set(UserAgent(String::from("github-rs")));
headers.set(Authorization(token));
headers.set(Accept(vec![qitem(mime)]));
Ok(headers)
}
pub fn pagination(url: &mut String, page: Option<u64>, num_per: Option<u64>) {
if let Some(page_num) = page {
url.push_str("?page=");
url.push_str(&page_num.to_string());
}
if let Some(num) = num_per {
url.push_str("&per_page=");
url.push_str(&num.to_string());
}
}
pub fn get(url: &str, input_headers: Headers) -> Result<RawJSON> {
let client = Client::with_connector(HttpsConnector::new(TlsClient::new()));
let request = client.get(url);
let mut buffer = String::new();
let mut res = try_hyper!(request.headers(input_headers).send());
if let Err(err) = res.read_to_string(&mut buffer) {
Err(GithubError::LibIo(err))
} else {
Ok(buffer)
}
}
pub fn get_status_code(url: &str, input_headers: Headers) -> Result<StatusCode> {
let client = Client::with_connector(HttpsConnector::new(TlsClient::new()));
let request = client.get(url);
let res = try_hyper!(request.headers(input_headers).send());
Ok(res.status)
}
pub fn put(url: &str, input_headers: Headers) -> Result<StatusCode> {
let client = Client::with_connector(HttpsConnector::new(TlsClient::new()));
let request = client.put(url);
let res = try_hyper!(request.headers(input_headers).send());
Ok(res.status)
}
pub fn post(url: &str, input_headers: Headers, json: String) -> Result<RawJSON> {
let client = Client::with_connector(HttpsConnector::new(TlsClient::new()));
let request = client.post(url).body(json.as_bytes());
let mut buffer = String::new();
let mut res = try_hyper!(request.headers(input_headers).send());
if let Err(err) = res.read_to_string(&mut buffer) {
Err(GithubError::LibIo(err))
} else {
Ok(buffer)
}
}
pub fn delete(url: &str, input_headers: Headers) -> Result<StatusCode> {
let client = Client::with_connector(HttpsConnector::new(TlsClient::new()));
let request = client.delete(url);
let res = try_hyper!(request.headers(input_headers).send());
Ok(res.status)
}
pub fn delete_with_data(url: &str, input_headers: Headers, json: String) -> Result<StatusCode> {
let client = Client::with_connector(HttpsConnector::new(TlsClient::new()));
let request = client.delete(url).body(json.as_bytes());
let res = try_hyper!(request.headers(input_headers).send());
Ok(res.status)
}
pub fn patch(url: &str, input_headers: Headers, json: String) -> Result<RawJSON> {
let client = Client::with_connector(HttpsConnector::new(TlsClient::new()));
let request = client.patch(url).body(json.as_bytes());
let mut buffer = String::new();
let mut res = try_hyper!(request.headers(input_headers).send());
if let Err(err) = res.read_to_string(&mut buffer) {
Err(GithubError::LibIo(err))
} else {
Ok(buffer)
}
}