@@ -15,7 +15,7 @@ trait RequestBuilder {
15
15
fn auth ( self , login : & Login ) -> Self ;
16
16
}
17
17
18
- impl RequestBuilder for reqwest:: blocking :: RequestBuilder {
18
+ impl RequestBuilder for reqwest:: RequestBuilder {
19
19
fn auth ( self , login : & Login ) -> Self {
20
20
match login {
21
21
Login :: OAuth ( token) => self . header ( AUTHORIZATION , format ! ( "token {}" , token) ) ,
@@ -83,16 +83,16 @@ enum AccessTokenResponse {
83
83
}
84
84
85
85
pub struct Client {
86
- client : reqwest:: blocking :: Client ,
86
+ client : reqwest:: Client ,
87
87
}
88
88
89
89
impl Client {
90
90
pub fn build ( ) -> Result < Self > {
91
- let b = reqwest:: blocking :: Client :: builder ( ) . user_agent ( "reqwest" ) ;
91
+ let b = reqwest:: Client :: builder ( ) . user_agent ( "reqwest" ) ;
92
92
Ok ( Client { client : b. build ( ) ? } )
93
93
}
94
94
95
- pub fn user ( & self , login : & Login ) -> Result < UserResponse > {
95
+ pub async fn user ( & self , login : & Login ) -> Result < UserResponse > {
96
96
let res = self
97
97
. client
98
98
. get ( "https://api.github.com/user" )
@@ -101,18 +101,19 @@ impl Client {
101
101
HeaderValue :: from_static ( "application/vnd.github.v3+json" ) ,
102
102
)
103
103
. auth ( & login)
104
- . send ( ) ?;
104
+ . send ( )
105
+ . await ?;
105
106
if res. status ( ) . is_success ( ) {
106
- Ok ( res. json ( ) ?)
107
+ Ok ( res. json ( ) . await ?)
107
108
} else {
108
109
Err ( Error :: new ( ErrorKind :: ApiWithStatus {
109
110
status : res. status ( ) ,
110
- message : res. text ( ) ?,
111
+ message : res. text ( ) . await ?,
111
112
} ) )
112
113
}
113
114
}
114
115
115
- pub fn upload (
116
+ pub async fn upload (
116
117
& self ,
117
118
login : & Login ,
118
119
public : bool ,
@@ -142,18 +143,23 @@ impl Client {
142
143
. post ( "https://api.github.com/gists" )
143
144
. auth ( & login)
144
145
. json ( & req)
145
- . send ( ) ?;
146
+ . send ( )
147
+ . await ?;
146
148
if res. status ( ) . is_success ( ) {
147
- Ok ( res. json :: < GistResponse > ( ) ?)
149
+ Ok ( res. json :: < GistResponse > ( ) . await ?)
148
150
} else {
149
151
Err ( Error :: new ( ErrorKind :: ApiWithStatus {
150
152
status : res. status ( ) ,
151
- message : res. text ( ) ?,
153
+ message : res. text ( ) . await ?,
152
154
} ) )
153
155
}
154
156
}
155
157
156
- pub fn list ( & self , login : Option < & Login > , username : Option < & str > ) -> Result < ListResponse > {
158
+ pub async fn list (
159
+ & self ,
160
+ login : Option < & Login > ,
161
+ username : Option < & str > ,
162
+ ) -> Result < ListResponse > {
157
163
let mut builder = if let Some ( username) = username {
158
164
self . client
159
165
. get ( & format ! ( "https://api.github.com/users/{}/gists" , username) )
@@ -165,50 +171,52 @@ impl Client {
165
171
builder = builder. auth ( & login) ;
166
172
}
167
173
168
- let res = builder. send ( ) ?;
174
+ let res = builder. send ( ) . await ?;
169
175
if res. status ( ) . is_success ( ) {
170
- Ok ( res. json :: < ListResponse > ( ) ?)
176
+ Ok ( res. json :: < ListResponse > ( ) . await ?)
171
177
} else {
172
178
Err ( Error :: new ( ErrorKind :: ApiWithStatus {
173
179
status : res. status ( ) ,
174
- message : res. text ( ) ?,
180
+ message : res. text ( ) . await ?,
175
181
} ) )
176
182
}
177
183
}
178
184
179
- pub fn list_starred ( & self , login : & Login ) -> Result < ListResponse > {
185
+ pub async fn list_starred ( & self , login : & Login ) -> Result < ListResponse > {
180
186
let res = self
181
187
. client
182
188
. get ( "https://api.github.com/gists/starred" )
183
189
. auth ( & login)
184
- . send ( ) ?;
190
+ . send ( )
191
+ . await ?;
185
192
if res. status ( ) . is_success ( ) {
186
- Ok ( res. json :: < ListResponse > ( ) ?)
193
+ Ok ( res. json :: < ListResponse > ( ) . await ?)
187
194
} else {
188
195
Err ( Error :: new ( ErrorKind :: ApiWithStatus {
189
196
status : res. status ( ) ,
190
- message : res. text ( ) ?,
197
+ message : res. text ( ) . await ?,
191
198
} ) )
192
199
}
193
200
}
194
201
195
- pub fn delete ( & self , login : & Login , id : & str ) -> Result < ( ) > {
202
+ pub async fn delete ( & self , login : & Login , id : & str ) -> Result < ( ) > {
196
203
let res = self
197
204
. client
198
205
. delete ( & format ! ( "https://api.github.com/gists/{}" , id) )
199
206
. auth ( & login)
200
- . send ( ) ?;
207
+ . send ( )
208
+ . await ?;
201
209
if res. status ( ) . is_success ( ) {
202
210
Ok ( ( ) )
203
211
} else {
204
212
Err ( Error :: new ( ErrorKind :: ApiWithStatus {
205
213
status : res. status ( ) ,
206
- message : res. text ( ) ?,
214
+ message : res. text ( ) . await ?,
207
215
} ) )
208
216
}
209
217
}
210
218
211
- pub fn request_verification_code (
219
+ pub async fn request_verification_code (
212
220
& self ,
213
221
client_id : & str ,
214
222
scope : & str ,
@@ -222,18 +230,19 @@ impl Client {
222
230
. post ( "https://github.com/login/device/code" )
223
231
. header ( ACCEPT , HeaderValue :: from_static ( "application/json" ) )
224
232
. json ( & req)
225
- . send ( ) ?;
233
+ . send ( )
234
+ . await ?;
226
235
if res. status ( ) . is_success ( ) {
227
- Ok ( res. json ( ) ?)
236
+ Ok ( res. json ( ) . await ?)
228
237
} else {
229
238
Err ( Error :: new ( ErrorKind :: ApiWithStatus {
230
239
status : res. status ( ) ,
231
- message : res. text ( ) ?,
240
+ message : res. text ( ) . await ?,
232
241
} ) )
233
242
}
234
243
}
235
244
236
- pub fn request_access_token (
245
+ pub async fn request_access_token (
237
246
& self ,
238
247
client_id : & str ,
239
248
device_code : & str ,
@@ -245,17 +254,18 @@ impl Client {
245
254
grant_type : "urn:ietf:params:oauth:grant-type:device_code" . to_owned ( ) ,
246
255
} ;
247
256
loop {
248
- std :: thread :: sleep ( Duration :: from_secs ( interval) ) ;
257
+ tokio :: time :: delay_for ( Duration :: from_secs ( interval) ) . await ;
249
258
250
259
let res = self
251
260
. client
252
261
. post ( "https://github.com/login/oauth/access_token" )
253
262
. header ( ACCEPT , HeaderValue :: from_static ( "application/json" ) )
254
263
. json ( & req)
255
- . send ( ) ?;
264
+ . send ( )
265
+ . await ?;
256
266
257
267
if res. status ( ) . is_success ( ) {
258
- match res. json :: < AccessTokenResponse > ( ) ? {
268
+ match res. json :: < AccessTokenResponse > ( ) . await ? {
259
269
AccessTokenResponse :: AccessToken { access_token } => {
260
270
return Ok ( Login :: OAuth ( access_token) )
261
271
}
@@ -267,7 +277,7 @@ impl Client {
267
277
} else {
268
278
return Err ( Error :: new ( ErrorKind :: ApiWithStatus {
269
279
status : res. status ( ) ,
270
- message : res. text ( ) ?,
280
+ message : res. text ( ) . await ?,
271
281
} ) ) ;
272
282
}
273
283
}
0 commit comments