| 88 | } |
| 89 | |
| 90 | async fn http_request<T: serde::de::DeserializeOwned, B: serde::Serialize>( |
| 91 | &self, |
| 92 | method: &str, |
| 93 | path: &str, |
| 94 | body: B, |
| 95 | ) -> Result<T> { |
| 96 | let body_bytes = match method { |
| 97 | "POST" | "PUT" | "PATCH" => serde_json::to_vec(&body)?, |
| 98 | _ => vec![], |
| 99 | }; |
| 100 | let (status, response_bytes) = |
| 101 | http_request(method, &self.base_url, path, &body_bytes).await?; |
| 102 | if status != 200 { |
| 103 | anyhow::bail!("Server returned error: {}", status); |
| 104 | } |
| 105 | let response: Response<T> = |
| 106 | serde_json::from_slice(&response_bytes).context("Failed to parse response")?; |
| 107 | response.into_result().context("Server returned error") |
| 108 | } |
| 109 | |
| 110 | async fn http_get<T: serde::de::DeserializeOwned>(&self, path: &str) -> Result<T> { |
| 111 | self.http_request("GET", path, ()).await |