Skip to content

Commit

Permalink
aw-server: Implement swagger for some endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-bjareholt committed Aug 11, 2020
1 parent 5c68853 commit 1761ba9
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 21 deletions.
89 changes: 89 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions aw-client-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate aw_models;
extern crate gethostname;
extern crate reqwest;
#[macro_use]
extern crate aw_models;
extern crate serde_json;

use std::collections::HashMap;
Expand Down
11 changes: 8 additions & 3 deletions aw-models/src/timeinterval.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::fmt;

use serde::de::{self, Deserialize, Deserializer, Visitor};
use serde::de::Error as DeserializeError;
use serde::de::Visitor;
use serde::{Deserialize, Deserializer};

use chrono::DateTime;
use chrono::Duration;
Expand Down Expand Up @@ -72,13 +74,16 @@ impl<'de> Visitor<'de> for TimeIntervalVisitor {

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
E: DeserializeError,
{
match TimeInterval::new_from_string(&value) {
Ok(ti) => Ok(ti),
Err(e) => {
warn!("{:?}", e);
Err(de::Error::invalid_value(Unexpected::Str(value), &self))
Err(DeserializeError::invalid_value(
Unexpected::Str(value),
&self,
))
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions aw-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ toml = "0.5"
gethostname = "0.2"
uuid = { version = "0.8", features = ["serde", "v4"] }
getopts = "0.2"
rocket_okapi = "0.5"
schemars = "0.7"
okapi = { version = "0.4", features = ["derive_json_schema"] }

aw-datastore = { path = "../aw-datastore" }
aw-models = { path = "../aw-models" }
Expand Down
22 changes: 17 additions & 5 deletions aw-server/src/endpoints/bucket.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::io::Cursor;

use rocket_contrib::json::Json;

Expand All @@ -10,13 +9,13 @@ use aw_models::Bucket;
use aw_models::BucketsExport;
use aw_models::Event;

use rocket::http::Header;
use rocket::http::Status;
use rocket::response::Response;
use rocket::State;
use rocket_okapi::openapi;

use crate::endpoints::{HttpErrorJson, ServerState};

#[openapi]
#[get("/")]
pub fn buckets_get(
state: State<ServerState>,
Expand All @@ -28,6 +27,7 @@ pub fn buckets_get(
}
}

#[openapi]
#[get("/<bucket_id>")]
pub fn bucket_get(
bucket_id: String,
Expand All @@ -40,6 +40,7 @@ pub fn bucket_get(
}
}

#[openapi]
#[post("/<bucket_id>", data = "<message>", format = "application/json")]
pub fn bucket_new(
bucket_id: String,
Expand All @@ -58,6 +59,7 @@ pub fn bucket_new(
}
}

#[openapi]
#[get("/<bucket_id>/events?<start>&<end>&<limit>")]
pub fn bucket_events_get(
bucket_id: String,
Expand Down Expand Up @@ -102,6 +104,7 @@ pub fn bucket_events_get(
}
}

#[openapi]
#[post("/<bucket_id>/events", data = "<events>", format = "application/json")]
pub fn bucket_events_create(
bucket_id: String,
Expand All @@ -116,6 +119,7 @@ pub fn bucket_events_create(
}
}

#[openapi]
#[post(
"/<bucket_id>/heartbeat?<pulsetime>",
data = "<heartbeat_json>",
Expand All @@ -135,6 +139,7 @@ pub fn bucket_events_heartbeat(
}
}

#[openapi]
#[get("/<bucket_id>/events/count")]
pub fn bucket_event_count(
bucket_id: String,
Expand All @@ -148,6 +153,7 @@ pub fn bucket_event_count(
}
}

#[openapi]
#[delete("/<bucket_id>/events/<event_id>")]
pub fn bucket_events_delete_by_id(
bucket_id: String,
Expand All @@ -161,11 +167,12 @@ pub fn bucket_events_delete_by_id(
}
}

#[openapi]
#[get("/<bucket_id>/export")]
pub fn bucket_export(
bucket_id: String,
state: State<ServerState>,
) -> Result<Response, HttpErrorJson> {
) -> Result<Json<BucketsExport>, HttpErrorJson> {
let datastore = endpoints_get_lock!(state.datastore);
let mut export = BucketsExport {
buckets: HashMap::new(),
Expand All @@ -180,8 +187,10 @@ pub fn bucket_export(
.expect("Failed to get events for bucket"),
);
export.buckets.insert(bucket_id.clone(), bucket);
let filename = format!("aw-bucket-export_{}.json", bucket_id);

// TODO: Add back Content-Disposition
/*
let filename = format!("aw-bucket-export_{}.json", bucket_id);
let header_content = format!("attachment; filename={}", filename);
Ok(Response::build()
.status(Status::Ok)
Expand All @@ -190,8 +199,11 @@ pub fn bucket_export(
serde_json::to_string(&export).expect("Failed to serialize"),
))
.finalize())
*/
Ok(Json(export))
}

#[openapi]
#[delete("/<bucket_id>")]
pub fn bucket_delete(bucket_id: String, state: State<ServerState>) -> Result<(), HttpErrorJson> {
let datastore = endpoints_get_lock!(state.datastore);
Expand Down
11 changes: 7 additions & 4 deletions aw-server/src/endpoints/export.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::collections::HashMap;
use std::io::Cursor;

use rocket::http::Header;
use rocket::http::Status;
use rocket::response::Response;
use rocket::State;
use rocket_contrib::json::Json;
use rocket_okapi::openapi;

use aw_models::BucketsExport;

use crate::endpoints::{HttpErrorJson, ServerState};

#[openapi]
#[get("/")]
pub fn buckets_export(state: State<ServerState>) -> Result<Response, HttpErrorJson> {
pub fn buckets_export(state: State<ServerState>) -> Result<Json<BucketsExport>, HttpErrorJson> {
let datastore = endpoints_get_lock!(state.datastore);
let mut export = BucketsExport {
buckets: HashMap::new(),
Expand All @@ -28,6 +28,7 @@ pub fn buckets_export(state: State<ServerState>) -> Result<Response, HttpErrorJs
export.buckets.insert(bid, bucket);
}

/*
Ok(Response::build()
.status(Status::Ok)
.header(Header::new(
Expand All @@ -38,4 +39,6 @@ pub fn buckets_export(state: State<ServerState>) -> Result<Response, HttpErrorJs
serde_json::to_string(&export).expect("Failed to serialize"),
))
.finalize())
*/
Ok(Json(export))
}
3 changes: 1 addition & 2 deletions aw-server/src/endpoints/import.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use multipart::server::Multipart;
use rocket::http::ContentType;
use rocket::http::Status;
use rocket::Data;
use rocket::State;
use rocket_contrib::json::Json;

use multipart::server::Multipart;

use std::io::Read;
use std::sync::Mutex;

Expand Down
Loading

0 comments on commit 1761ba9

Please sign in to comment.