@@ -2,15 +2,14 @@ use crate::generated::exports::obelisk_flyio::activity_fly_http::apps;
22use crate :: { API_BASE_URL , AppName , OrgSlug , request_with_api_token} ;
33use anyhow:: anyhow;
44use serde:: { Deserialize , Serialize } ;
5- use wstd:: http:: request:: JsonRequest as _;
6- use wstd:: http:: { Client , Method , StatusCode } ;
5+ use wstd:: http:: { Body , Client , Method , StatusCode } ;
76use wstd:: runtime:: block_on;
87
98async fn get ( app_name : AppName ) -> Result < Option < apps:: App > , anyhow:: Error > {
109 let request = request_with_api_token ( ) ?
1110 . method ( Method :: GET )
1211 . uri ( format ! ( "{API_BASE_URL}/apps/{app_name}" ) )
13- . body ( wstd :: io :: empty ( ) ) ?;
12+ . body ( Body :: empty ( ) ) ?;
1413 let mut response = Client :: new ( ) . send ( request) . await ?;
1514
1615 if response. status ( ) . is_success ( ) {
@@ -20,11 +19,9 @@ async fn get(app_name: AppName) -> Result<Option<apps::App>, anyhow::Error> {
2019 Ok ( None )
2120 } else {
2221 let error_status = response. status ( ) ;
23- let error_body = response. body_mut ( ) . bytes ( ) . await ?;
24- Err ( anyhow ! (
25- "failed with status {error_status}: {}" ,
26- String :: from_utf8_lossy( & error_body)
27- ) )
22+ let mut response = response. into_body ( ) ;
23+ let error_body = response. str_contents ( ) . await ?;
24+ Err ( anyhow ! ( "failed with status {error_status}: {error_body}" , ) )
2825 }
2926}
3027
@@ -46,7 +43,7 @@ async fn put(org_slug: OrgSlug, app_name: AppName) -> Result<apps::App, anyhow::
4643 let post_request = request_with_api_token ( ) ?
4744 . method ( Method :: POST )
4845 . uri ( format ! ( "{API_BASE_URL}/apps" ) )
49- . json ( & request_body) ?;
46+ . body ( Body :: from_json ( & request_body) ? ) ?;
5047
5148 let mut response = client. send ( post_request) . await ?;
5249
@@ -64,14 +61,14 @@ async fn put(org_slug: OrgSlug, app_name: AppName) -> Result<apps::App, anyhow::
6461
6562 // Investigate if the app already exists
6663 let original_post_status = response. status ( ) ;
67- let original_post_error = response. into_body ( ) . bytes ( ) . await ;
64+ let mut response = response. into_body ( ) ;
6865
6966 if original_post_status == StatusCode :: UNPROCESSABLE_ENTITY {
7067 // Prepare a GET request to check for the existing app.
7168 let get_request = request_with_api_token ( ) ?
7269 . method ( Method :: GET )
7370 . uri ( format ! ( "{API_BASE_URL}/apps/{app_name}" ) )
74- . body ( wstd :: io :: empty ( ) ) ?;
71+ . body ( Body :: empty ( ) ) ?;
7572
7673 let mut get_response = client. send ( get_request) . await ?;
7774
@@ -108,19 +105,17 @@ async fn put(org_slug: OrgSlug, app_name: AppName) -> Result<apps::App, anyhow::
108105 }
109106 // The GET request failed, so the app doesn't exist.
110107 // The original error from the POST request is the true cause of failure.
108+ let original_post_error = response. str_contents ( ) . await ?;
111109 Err ( anyhow ! (
112- "failed with status {original_post_status}: {}" ,
113- original_post_error
114- . map( |vec| String :: from_utf8_lossy( & vec) . to_string( ) )
115- . unwrap_or_else( |e| e. to_string( ) )
110+ "failed with status {original_post_status}: {original_post_error}" ,
116111 ) )
117112}
118113
119114async fn list ( org_slug : OrgSlug ) -> Result < Vec < apps:: App > , anyhow:: Error > {
120115 let request = request_with_api_token ( ) ?
121116 . method ( Method :: GET )
122117 . uri ( format ! ( "{API_BASE_URL}/apps?org_slug={org_slug}" ) )
123- . body ( wstd :: io :: empty ( ) ) ?;
118+ . body ( Body :: empty ( ) ) ?;
124119 let mut response = Client :: new ( ) . send ( request) . await ?;
125120
126121 if response. status ( ) . is_success ( ) {
@@ -132,11 +127,9 @@ async fn list(org_slug: OrgSlug) -> Result<Vec<apps::App>, anyhow::Error> {
132127 Ok ( apps_response. apps )
133128 } else {
134129 let error_status = response. status ( ) ;
135- let error_body = response. body_mut ( ) . bytes ( ) . await ?;
136- Err ( anyhow ! (
137- "failed with status {error_status}: {}" ,
138- String :: from_utf8_lossy( & error_body)
139- ) )
130+ let mut response = response. into_body ( ) ;
131+ let error_body = response. str_contents ( ) . await ?;
132+ Err ( anyhow ! ( "failed with status {error_status}: {error_body}" , ) )
140133 }
141134}
142135
@@ -148,7 +141,7 @@ async fn delete(app_name: AppName, force: bool) -> Result<(), anyhow::Error> {
148141 let request = request_with_api_token ( ) ?
149142 . method ( Method :: DELETE )
150143 . uri ( url)
151- . body ( wstd :: io :: empty ( ) ) ?;
144+ . body ( Body :: empty ( ) ) ?;
152145
153146 let response = Client :: new ( ) . send ( request) . await ?;
154147 let status = response. status ( ) ;
@@ -157,11 +150,9 @@ async fn delete(app_name: AppName, force: bool) -> Result<(), anyhow::Error> {
157150 Ok ( ( ) )
158151 } else {
159152 let error_status = response. status ( ) ;
160- let error_body = response. into_body ( ) . bytes ( ) . await ?;
161- Err ( anyhow ! (
162- "failed with status {error_status}: {}" ,
163- String :: from_utf8_lossy( & error_body)
164- ) )
153+ let mut response = response. into_body ( ) ;
154+ let error_body = response. str_contents ( ) . await ?;
155+ Err ( anyhow ! ( "failed with status {error_status}: {error_body}" , ) )
165156 }
166157}
167158
0 commit comments