@@ -10,10 +10,12 @@ use clap::{ColorChoice, Parser, Subcommand, ValueEnum};
10
10
use config_file:: { ConfigFile , ConfigType } ;
11
11
use console:: { self , Term } ;
12
12
use futures:: stream:: { self , StreamExt } ;
13
+ use http_cache_reqwest:: { CACacheManager , Cache , CacheMode , HttpCache } ;
13
14
use indicatif:: { HumanCount , HumanDuration , ProgressBar , ProgressIterator } ;
14
15
use lazy_static:: lazy_static;
15
16
use package:: parsing:: ParsedPackage ;
16
- use reqwest:: Client ;
17
+ use reqwest:: { header:: HeaderMap , ClientBuilder as NormalClientBuilder } ;
18
+ use reqwest_middleware:: { ClientBuilder , ClientWithMiddleware } ;
17
19
use std:: fs:: { create_dir, read_dir, read_to_string, remove_dir, write} ;
18
20
use std:: io:: { stdin, Read } ;
19
21
use std:: path:: { Path , PathBuf } ;
@@ -146,7 +148,7 @@ async fn main() {
146
148
ColorChoice :: Never => set_colors ( false ) ,
147
149
ColorChoice :: Auto => set_colors ( Term :: stdout ( ) . is_term ( ) && Term :: stderr ( ) . is_term ( ) ) ,
148
150
}
149
- async fn get_cfg ( path : PathBuf , client : Client ) -> ConfigFile {
151
+ async fn get_cfg ( path : PathBuf , client : ClientWithMiddleware ) -> ConfigFile {
150
152
let mut contents = String :: from ( "" ) ;
151
153
if path == Path :: new ( "-" ) {
152
154
let bytes = stdin ( )
@@ -168,8 +170,18 @@ async fn main() {
168
170
write ( path, lockfile) . expect ( "Writing lock file should be ok" ) ;
169
171
}
170
172
}
173
+ let mut headers = HeaderMap :: new ( ) ;
174
+ headers. insert (
175
+ "User-Agent" ,
176
+ format ! (
177
+ "gpm/{} (godot-package-manager/cli on GitHub)" ,
178
+ env!( "CARGO_PKG_VERSION" )
179
+ )
180
+ . parse ( )
181
+ . unwrap ( ) ,
182
+ ) ;
183
+ let client = mkclient ( ) ;
171
184
let _ = BEGIN . elapsed ( ) ; // needed to initialize the instant for whatever reason
172
- let client = Client :: new ( ) ;
173
185
match args. action {
174
186
Actions :: Update => {
175
187
let c = & mut get_cfg ( args. config_file , client. clone ( ) ) . await ;
@@ -207,7 +219,39 @@ async fn main() {
207
219
}
208
220
}
209
221
210
- async fn update ( cfg : & mut ConfigFile , modify : bool , v : Verbosity , client : Client ) {
222
+ pub fn mkclient ( ) -> ClientWithMiddleware {
223
+ let mut headers = HeaderMap :: new ( ) ;
224
+ headers. insert (
225
+ "User-Agent" ,
226
+ format ! (
227
+ "gpm/{} (godot-package-manager/cli on GitHub)" ,
228
+ env!( "CARGO_PKG_VERSION" )
229
+ )
230
+ . parse ( )
231
+ . unwrap ( ) ,
232
+ ) ;
233
+ let path = if Path :: new ( ".import" ) . is_dir ( ) {
234
+ ".import/gpm_cache"
235
+ } else if Path :: new ( ".godot" ) . is_dir ( ) {
236
+ ".godot/gpm_cache"
237
+ } else {
238
+ ".gpm_cache"
239
+ } ;
240
+ ClientBuilder :: new (
241
+ NormalClientBuilder :: new ( )
242
+ . default_headers ( headers)
243
+ . build ( )
244
+ . unwrap ( ) ,
245
+ )
246
+ . with ( Cache ( HttpCache {
247
+ mode : CacheMode :: Default ,
248
+ manager : CACacheManager { path : path. into ( ) } ,
249
+ options : None ,
250
+ } ) )
251
+ . build ( )
252
+ }
253
+
254
+ async fn update ( cfg : & mut ConfigFile , modify : bool , v : Verbosity , client : ClientWithMiddleware ) {
211
255
if !Path :: new ( "./addons/" ) . exists ( ) {
212
256
create_dir ( "./addons/" ) . expect ( "Should be able to create addons folder" ) ;
213
257
}
@@ -248,7 +292,7 @@ async fn update(cfg: &mut ConfigFile, modify: bool, v: Verbosity, client: Client
248
292
Finished ( String ) ,
249
293
}
250
294
let bar_or_info = v. bar ( ) || v. info ( ) ;
251
- let ( tx, rx) = bar_or_info. then ( || channel ( ) ) . unzip ( ) ;
295
+ let ( tx, rx) = bar_or_info. then ( channel) . unzip ( ) ;
252
296
let buf = stream:: iter ( packages)
253
297
. map ( |mut p| async {
254
298
let p_name = p. to_string ( ) ;
@@ -394,7 +438,7 @@ async fn tree(
394
438
charset : CharSet ,
395
439
prefix : PrefixType ,
396
440
print_tarballs : bool ,
397
- client : Client ,
441
+ client : ClientWithMiddleware ,
398
442
) -> String {
399
443
let mut tree: String = if let Ok ( s) = current_dir ( ) {
400
444
format ! ( "{}\n " , s. to_string_lossy( ) )
@@ -434,7 +478,7 @@ async fn tree(
434
478
print_tarballs : bool ,
435
479
depth : u32 ,
436
480
count : & mut u64 ,
437
- client : Client ,
481
+ client : ClientWithMiddleware ,
438
482
) {
439
483
// the index is used to decide if the package is the last package,
440
484
// so we can use a L instead of a T.
@@ -484,7 +528,7 @@ async fn tree(
484
528
tree
485
529
}
486
530
487
- async fn init ( mut packages : Vec < Package > , client : Client ) -> Result < ( ) > {
531
+ async fn init ( mut packages : Vec < Package > , client : ClientWithMiddleware ) -> Result < ( ) > {
488
532
let mut c = ConfigFile :: default ( ) ;
489
533
if packages. is_empty ( ) {
490
534
let mut has_asked = false ;
@@ -594,7 +638,7 @@ mod test_utils {
594
638
#[ tokio:: test]
595
639
async fn gpm ( ) {
596
640
let _t = test_utils:: mktemp ( ) ;
597
- let c = Client :: new ( ) ;
641
+ let c = mkclient ( ) ;
598
642
let cfg_file =
599
643
& mut config_file:: ConfigFile :: new ( & r#"packages: {"@bendn/test":2.0.10}"# . into ( ) , c. clone ( ) )
600
644
. await ;
0 commit comments