Radish alpha
r
Radicle web interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
Add `GET /api/v1/sessions/:id` route to get session information
Sebastian Martinez committed 3 years ago
commit 585dcf4afe72889d45ada613a644c8061278dd16
parent d8da80821b1696c6872cb4bec09ea61c1a2f4eab
2 files changed +22 -2
modified radicle-httpd/src/api/auth.rs
@@ -11,6 +11,8 @@ impl Serialize for DateTime {
    }
}

+
#[derive(Clone, Serialize)]
+
#[serde(tag = "type", rename_all = "camelCase")]
pub enum AuthState {
    Authorized(Session),
    Unauthorized {
modified radicle-httpd/src/api/v1/sessions.rs
@@ -20,7 +20,10 @@ pub const AUTHORIZED_SESSIONS_EXPIRATION: Duration = Duration::weeks(1);
pub fn router(ctx: Context) -> Router {
    Router::new()
        .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)
}

@@ -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.
/// `PUT /sessions/:id`
async fn session_signin_handler(
@@ -100,7 +115,7 @@ mod routes {
    use axum::http::StatusCode;
    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]
    async fn test_session() {
@@ -131,5 +146,8 @@ mod routes {
        .await;

        assert_eq!(response.status(), StatusCode::OK);
+

+
        let response = get(&app, format!("/sessions/{}", session_info.session_id)).await;
+
        assert_eq!(response.status(), StatusCode::OK);
    }
}