Skip to content

Commit

Permalink
refactor: support json response for mediator base path
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Prazak <ondrej.prazak@absa.africa>
  • Loading branch information
Ondrej Prazak committed Mar 26, 2024
1 parent 05c92f2 commit d95c346
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
32 changes: 25 additions & 7 deletions aries/agents/mediator/src/http_routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use axum::{
routing::get,
Json, Router,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{
Expand All @@ -22,11 +23,7 @@ pub async fn oob_invite_qr(
State(agent): State<ArcAgent<impl BaseWallet, impl MediatorPersistence>>,
) -> Response {
let Json(oob_json) = oob_invite_json(State(agent)).await;
let preferred_mimetype = headers
.get(ACCEPT)
.map(|s| s.to_str().unwrap_or_default())
.unwrap_or_default();
match preferred_mimetype {
match detect_mime_type(&headers) {
"application/json" => Json(oob_json).into_response(),
_ => {
let oob_string = serde_json::to_string_pretty(&oob_json).unwrap();
Expand All @@ -47,6 +44,13 @@ pub async fn oob_invite_qr(
}
}

fn detect_mime_type(headers: &HeaderMap) -> &str {
headers
.get(ACCEPT)
.map(|s| s.to_str().unwrap_or_default())
.unwrap_or_default()
}

pub async fn oob_invite_json(
State(agent): State<ArcAgent<impl BaseWallet, impl MediatorPersistence>>,
) -> Json<Value> {
Expand All @@ -61,8 +65,22 @@ pub async fn handle_didcomm(
didcomm_handlers::handle_aries(State(agent), didcomm_msg).await
}

pub async fn readme() -> Html<String> {
Html("<p>Please refer to the API section of <a>readme</a> for usage. Thanks. </p>".into())
#[derive(Serialize, Deserialize)]
pub struct ReadmeInfo {
message: String,
}

pub async fn readme(headers: HeaderMap) -> Response {
match detect_mime_type(&headers) {
"application/json" => Json(ReadmeInfo {
message: "Please refer to the API section of a readme for usage. Thanks.".into(),
})
.into_response(),
_ => Html(format!(
"<p>Please refer to the API section of <a>readme</a> for usage. Thanks. </p>"
))
.into_response(),
}
}

pub async fn build_router(
Expand Down
32 changes: 32 additions & 0 deletions aries/agents/mediator/tests/mediator-readme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
mod common;

use anyhow::Result;
use log::info;
use mediator::http_routes::ReadmeInfo;
use reqwest::header::ACCEPT;
use url::Url;

use crate::common::test_setup::setup_env_logging;

static LOGGING_INIT: std::sync::Once = std::sync::Once::new();

const ENDPOINT_ROOT: &str = "http://localhost:8005";

#[test]
fn base_path_returns_readme() -> Result<()> {
LOGGING_INIT.call_once(setup_env_logging);

let client = reqwest::blocking::Client::new();
let endpoint: Url = ENDPOINT_ROOT.parse().unwrap();

let res = client
.get(endpoint)
.header(ACCEPT, "application/json")
.send()?
.error_for_status()?;
info!("{:?}", res);

let _: ReadmeInfo = res.json()?;

Ok(())
}

0 comments on commit d95c346

Please sign in to comment.