Skip to content

Commit

Permalink
Small api cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleragreen committed Jun 27, 2024
1 parent 50945f4 commit 1daf4b7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 39 deletions.
92 changes: 56 additions & 36 deletions example-app/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ use crate::scheduler_interface::ToScheduler;

#[derive(Clone)]
struct AppState {
feed_id: Arc<RwLock<usize>>,
next_feed_id: Arc<RwLock<usize>>,
db: Arc<RwLock<HashMap<usize, Feed>>>,
scheduler_interface: Arc<dyn ToScheduler + Send + Sync>,
}

pub fn app(scheduler_interface: Arc<dyn ToScheduler + Send + Sync>) -> Router {
let state = AppState {
feed_id: Arc::new(RwLock::new(1)),
next_feed_id: Arc::new(RwLock::new(1)),
db: Arc::new(RwLock::new(HashMap::new())),
scheduler_interface,
};
Expand All @@ -40,10 +40,16 @@ struct Status {
status: String,
}

impl Status {
fn new(status: &str) -> Self {
Self {
status: status.to_string(),
}
}
}

async fn status_handler() -> impl IntoResponse {
Json(Status {
status: "OK".to_string(),
})
Json(Status::new("OK"))
}

#[derive(Clone, Deserialize, Serialize)]
Expand All @@ -56,29 +62,31 @@ struct CreateFeed {

async fn post_handler(
state: State<AppState>,
Json(payload): Json<CreateFeed>,
Json(CreateFeed {
name,
url,
frequency,
headers,
}): Json<CreateFeed>,
) -> impl IntoResponse {
let id = *(state.feed_id.read().unwrap());
let id = *(state.next_feed_id.read().unwrap());
let feed = Feed {
id,
name: payload.name,
url: payload.url,
frequency: payload.frequency,
headers: payload.headers,
name,
url,
frequency,
headers,
};

*(state.feed_id.write().unwrap()) += 1;
*(state.next_feed_id.write().unwrap()) += 1;
state.db.write().unwrap().insert(id, feed.clone());
state.scheduler_interface.create(feed.clone());

(StatusCode::CREATED, Json(feed))
}

async fn get_handler(path: Path<String>, state: State<AppState>) -> impl IntoResponse {
let id: usize = match path.parse() {
Ok(i) => i,
Err(_) => return Err(StatusCode::BAD_REQUEST),
};
let id = path.parse::<usize>().map_err(|_| StatusCode::BAD_REQUEST)?;

match state.db.read().unwrap().get(&id).cloned() {
Some(feed) => Ok(Json(feed)),
Expand All @@ -89,22 +97,24 @@ async fn get_handler(path: Path<String>, state: State<AppState>) -> impl IntoRes
async fn put_handler(
path: Path<String>,
state: State<AppState>,
Json(payload): Json<CreateFeed>,
Json(CreateFeed {
name,
url,
frequency,
headers,
}): Json<CreateFeed>,
) -> impl IntoResponse {
let id: usize = match path.parse() {
Ok(i) => i,
Err(_) => return Err(StatusCode::BAD_REQUEST),
};
let id = path.parse::<usize>().map_err(|_| StatusCode::BAD_REQUEST)?;
if state.db.read().unwrap().get(&id).is_none() {
return Err(StatusCode::NOT_FOUND);
}

let feed = Feed {
id,
name: payload.name,
url: payload.url,
frequency: payload.frequency,
headers: payload.headers,
name,
url,
frequency,
headers,
};

state.db.write().unwrap().insert(id, feed.clone());
Expand All @@ -114,10 +124,7 @@ async fn put_handler(
}

async fn delete_handler(path: Path<String>, state: State<AppState>) -> impl IntoResponse {
let id: usize = match path.parse() {
Ok(i) => i,
Err(_) => return Err(StatusCode::BAD_REQUEST),
};
let id = path.parse::<usize>().map_err(|_| StatusCode::BAD_REQUEST)?;
let feed = match state.db.read().unwrap().get(&id).cloned() {
Some(f) => f,
None => return Err(StatusCode::NOT_FOUND),
Expand Down Expand Up @@ -195,7 +202,9 @@ mod api_tests {
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(sender.lock().unwrap().count(), 0);

let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
assert_eq!(&body[..], b"{\"status\":\"OK\"}");
}

Expand All @@ -215,7 +224,9 @@ mod api_tests {

assert_eq!(response.status(), StatusCode::BAD_REQUEST);

let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
assert_eq!(body.len(), 0);

let sender = Arc::new(Mutex::new(MockSender::new()));
Expand All @@ -232,7 +243,9 @@ mod api_tests {

assert_eq!(response.status(), StatusCode::BAD_REQUEST);

let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
assert_eq!(body.len(), 0);
}

Expand Down Expand Up @@ -305,7 +318,9 @@ mod api_tests {
assert_eq!(response.status(), StatusCode::CREATED);
assert_eq!(sender.lock().unwrap().count(), 1);

let body = axum::body::to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let f: Feed = serde_json::from_slice(&body).unwrap();

assert_eq!(f.id, 1);
Expand Down Expand Up @@ -355,7 +370,10 @@ mod api_tests {

let response = client
.post(format!("http://localhost:3000/feed"))
.header(http::header::CONTENT_TYPE.as_str(), mime::APPLICATION_JSON.as_ref())
.header(
http::header::CONTENT_TYPE.as_str(),
mime::APPLICATION_JSON.as_ref(),
)
.json(&serde_json::json!(input))
.send()
.await
Expand All @@ -377,7 +395,10 @@ mod api_tests {

let response = client
.put(format!("http://localhost:3000/feed/1"))
.header(http::header::CONTENT_TYPE.as_str(), mime::APPLICATION_JSON.as_ref())
.header(
http::header::CONTENT_TYPE.as_str(),
mime::APPLICATION_JSON.as_ref(),
)
.json(&serde_json::json!(input_new))
.send()
.await
Expand All @@ -401,7 +422,6 @@ mod api_tests {
assert_eq!(f.url, "http");
assert_eq!(f.frequency, 20);


let response = client
.delete(format!("http://localhost:3000/feed/1"))
.send()
Expand Down
4 changes: 2 additions & 2 deletions example-app/src/deps/mockito/mock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hyper::StatusCode;
use hyper::Request;
use hyper::body::Incoming;
use hyper::Request;
use hyper::StatusCode;
use rand;
use std::sync::{Arc, RwLock};

Expand Down
2 changes: 1 addition & 1 deletion example-app/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
mod tests {
use reqwest::blocking::Client;
use serde_json::json;
use tokio::net::TcpListener;
use std::net::SocketAddr;
use std::thread;
use std::time::Duration;
use tokio::net::TcpListener;
use tokio::runtime::Builder;

use gtfs_realtime_rust::api;
Expand Down

0 comments on commit 1daf4b7

Please sign in to comment.