Add `GET /api/v1/sessions/:id` route to get session information

Signed-off-by: Sebastian Martinez <me@sebastinez.dev>
This commit is contained in:
Sebastian Martinez 2023-02-02 10:06:34 -03:00 committed by Alexis Sellier
parent 3f48c2c516
commit 34064ef8b5
No known key found for this signature in database
2 changed files with 22 additions and 2 deletions

View File

@ -11,6 +11,8 @@ impl Serialize for DateTime {
} }
} }
#[derive(Clone, Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum AuthState { pub enum AuthState {
Authorized(Session), Authorized(Session),
Unauthorized { Unauthorized {

View File

@ -20,7 +20,10 @@ pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1);
pub fn router(ctx: Context) -> Router { pub fn router(ctx: Context) -> Router {
Router::new() Router::new()
.route("/sessions", post(session_create_handler)) .route("/sessions", post(session_create_handler))
.route("/sessions/:id", put(session_signin_handler)) .route(
"/sessions/:id",
put(session_signin_handler).get(session_handler),
)
.with_state(ctx) .with_state(ctx)
} }
@ -53,6 +56,18 @@ async fn session_create_handler(State(ctx): State<Context>) -> impl IntoResponse
)) ))
} }
/// Get a session.
/// `GET /sessions/:id`
async fn session_handler(
State(ctx): State<Context>,
Path(session_id): Path<String>,
) -> impl IntoResponse {
let sessions = ctx.sessions.read().await;
let session = sessions.get(&session_id).ok_or(Error::NotFound)?.to_owned();
Ok::<_, Error>(Json(session))
}
/// Update session. /// Update session.
/// `PUT /sessions/:id` /// `PUT /sessions/:id`
async fn session_signin_handler( async fn session_signin_handler(
@ -100,7 +115,7 @@ mod routes {
use axum::http::StatusCode; use axum::http::StatusCode;
use radicle_cli::commands::rad_web::{self, SessionInfo}; use radicle_cli::commands::rad_web::{self, SessionInfo};
use crate::api::test::{self, post, put}; use crate::api::test::{self, get, post, put};
#[tokio::test] #[tokio::test]
async fn test_session() { async fn test_session() {
@ -131,5 +146,8 @@ mod routes {
.await; .await;
assert_eq!(response.status(), StatusCode::OK); assert_eq!(response.status(), StatusCode::OK);
let response = get(&app, format!("/sessions/{}", session_info.session_id)).await;
assert_eq!(response.status(), StatusCode::OK);
} }
} }