diff options
-rw-r--r-- | rust/src/api_server.rs | 94 | ||||
-rw-r--r-- | rust/src/bin/fatcatd.rs | 18 | ||||
-rw-r--r-- | rust/tests/api_server.rs | 20 |
3 files changed, 95 insertions, 37 deletions
diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index 679f2ad8..7df67cee 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -298,7 +298,7 @@ impl Server { issue: rev.issue, container_id: None, // TODO work_id: rev.work_ident_id.to_string(), - state: None, // TODO: + state: None, // TODO: ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -333,7 +333,7 @@ impl Server { issue: rev.issue, container_id: None, // TODO work_id: rev.work_ident_id.to_string(), - state: None, // TODO: + state: None, // TODO: ident: Some(ident.id.to_string()), revision: ident.rev_id.map(|v| v as isize), redirect: ident.redirect_id.map(|u| u.to_string()), @@ -390,21 +390,64 @@ impl Server { .collect(); Ok(Some(entries)) } - } impl Api for Server { - wrap_get_id_handler!(container_id_get, container_id_get_handler, ContainerIdGetResponse, String); - wrap_get_id_handler!(creator_id_get, creator_id_get_handler, CreatorIdGetResponse, String); + wrap_get_id_handler!( + container_id_get, + container_id_get_handler, + ContainerIdGetResponse, + String + ); + wrap_get_id_handler!( + creator_id_get, + creator_id_get_handler, + CreatorIdGetResponse, + String + ); wrap_get_id_handler!(file_id_get, file_id_get_handler, FileIdGetResponse, String); wrap_get_id_handler!(work_id_get, work_id_get_handler, WorkIdGetResponse, String); - wrap_get_id_handler!(release_id_get, release_id_get_handler, ReleaseIdGetResponse, String); - wrap_get_id_handler!(editgroup_id_get, editgroup_id_get_handler, EditgroupIdGetResponse, i32); - - wrap_lookup_handler!(container_lookup_get, container_lookup_get_handler, ContainerLookupGetResponse, issn, String); - wrap_lookup_handler!(creator_lookup_get, creator_lookup_get_handler, CreatorLookupGetResponse, orcid, String); - wrap_lookup_handler!(file_lookup_get, file_lookup_get_handler, FileLookupGetResponse, sha1, String); - wrap_lookup_handler!(release_lookup_get, release_lookup_get_handler, ReleaseLookupGetResponse, doi, String); + wrap_get_id_handler!( + release_id_get, + release_id_get_handler, + ReleaseIdGetResponse, + String + ); + wrap_get_id_handler!( + editgroup_id_get, + editgroup_id_get_handler, + EditgroupIdGetResponse, + i32 + ); + + wrap_lookup_handler!( + container_lookup_get, + container_lookup_get_handler, + ContainerLookupGetResponse, + issn, + String + ); + wrap_lookup_handler!( + creator_lookup_get, + creator_lookup_get_handler, + CreatorLookupGetResponse, + orcid, + String + ); + wrap_lookup_handler!( + file_lookup_get, + file_lookup_get_handler, + FileLookupGetResponse, + sha1, + String + ); + wrap_lookup_handler!( + release_lookup_get, + release_lookup_get_handler, + ReleaseLookupGetResponse, + doi, + String + ); fn container_post( &self, @@ -503,7 +546,9 @@ impl Api for Server { INSERT INTO file_edit (editgroup_id, ident_id, rev_id) VALUES ($4, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev)) RETURNING *", - ).bind::<diesel::sql_types::Nullable<diesel::sql_types::Int4>, _>(body.size.map(|v| v as i32)) + ).bind::<diesel::sql_types::Nullable<diesel::sql_types::Int4>, _>( + body.size.map(|v| v as i32), + ) .bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.sha1) .bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.url) .bind::<diesel::sql_types::BigInt, _>(editgroup_id) @@ -532,8 +577,9 @@ impl Api for Server { let editgroup_id = 1; let conn = self.db_pool.get().expect("db_pool error"); - let edit: WorkEditRow = diesel::sql_query( - "WITH rev AS ( INSERT INTO work_rev (work_type) + let edit: WorkEditRow = + diesel::sql_query( + "WITH rev AS ( INSERT INTO work_rev (work_type) VALUES ($1) RETURNING id ), ident AS ( INSERT INTO work_ident (rev_id) @@ -542,10 +588,10 @@ impl Api for Server { INSERT INTO work_edit (editgroup_id, ident_id, rev_id) VALUES ($2, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev)) RETURNING *", - ).bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.work_type) - .bind::<diesel::sql_types::BigInt, _>(editgroup_id) - .get_result(&conn) - .unwrap(); + ).bind::<diesel::sql_types::Nullable<diesel::sql_types::Text>, _>(body.work_type) + .bind::<diesel::sql_types::BigInt, _>(editgroup_id) + .get_result(&conn) + .unwrap(); let edit = &edit; let entity_edit = EntityEdit { @@ -634,8 +680,8 @@ impl Api for Server { let row: EditgroupRow = insert_into(editgroup::table) .values(( editgroup::editor_id.eq(body.editor_id as i64), - editgroup::description.eq(body.description) - )) + editgroup::description.eq(body.description), + )) .get_result(&conn) .expect("error creating edit group"); @@ -644,9 +690,9 @@ impl Api for Server { editor_id: row.editor_id as isize, description: row.description, }; - Box::new(futures::done(Ok(EditgroupPostResponse::SuccessfullyCreated( - new_eg, - )))) + Box::new(futures::done(Ok( + EditgroupPostResponse::SuccessfullyCreated(new_eg), + ))) } fn editor_username_changelog_get( diff --git a/rust/src/bin/fatcatd.rs b/rust/src/bin/fatcatd.rs index d4bc2683..53df7f71 100644 --- a/rust/src/bin/fatcatd.rs +++ b/rust/src/bin/fatcatd.rs @@ -17,8 +17,8 @@ extern crate slog_async; extern crate slog_term; use clap::{App, Arg}; -use iron::{Chain, Iron, IronResult, Response, Request, status, Url}; use iron::modifiers::RedirectRaw; +use iron::{status, Chain, Iron, IronResult, Request, Response, Url}; use iron_slog::{DefaultLogFormatter, LoggerMiddleware}; use slog::{Drain, Logger}; //use dotenv::dotenv; @@ -51,14 +51,24 @@ fn main() { fn root_handler(_: &mut Request) -> IronResult<Response> { //Ok(Response::with((status::Found, Redirect(Url::parse("/swagger-ui").unwrap())))) - Ok(Response::with((status::Found, RedirectRaw("/swagger-ui".to_string())))) + Ok(Response::with(( + status::Found, + RedirectRaw("/swagger-ui".to_string()), + ))) } fn swaggerui_handler(_: &mut Request) -> IronResult<Response> { let html_type = "text/html".parse::<iron::mime::Mime>().unwrap(); - Ok(Response::with((html_type, status::Ok, include_str!("../../swagger-ui/index.html")))) + Ok(Response::with(( + html_type, + status::Ok, + include_str!("../../swagger-ui/index.html"), + ))) } fn yaml_handler(_: &mut Request) -> IronResult<Response> { - Ok(Response::with((status::Ok, include_str!("../../fatcat-openapi2.yml")))) + Ok(Response::with(( + status::Ok, + include_str!("../../fatcat-openapi2.yml"), + ))) } let mut chain = Chain::new(LoggerMiddleware::new(router, logger, formatter)); diff --git a/rust/tests/api_server.rs b/rust/tests/api_server.rs index a2d58740..84cd4aaf 100644 --- a/rust/tests/api_server.rs +++ b/rust/tests/api_server.rs @@ -1,27 +1,29 @@ - -extern crate iron; -extern crate iron_test; extern crate fatcat; extern crate fatcat_api; +extern crate iron; +extern crate iron_test; +use iron::{status, Headers}; use iron_test::{request, response}; -use iron::{Headers, status}; #[test] fn test_basics() { - let server = fatcat::server().unwrap(); let router = fatcat_api::router(server); - let response = request::get("http://localhost:9411/v0/creator/f1f046a3-45c9-4b99-adce-000000000001", + let response = request::get( + "http://localhost:9411/v0/creator/f1f046a3-45c9-4b99-adce-000000000001", Headers::new(), - &router).unwrap(); + &router, + ).unwrap(); assert_eq!(response.status, Some(status::Ok)); let body = response::extract_body_to_string(response); assert!(body.contains("Grace Hopper")); - let response = request::get("http://localhost:9411/v0/creator/f1f046a3-45c9-4b99-adce-999999999999", + let response = request::get( + "http://localhost:9411/v0/creator/f1f046a3-45c9-4b99-adce-999999999999", Headers::new(), - &router).unwrap(); + &router, + ).unwrap(); assert_eq!(response.status, Some(status::NotFound)); } |