1- use std:: { env, fs, path:: PathBuf } ;
1+ use std:: { convert :: TryFrom , env, fs, path:: PathBuf } ;
22
3+ use reqwest:: header:: { HeaderMap , HeaderName , HeaderValue } ;
34use serde:: Deserialize ;
45use sha256:: digest;
56use url:: Url ;
@@ -127,45 +128,67 @@ impl LocalFileImport {
127128
128129 match loading_location {
129130 Location :: LocalFile ( path) => fs:: read_to_string ( path) . map_err ( DevrcError :: IoError ) ,
130- Location :: Url ( url) => match reqwest:: blocking:: get ( url. as_str ( ) ) {
131- Ok ( response) if response. status ( ) == 200 => {
132- let content = response. text ( ) . map_err ( |_| DevrcError :: RuntimeError ) ?;
133-
134- if let Some ( control_checksum) = self . checksum . clone ( ) {
135- let content_checksum = digest ( content. as_str ( ) ) ;
136-
137- if control_checksum != content_checksum {
138- return Err ( DevrcError :: UrlImportChecksumError {
139- url : url. as_str ( ) . to_string ( ) ,
140- control_checksum,
141- content_checksum,
142- } ) ;
143- }
144- }
145-
146- Ok ( content)
147- }
148- Ok ( response) => {
149- config. log_level . debug (
150- & format ! (
151- "Loadin ENV FILE error: invalid status code `{:}` ..." ,
152- response. status( )
153- ) ,
154- & config. designer . banner ( ) ,
131+ Location :: Remote { url, auth } => {
132+ let client = reqwest:: blocking:: Client :: new ( ) ;
133+ let mut headers_map: HeaderMap = HeaderMap :: new ( ) ;
134+
135+ if let Some ( ( key, value) ) = auth. get_header ( ) {
136+ headers_map. insert (
137+ HeaderName :: try_from ( key. clone ( ) ) . map_err ( |_| {
138+ DevrcError :: UrlImportHeadersError {
139+ name : key. clone ( ) ,
140+ value : value. clone ( ) ,
141+ }
142+ } ) ?,
143+ HeaderValue :: try_from ( value. clone ( ) ) . map_err ( |_| {
144+ DevrcError :: UrlImportHeadersError {
145+ name : key. clone ( ) ,
146+ value : value. clone ( ) ,
147+ }
148+ } ) ?,
155149 ) ;
156- Err ( DevrcError :: EnvfileUrlImportStatusError {
157- url : url. as_str ( ) . to_string ( ) ,
158- status : response. status ( ) ,
159- } )
160150 }
161- Err ( error) => {
162- config. log_level . debug (
163- & format ! ( "Error: `{:}` ..." , & error) ,
164- & config. designer . banner ( ) ,
165- ) ;
166- Err ( DevrcError :: RuntimeError )
151+
152+ match client. get ( url. as_str ( ) ) . headers ( headers_map) . send ( ) {
153+ Ok ( response) if response. status ( ) == 200 => {
154+ let content = response. text ( ) . map_err ( |_| DevrcError :: RuntimeError ) ?;
155+
156+ if let Some ( control_checksum) = self . checksum . clone ( ) {
157+ let content_checksum = digest ( content. as_str ( ) ) ;
158+
159+ if control_checksum != content_checksum {
160+ return Err ( DevrcError :: UrlImportChecksumError {
161+ url : url. as_str ( ) . to_string ( ) ,
162+ control_checksum,
163+ content_checksum,
164+ } ) ;
165+ }
166+ }
167+
168+ Ok ( content)
169+ }
170+ Ok ( response) => {
171+ config. log_level . debug (
172+ & format ! (
173+ "Loadin ENV FILE error: invalid status code `{:}` ..." ,
174+ response. status( )
175+ ) ,
176+ & config. designer . banner ( ) ,
177+ ) ;
178+ Err ( DevrcError :: EnvfileUrlImportStatusError {
179+ url : url. as_str ( ) . to_string ( ) ,
180+ status : response. status ( ) ,
181+ } )
182+ }
183+ Err ( error) => {
184+ config. log_level . debug (
185+ & format ! ( "Error: `{:}` ..." , & error) ,
186+ & config. designer . banner ( ) ,
187+ ) ;
188+ Err ( DevrcError :: RuntimeError )
189+ }
167190 }
168- } ,
191+ }
169192 _ => Ok ( "" . to_string ( ) ) ,
170193 }
171194 }
@@ -195,7 +218,7 @@ impl LocalFileImport {
195218 Ok ( Location :: LocalFile ( path) )
196219 }
197220 } ,
198- Location :: Url ( url) => match self . path_resolve {
221+ Location :: Remote { url, auth } => match self . path_resolve {
199222 PathResolve :: Relative => {
200223 let path = self
201224 . file
@@ -204,7 +227,10 @@ impl LocalFileImport {
204227 . into_string ( )
205228 . map_err ( |_| DevrcError :: RuntimeError ) ?;
206229 let include_url = url. join ( & path) . map_err ( |_| DevrcError :: RuntimeError ) ?;
207- Ok ( Location :: Url ( include_url) )
230+ Ok ( Location :: Remote {
231+ url : include_url,
232+ auth,
233+ } )
208234 }
209235 PathResolve :: Pwd => {
210236 let path =
0 commit comments