Offer the local identity document for binding (first-write-wins).
(&self, identity: &Value)
| 167 | |
| 168 | /// Offer the local identity document for binding (first-write-wins). |
| 169 | pub async fn bind_project_identity(&self, identity: &Value) -> RemoteResult<BindOutcome> { |
| 170 | let url = format!("{}?project-identity=1", self.base_url.as_str()); |
| 171 | let body = serde_json::to_vec(identity) |
| 172 | .map_err(|e| RemoteError::protocol(format!("identity is not valid JSON: {e}")))?; |
| 173 | let response = self |
| 174 | .client |
| 175 | .post(&url) |
| 176 | .header(CONTENT_TYPE, "application/json") |
| 177 | .body(body) |
| 178 | .send() |
| 179 | .await |
| 180 | .map_err(|e| RemoteError::connection_failed(&url, e))?; |
| 181 | match response.status() { |
| 182 | StatusCode::OK | StatusCode::CREATED => Ok(BindOutcome::Bound), |
| 183 | StatusCode::CONFLICT => { |
| 184 | // Read the rejection body (the server's reason) and the |
| 185 | // binding the server already holds so the caller can name the |
| 186 | // divergence; neither read is fatal here. |
| 187 | let body = response.text().await.unwrap_or_default(); |
| 188 | let server = BindServerError::from_body(&body); |
| 189 | let remote = ConflictRemote::from_fetch(self.fetch_project_identity().await); |
| 190 | Ok(BindOutcome::Conflict { remote, server }) |
| 191 | } |
| 192 | StatusCode::NOT_FOUND |
| 193 | | StatusCode::METHOD_NOT_ALLOWED |
| 194 | | StatusCode::NOT_IMPLEMENTED => Ok(BindOutcome::Unsupported), |
| 195 | StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => Err(RemoteError::auth_failed( |
| 196 | &url, |
| 197 | response.text().await.unwrap_or_default(), |
| 198 | )), |
| 199 | s => Err(RemoteError::http( |
| 200 | s.as_u16(), |
| 201 | response.text().await.unwrap_or_default(), |
| 202 | )), |
| 203 | } |
| 204 | } |
| 205 | } |