Skip to content

Commit

Permalink
chore: add bad request response to get events API (#329)
Browse files Browse the repository at this point in the history
improve event creation 400 response as well
  • Loading branch information
dav1do authored May 1, 2024
1 parent 6611fb5 commit 6113064
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 9 deletions.
2 changes: 1 addition & 1 deletion api-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To see how to make this your own, look here:
[README]((https://openapi-generator.tech))

- API version: 0.15.0
- Build date: 2024-04-29T17:53:06.595190991Z[Etc/UTC]
- Build date: 2024-05-01T13:10:53.544095-06:00[America/Denver]



Expand Down
6 changes: 6 additions & 0 deletions api-server/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ paths:
schema:
$ref: '#/components/schemas/Event'
description: success
"400":
content:
application/json:
schema:
$ref: '#/components/schemas/BadRequestResponse'
description: bad request
"404":
content:
text/plain:
Expand Down
14 changes: 14 additions & 0 deletions api-server/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,20 @@ where
})?;
Ok(EventsEventIdGetResponse::Success(body))
}
400 => {
let body = response.into_body();
let body = body
.into_raw()
.map_err(|e| ApiError(format!("Failed to read response: {}", e)))
.await?;
let body = str::from_utf8(&body)
.map_err(|e| ApiError(format!("Response was not valid UTF8: {}", e)))?;
let body =
serde_json::from_str::<models::BadRequestResponse>(body).map_err(|e| {
ApiError(format!("Response body did not match the schema: {}", e))
})?;
Ok(EventsEventIdGetResponse::BadRequest(body))
}
404 => {
let body = response.into_body();
let body = body
Expand Down
2 changes: 2 additions & 0 deletions api-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub enum DebugHeapGetResponse {
pub enum EventsEventIdGetResponse {
/// success
Success(models::Event),
/// bad request
BadRequest(models::BadRequestResponse),
/// Event not found
EventNotFound(String),
/// Internal server error
Expand Down
11 changes: 11 additions & 0 deletions api-server/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,17 @@ where
.expect("impossible to fail to serialize");
*response.body_mut() = Body::from(body_content);
}
EventsEventIdGetResponse::BadRequest(body) => {
*response.status_mut() = StatusCode::from_u16(400)
.expect("Unable to turn 400 into a StatusCode");
response.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_str("application/json")
.expect("Unable to create Content-Type header for EVENTS_EVENT_ID_GET_BAD_REQUEST"));
let body_content = serde_json::to_string(&body)
.expect("impossible to fail to serialize");
*response.body_mut() = Body::from(body_content);
}
EventsEventIdGetResponse::EventNotFound(body) => {
*response.status_mut() = StatusCode::from_u16(404)
.expect("Unable to turn 404 into a StatusCode");
Expand Down
6 changes: 6 additions & 0 deletions api/ceramic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Event"
"400":
description: bad request
content:
application/json:
schema:
$ref: "#/components/schemas/BadRequestResponse"
"404":
description: Event not found
content:
Expand Down
29 changes: 21 additions & 8 deletions api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,14 @@ where
}

pub async fn post_events(&self, event: Event) -> Result<EventsPostResponse, ErrorResponse> {
let event_id = decode_event_id(&event.id)?;
let event_data = decode_event_data(&event.data)?;
let event_id = match decode_event_id(&event.id) {
Ok(v) => v,
Err(e) => return Ok(EventsPostResponse::BadRequest(e)),
};
let event_data = match decode_event_data(&event.data) {
Ok(v) => v,
Err(e) => return Ok(EventsPostResponse::BadRequest(e)),
};
let (tx, rx) = tokio::sync::oneshot::channel();
tokio::time::timeout(
INSERT_ENQUEUE_TIMEOUT,
Expand Down Expand Up @@ -442,7 +448,10 @@ where
&self,
event_id: String,
) -> Result<EventsEventIdGetResponse, ErrorResponse> {
let decoded_event_id = decode_event_id(&event_id)?;
let decoded_event_id = match decode_event_id(&event_id) {
Ok(v) => v,
Err(e) => return Ok(EventsEventIdGetResponse::BadRequest(e)),
};
match self.model.value_for_key(&decoded_event_id).await {
Ok(Some(data)) => {
let event = BuildResponse::event(decoded_event_id, data);
Expand Down Expand Up @@ -530,16 +539,20 @@ where
}
}

pub(crate) fn decode_event_id(value: &str) -> Result<EventId, ErrorResponse> {
pub(crate) fn decode_event_id(value: &str) -> Result<EventId, BadRequestResponse> {
multibase::decode(value)
.map_err(|err| ErrorResponse::new(format!("multibase error: {err}")))?
.map_err(|err| {
BadRequestResponse::new(format!("Invalid Event ID: multibase error: {err}"))
})?
.1
.try_into()
.map_err(|err| ErrorResponse::new(format!("invalid event id: {err}")))
.map_err(|err| BadRequestResponse::new(format!("Invalid event id: {err}")))
}
pub(crate) fn decode_event_data(value: &str) -> Result<Vec<u8>, ErrorResponse> {
pub(crate) fn decode_event_data(value: &str) -> Result<Vec<u8>, BadRequestResponse> {
Ok(multibase::decode(value)
.map_err(|err| ErrorResponse::new(format!("multibase error: {err}")))?
.map_err(|err| {
BadRequestResponse::new(format!("Invalid event data: multibase error: {err}"))
})?
.1)
}

Expand Down

0 comments on commit 6113064

Please sign in to comment.