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

refactor: support json response for mediator base path #1168

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
33 changes: 26 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,23 @@ 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(
"<p>Please refer to the API section of <a>readme</a> for usage. Thanks. </p>"
.to_string(),
)
.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(())
}
Loading