diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-06-19 18:31:55 -0700 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-06-19 18:31:55 -0700 |
commit | d2f50808ef9b96afc36d864adec74f10c9cea9af (patch) | |
tree | 18d563e5570e9b1d578326664e7fe1cf3bc8ea9e /rust/fatcat-api/src/server.rs | |
parent | b262ad227ba5293e210f1137e8080be0796a8c86 (diff) | |
download | fatcat-d2f50808ef9b96afc36d864adec74f10c9cea9af.tar.gz fatcat-d2f50808ef9b96afc36d864adec74f10c9cea9af.zip |
implement (most) of stats endpoint
Diffstat (limited to 'rust/fatcat-api/src/server.rs')
-rw-r--r-- | rust/fatcat-api/src/server.rs | 60 |
1 files changed, 59 insertions, 1 deletions
diff --git a/rust/fatcat-api/src/server.rs b/rust/fatcat-api/src/server.rs index 2716dfc5..7eb73122 100644 --- a/rust/fatcat-api/src/server.rs +++ b/rust/fatcat-api/src/server.rs @@ -38,7 +38,7 @@ use swagger::{ApiError, Context, XSpanId}; use models; use {AcceptEditgroupResponse, Api, CreateContainerBatchResponse, CreateContainerResponse, CreateCreatorBatchResponse, CreateCreatorResponse, CreateEditgroupResponse, CreateFileBatchResponse, CreateFileResponse, CreateReleaseBatchResponse, CreateReleaseResponse, CreateWorkBatchResponse, CreateWorkResponse, GetContainerResponse, GetCreatorReleasesResponse, GetCreatorResponse, - GetEditgroupResponse, GetEditorChangelogResponse, GetEditorResponse, GetFileResponse, GetReleaseFilesResponse, GetReleaseResponse, GetWorkReleasesResponse, GetWorkResponse, + GetEditgroupResponse, GetEditorChangelogResponse, GetEditorResponse, GetFileResponse, GetReleaseFilesResponse, GetReleaseResponse, GetStatsResponse, GetWorkReleasesResponse, GetWorkResponse, LookupContainerResponse, LookupCreatorResponse, LookupFileResponse, LookupReleaseResponse}; header! { (Warning, "Warning") => [String] } @@ -2101,6 +2101,64 @@ where let api_clone = api.clone(); router.get( + "/v0/stats", + move |req: &mut Request| { + let mut context = Context::default(); + + // Helper function to provide a code block to use `?` in (to be replaced by the `catch` block when it exists). + fn handle_request<T>(req: &mut Request, api: &T, context: &mut Context) -> Result<Response, Response> + where + T: Api, + { + context.x_span_id = Some(req.headers.get::<XSpanId>().map(XSpanId::to_string).unwrap_or_else(|| self::uuid::Uuid::new_v4().to_string())); + context.auth_data = req.extensions.remove::<AuthData>(); + context.authorization = req.extensions.remove::<Authorization>(); + + // Query parameters (note that non-required or collection query parameters will ignore garbage values, rather than causing a 400 response) + let query_params = req.get::<UrlEncodedQuery>().unwrap_or_default(); + let param_more = query_params.get("more").and_then(|list| list.first()).and_then(|x| x.parse::<String>().ok()); + + match api.get_stats(param_more, context).wait() { + Ok(rsp) => match rsp { + GetStatsResponse::Success(body) => { + let body_string = serde_json::to_string(&body).expect("impossible to fail to serialize"); + + let mut response = Response::with((status::Status::from_u16(200), body_string)); + response.headers.set(ContentType(mimetypes::responses::GET_STATS_SUCCESS.clone())); + + context.x_span_id.as_ref().map(|header| response.headers.set(XSpanId(header.clone()))); + + Ok(response) + } + GetStatsResponse::GenericError(body) => { + let body_string = serde_json::to_string(&body).expect("impossible to fail to serialize"); + + let mut response = Response::with((status::Status::from_u16(0), body_string)); + response.headers.set(ContentType(mimetypes::responses::GET_STATS_GENERIC_ERROR.clone())); + + context.x_span_id.as_ref().map(|header| response.headers.set(XSpanId(header.clone()))); + + Ok(response) + } + }, + Err(_) => { + // Application code returned an error. This should not happen, as the implementation should + // return a valid response. + Err(Response::with((status::InternalServerError, "An internal error occurred".to_string()))) + } + } + } + + handle_request(req, &api_clone, &mut context).or_else(|mut response| { + context.x_span_id.as_ref().map(|header| response.headers.set(XSpanId(header.clone()))); + Ok(response) + }) + }, + "GetStats", + ); + + let api_clone = api.clone(); + router.get( "/v0/work/:id", move |req: &mut Request| { let mut context = Context::default(); |