diff options
Diffstat (limited to 'src/response/mod.rs')
| -rw-r--r-- | src/response/mod.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/response/mod.rs b/src/response/mod.rs new file mode 100644 index 0000000..166bc13 --- /dev/null +++ b/src/response/mod.rs | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | mod error; | ||
| 2 | mod success; | ||
| 3 | |||
| 4 | pub use error::ErrorResponse; | ||
| 5 | use serde_json::json; | ||
| 6 | pub use success::SuccessResponse; | ||
| 7 | |||
| 8 | use std::collections::BTreeMap; | ||
| 9 | |||
| 10 | use utoipa::{ | ||
| 11 | IntoResponses, ToSchema, | ||
| 12 | openapi::{ | ||
| 13 | ContentBuilder, RefOr, ResponseBuilder, ResponsesBuilder, example::ExampleBuilder, | ||
| 14 | response::Response, schema::RefBuilder, | ||
| 15 | }, | ||
| 16 | }; | ||
| 17 | |||
| 18 | pub type ApiResult<T> = Result<SuccessResponse<T>, ErrorResponse>; | ||
| 19 | |||
| 20 | pub struct GlobalResponses; | ||
| 21 | |||
| 22 | impl IntoResponses for GlobalResponses { | ||
| 23 | fn responses() -> BTreeMap<String, RefOr<Response>> { | ||
| 24 | ResponsesBuilder::new() | ||
| 25 | .response( | ||
| 26 | "400", | ||
| 27 | ResponseBuilder::new() | ||
| 28 | .content( | ||
| 29 | "application/json", | ||
| 30 | ContentBuilder::new() | ||
| 31 | .schema(Some( | ||
| 32 | RefBuilder::new() | ||
| 33 | .ref_location_from_schema_name(ErrorResponse::name()), | ||
| 34 | )) | ||
| 35 | .examples_from_iter([( | ||
| 36 | "Fail", | ||
| 37 | ExampleBuilder::new().value(Some(json!(ErrorResponse::fail( | ||
| 38 | "SomeFailKind", | ||
| 39 | "some fail message" | ||
| 40 | )))), | ||
| 41 | )]) | ||
| 42 | .build(), | ||
| 43 | ) | ||
| 44 | .description("General response for invalid request"), | ||
| 45 | ) | ||
| 46 | .response( | ||
| 47 | "500", | ||
| 48 | ResponseBuilder::new() | ||
| 49 | .content( | ||
| 50 | "application/json", | ||
| 51 | ContentBuilder::new() | ||
| 52 | .schema(Some( | ||
| 53 | RefBuilder::new() | ||
| 54 | .ref_location_from_schema_name(ErrorResponse::name()), | ||
| 55 | )) | ||
| 56 | .examples_from_iter([( | ||
| 57 | "Error", | ||
| 58 | ExampleBuilder::new().value(Some(json!(ErrorResponse::error( | ||
| 59 | "SomeErrorKind", | ||
| 60 | "some error message" | ||
| 61 | )))), | ||
| 62 | )]) | ||
| 63 | .build(), | ||
| 64 | ) | ||
| 65 | .description("General response when a server error occurs"), | ||
| 66 | ) | ||
| 67 | .build() | ||
| 68 | .into() | ||
| 69 | } | ||
| 70 | } | ||
