1- use anyhow:: bail;
21use backoff:: { retry, ExponentialBackoff } ;
32use data_encoding:: BASE64 ;
43use digest:: Digest ;
54use isahc:: {
65 config:: { CaCertificate , Configurable , RedirectPolicy , SslOption } ,
76 Body , Request , RequestExt ,
87} ;
9- use log:: info;
108use nix_nar:: { Encoder , NarError } ;
119use serde_json:: { Map , Value } ;
1210use sha2:: Sha256 ;
@@ -17,7 +15,7 @@ use std::{
1715} ;
1816use url:: Url ;
1917
20- pub fn get_url ( url : & Url ) -> Result < Body , anyhow :: Error > {
18+ pub fn get_url ( url : & Url ) -> Result < Body , isahc :: Error > {
2119 let mut request = Request :: get ( url. as_str ( ) ) . redirect_policy ( RedirectPolicy :: Limit ( 10 ) ) ;
2220
2321 // Respect SSL_CERT_FILE if environment variable exists
@@ -39,27 +37,16 @@ pub fn get_url(url: &Url) -> Result<Body, anyhow::Error> {
3937 if let Ok ( npm_tokens) = env:: var ( "NIX_NPM_TOKENS" ) {
4038 if let Ok ( tokens) = serde_json:: from_str :: < Map < String , Value > > ( & npm_tokens) {
4139 if let Some ( token) = tokens. get ( host) . and_then ( serde_json:: Value :: as_str) {
42- info ! ( "Found NPM token for {}. Adding authorization header to request." , host) ;
4340 request = request. header ( "Authorization" , format ! ( "Bearer {token}" ) ) ;
4441 }
4542 }
4643 }
4744 }
4845
49- let res = request. body ( ( ) ) ?. send ( ) ?;
50- if !res. status ( ) . is_success ( ) {
51- if res. status ( ) . is_client_error ( ) {
52- bail ! ( "Client error: {}" , res. status( ) ) ;
53- }
54- if res. status ( ) . is_server_error ( ) {
55- bail ! ( "Server error: {}" , res. status( ) ) ;
56- }
57- bail ! ( "{}" , res. status( ) ) ;
58- }
59- Ok ( res. into_body ( ) )
46+ Ok ( request. body ( ( ) ) ?. send ( ) ?. into_body ( ) )
6047}
6148
62- pub fn get_url_body_with_retry ( url : & Url ) -> Result < Vec < u8 > , anyhow :: Error > {
49+ pub fn get_url_body_with_retry ( url : & Url ) -> Result < Vec < u8 > , isahc :: Error > {
6350 retry ( ExponentialBackoff :: default ( ) , || {
6451 get_url ( url)
6552 . and_then ( |mut body| {
@@ -69,15 +56,12 @@ pub fn get_url_body_with_retry(url: &Url) -> Result<Vec<u8>, anyhow::Error> {
6956
7057 Ok ( buf)
7158 } )
72- . map_err ( |err| match err. downcast_ref :: < isahc:: Error > ( ) {
73- Some ( isahc_err) => {
74- if isahc_err. is_network ( ) || isahc_err. is_timeout ( ) {
75- backoff:: Error :: transient ( err)
76- } else {
77- backoff:: Error :: permanent ( err)
78- }
59+ . map_err ( |err| {
60+ if err. is_network ( ) || err. is_timeout ( ) {
61+ backoff:: Error :: transient ( err)
62+ } else {
63+ backoff:: Error :: permanent ( err)
7964 }
80- None => backoff:: Error :: permanent ( err) ,
8165 } )
8266 } )
8367 . map_err ( |backoff_err| match backoff_err {
0 commit comments