Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requestor API routes #86

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions fpx/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::data::Store;
use crate::events::ServerEvents;
use crate::inspector::InspectorService;
use axum::extract::FromRef;
use axum::routing::{any, get, post};
use axum::routing::{any, get};
use http::StatusCode;
use url::Url;

Expand Down Expand Up @@ -67,11 +67,14 @@ fn api_router(
inspector_service,
};
axum::Router::new()
.route(
"/requests",
get(handlers::requests_list_handler).post(handlers::requests_post_handler),
)
.route(
"/requests/:id",
get(handlers::request_get_handler).delete(handlers::request_delete_handler),
get(handlers::requests_get_handler).delete(handlers::request_delete_handler),
)
.route("/requestor", post(handlers::execute_requestor))
.route(
"/inspectors",
get(handlers::inspector_list_handler).post(handlers::inspector_create_handler),
Expand Down
36 changes: 31 additions & 5 deletions fpx/src/api/client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! API client for the FPX API.

use crate::models::Request;
use crate::models;

use super::errors::ApiClientError;
use super::handlers::RequestGetError;
use super::handlers::{RequestGetError, RequestListError};
use anyhow::Result;
use http::Method;
use tracing::trace;
Expand Down Expand Up @@ -34,14 +34,19 @@ impl ApiClient {
&self,
method: Method,
path: impl AsRef<str>,
body: Option<String>,
) -> Result<T, ApiClientError<E>>
where
T: serde::de::DeserializeOwned,
E: serde::de::DeserializeOwned,
{
let u = self.base_url.join(path.as_ref())?;

let req = self.client.request(method, u);
let req = self
.client
.request(method, u)
.header("Content-Type", "application/json")
.body(body.unwrap_or_default());

// Make request
let response = req.send().await?;
Expand Down Expand Up @@ -71,9 +76,30 @@ impl ApiClient {
pub async fn request_get(
&self,
request_id: i64,
) -> Result<Request, ApiClientError<RequestGetError>> {
) -> Result<models::Request, ApiClientError<RequestGetError>> {
let path = format!("api/requests/{}", request_id);

self.do_req(Method::GET, path).await
self.do_req(Method::GET, path, None).await
}

/// Retrieve a list of requests
pub async fn request_list(
&self,
) -> Result<Vec<models::RequestSummary>, ApiClientError<RequestListError>> {
let path = "api/requests";

self.do_req(Method::GET, path, None).await
}

/// Create and execute a new request
pub async fn request_post(
&self,
new_request: models::NewRequest,
) -> Result<models::Response, ApiClientError<models::NewRequestError>> {
let path = "/api/requests";

let json = serde_json::to_string(&new_request).unwrap();

self.do_req(Method::POST, path, Some(json)).await
}
}
2 changes: 0 additions & 2 deletions fpx/src/api/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ use axum::response::IntoResponse;

mod inspect;
mod inspectors;
mod requestor;
mod requests;

// Re-export the all the handlers from different modules
pub use inspect::*;
pub use inspectors::*;
pub use requestor::*;
pub use requests::*;

#[tracing::instrument(skip_all)]
Expand Down
9 changes: 5 additions & 4 deletions fpx/src/api/handlers/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,24 @@ pub async fn inspect_request_handler(
)
})
.collect();
let request_id: u32 = Store::request_create(

let request = Store::request_create(
&tx,
parts.method.as_ref(),
&parts.uri.to_string(),
&String::from_utf8(body.to_vec()).unwrap(),
Some(String::from_utf8(body.to_vec()).unwrap().as_str()),
headers,
)
.await
.unwrap(); // TODO

tx.commit().await.unwrap(); // TODO

events.broadcast(models::RequestAdded::new(request_id, None).into());
events.broadcast(models::RequestAdded::new(request.id, None).into());

// TODO: This should return the same payload as the GET /requests/{id} endpoint
base_url
.join(&format!("api/requests/{}", request_id))
.join(&format!("api/requests/{}", request.id))
.unwrap()
.to_string()
}
95 changes: 0 additions & 95 deletions fpx/src/api/handlers/requestor.rs

This file was deleted.

Loading
Loading