@@ -685,18 +685,7 @@ fn cloud_binary_path(project_name: &str, target: Option<&str>) -> PathBuf {
685685
686686#[ cfg( feature = "cloud" ) ]
687687async fn deploy_cloud ( args : CloudArgs ) -> Result < ( ) > {
688- let config = load_config ( ) ?;
689-
690- let token = config. token . as_ref ( ) . ok_or_else ( || {
691- anyhow:: anyhow!( "Not logged in. Run `cargo rustapi login` or `cargo-rustapi login` first." )
692- } ) ?;
693-
694- let cloud_url = config
695- . cloud_url
696- . as_deref ( )
697- . unwrap_or ( "https://api.rustapi.cloud" )
698- . trim_end_matches ( '/' ) ;
699-
688+ let _config = load_config ( ) ?;
700689 let project_name = args
701690 . name
702691 . unwrap_or_else ( || get_package_name ( ) . unwrap_or_else ( |_| "rustapi-app" . to_string ( ) ) ) ;
@@ -747,31 +736,39 @@ async fn deploy_cloud(args: CloudArgs) -> Result<()> {
747736 println ! ( " 📤 Uploading {:.1} MB..." , upload_mb) ;
748737 }
749738
750- let client = cloud_upload_client ( binary_data. len ( ) ) ?;
751- let form = reqwest:: multipart:: Form :: new ( )
752- . text ( "project_name" , project_name. clone ( ) )
753- . part (
754- "binary" ,
755- reqwest:: multipart:: Part :: bytes ( binary_data) . file_name ( format ! ( "{}.bin" , project_name) ) ,
756- ) ;
757-
758- let deploy_resp: DeployResponse = client
759- . post ( format ! ( "{}/deploy" , cloud_url) )
760- . header ( "Authorization" , format ! ( "Bearer {}" , token) )
761- . multipart ( form)
762- . send ( )
763- . await
764- . with_context ( || {
765- format ! (
766- "Failed to upload to RustAPI Cloud ({cloud_url}/deploy). \
767- If the connection timed out, check your upload speed or try again."
768- )
769- } ) ?
770- . json ( )
771- . await
772- . context ( "Invalid response from deploy endpoint" ) ?;
739+ let deploy_id = crate :: cloud:: with_access_token ( |_client, cloud_url, token| {
740+ let project_name = project_name. clone ( ) ;
741+ let binary_data = binary_data. clone ( ) ;
742+ async move {
743+ let upload_client = cloud_upload_client ( binary_data. len ( ) ) ?;
744+ let form = reqwest:: multipart:: Form :: new ( )
745+ . text ( "project_name" , project_name. clone ( ) )
746+ . part (
747+ "binary" ,
748+ reqwest:: multipart:: Part :: bytes ( binary_data)
749+ . file_name ( format ! ( "{}.bin" , project_name) ) ,
750+ ) ;
773751
774- let deploy_id = deploy_resp. deploy_id ;
752+ let deploy_resp: DeployResponse = upload_client
753+ . post ( format ! ( "{}/deploy" , cloud_url. trim_end_matches( '/' ) ) )
754+ . header ( "Authorization" , format ! ( "Bearer {}" , token) )
755+ . multipart ( form)
756+ . send ( )
757+ . await
758+ . with_context ( || {
759+ format ! (
760+ "Failed to upload to RustAPI Cloud ({cloud_url}/deploy). \
761+ If the connection timed out, check your upload speed or try again."
762+ )
763+ } ) ?
764+ . json ( )
765+ . await
766+ . context ( "Invalid response from deploy endpoint" ) ?;
767+
768+ Ok ( deploy_resp. deploy_id )
769+ }
770+ } )
771+ . await ?;
775772
776773 if args. no_wait {
777774 println ! ( " ✅ Deploy queued: {}" , deploy_id) ;
@@ -788,7 +785,12 @@ async fn deploy_cloud(args: CloudArgs) -> Result<()> {
788785 tokio:: time:: sleep ( Duration :: from_secs ( 3 ) ) . await ;
789786
790787 let status_resp: DeployResponse =
791- match fetch_deploy_status ( cloud_url, token, & deploy_id) . await {
788+ match crate :: cloud:: with_access_token ( |client, base, token| {
789+ let deploy_id = deploy_id. clone ( ) ;
790+ async move { fetch_deploy_status ( & client, & base, & token, & deploy_id) . await }
791+ } )
792+ . await
793+ {
792794 Ok ( body) => body,
793795 Err ( _) => continue ,
794796 } ;
@@ -819,14 +821,6 @@ async fn deploy_cloud(args: CloudArgs) -> Result<()> {
819821 Ok ( ( ) )
820822}
821823
822- #[ cfg( feature = "cloud" ) ]
823- fn cloud_http_client ( ) -> Result < reqwest:: Client > {
824- reqwest:: Client :: builder ( )
825- . timeout ( Duration :: from_secs ( 30 ) )
826- . build ( )
827- . context ( "Failed to build HTTP client" )
828- }
829-
830824#[ cfg( feature = "cloud" ) ]
831825fn upload_timeout_secs ( binary_bytes : usize ) -> u64 {
832826 // ~13 MB needs more than 10s on typical home uplinks; scale with payload size.
@@ -846,11 +840,11 @@ fn cloud_upload_client(binary_bytes: usize) -> Result<reqwest::Client> {
846840
847841#[ cfg( feature = "cloud" ) ]
848842async fn fetch_deploy_status (
843+ client : & reqwest:: Client ,
849844 cloud_url : & str ,
850845 token : & str ,
851846 deploy_id : & str ,
852847) -> Result < DeployResponse > {
853- let client = cloud_http_client ( ) ?;
854848 let response = client
855849 . get ( format ! ( "{}/deploy/{}/status" , cloud_url, deploy_id) )
856850 . header ( "Authorization" , format ! ( "Bearer {}" , token) )
@@ -904,19 +898,11 @@ mod status_tests {
904898
905899#[ cfg( feature = "cloud" ) ]
906900async fn deploy_status ( args : DeployStatusArgs ) -> Result < ( ) > {
907- let config = load_config ( ) ?;
908-
909- let token = config. token . as_ref ( ) . ok_or_else ( || {
910- anyhow:: anyhow!( "Not logged in. Run `cargo rustapi login` or `cargo-rustapi login` first." )
911- } ) ?;
912-
913- let cloud_url = config
914- . cloud_url
915- . as_deref ( )
916- . unwrap_or ( "https://api.rustapi.cloud" )
917- . trim_end_matches ( '/' ) ;
918-
919- let status = fetch_deploy_status ( cloud_url, token, & args. deploy_id ) . await ?;
901+ let status = crate :: cloud:: with_access_token ( |client, cloud_url, token| {
902+ let deploy_id = args. deploy_id . clone ( ) ;
903+ async move { fetch_deploy_status ( & client, & cloud_url, & token, & deploy_id) . await }
904+ } )
905+ . await ?;
920906
921907 println ! ( " Deploy ID: {}" , status. deploy_id) ;
922908 println ! ( " Status: {}" , status. status) ;
0 commit comments