feature/ruby-runner
claude/smbcloud-github-app-k9641p
development
feature/apps
feature/build-release
feature/ci-docs
feature/ci-ruby-gems
feature/cicd
feature/completion
feature/dist-nuget
feature/fix-ci-sdk-pypi-rubygem
feature/fix-clippy-on-stable-channel
feature/fix-deployment-script
feature/github-email
feature/gresiq-v1
feature/project-new-structure
feature/rsync-deploy
feature/ruby-runner
feature/sdk-npm
feature/sdk-workflows
feature/smbcloud-cli-release-npm
feature/smbcloud-deploy
feature/wasm
main
release/v0.7.4
| 1 | use log::error; |
| 2 | use reqwest::{RequestBuilder, Response}; |
| 3 | use serde::de::DeserializeOwned; |
| 4 | use smbcloud_model::error_codes::{ErrorCode, ErrorResponse}; |
| 5 | #[cfg(debug_assertions)] |
| 6 | const LOG_RESPONSE_BODY: bool = false; // You know what to do here. |
| 7 | #[cfg(not(debug_assertions))] |
| 8 | const LOG_RESPONSE_BODY: bool = false; |
| 9 | |
| 10 | pub async fn parse_error_response<T: DeserializeOwned>( |
| 11 | response: Response, |
| 12 | ) -> Result<T, ErrorResponse> { |
| 13 | let response_body = match response.text().await { |
| 14 | Ok(body) => body, |
| 15 | Err(e) => { |
| 16 | error!("Failed to get response body: {:?}", e); |
| 17 | return Err(ErrorResponse::Error { |
| 18 | error_code: ErrorCode::NetworkError, |
| 19 | message: ErrorCode::NetworkError.message(None).to_string(), |
| 20 | }); |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | if LOG_RESPONSE_BODY { |
| 25 | println!(); |
| 26 | println!("Parse Error >>>>"); |
| 27 | println!("{:?}", serde_json::to_string_pretty(&response_body)); |
| 28 | println!("Parse Error >>>>"); |
| 29 | println!(); |
| 30 | } |
| 31 | |
| 32 | let e = match serde_json::from_str::<ErrorResponse>(&response_body) { |
| 33 | Ok(json) => json, |
| 34 | Err(e) => { |
| 35 | error!("Failed to parse error response: {:?}", e); |
| 36 | return Err(ErrorResponse::Error { |
| 37 | error_code: ErrorCode::ParseError, |
| 38 | message: ErrorCode::ParseError.message(None).to_string(), |
| 39 | }); |
| 40 | } |
| 41 | }; |
| 42 | error!("Error response: {:?}", e); |
| 43 | Err(e) |
| 44 | } |
| 45 | |
| 46 | pub async fn request<R: DeserializeOwned>(builder: RequestBuilder) -> Result<R, ErrorResponse> { |
| 47 | let response = builder.send().await; |
| 48 | let response = match response { |
| 49 | Ok(response) => response, |
| 50 | Err(e) => { |
| 51 | error!("Failed to get response: {:?}", e); |
| 52 | return Err(ErrorResponse::Error { |
| 53 | error_code: ErrorCode::NetworkError, |
| 54 | message: ErrorCode::NetworkError.message(None).to_string(), |
| 55 | }); |
| 56 | } |
| 57 | }; |
| 58 | let response = match response.status() { |
| 59 | reqwest::StatusCode::OK | reqwest::StatusCode::CREATED => response, |
| 60 | status => { |
| 61 | error!("Failed to get response: {:?}", status); |
| 62 | return parse_error_response(response).await; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | let response_body = match response.text().await { |
| 67 | Ok(body) => body, |
| 68 | Err(e) => { |
| 69 | error!("Failed to get response body: {:?}", e); |
| 70 | return Err(ErrorResponse::Error { |
| 71 | error_code: ErrorCode::NetworkError, |
| 72 | message: ErrorCode::NetworkError.message(None).to_string(), |
| 73 | }); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | if LOG_RESPONSE_BODY { |
| 78 | println!(); |
| 79 | println!("Parse >>>>"); |
| 80 | println!("{:?}", serde_json::to_string_pretty(&response_body)); |
| 81 | println!("Parse >>>>"); |
| 82 | println!(); |
| 83 | } |
| 84 | |
| 85 | let response = match serde_json::from_str::<R>(&response_body) { |
| 86 | Ok(response) => response, |
| 87 | Err(e) => { |
| 88 | error!("Failed to parse response: {:?}", e); |
| 89 | return Err(ErrorResponse::Error { |
| 90 | error_code: ErrorCode::ParseError, |
| 91 | message: e.to_string(), |
| 92 | }); |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | Ok(response) |
| 97 | } |