Skip to content

Commit

Permalink
Use the guide-scoper service instead of the consensus apis.
Browse files Browse the repository at this point in the history
  • Loading branch information
yjcyxky committed Mar 17, 2024
1 parent 44541a4 commit d45427a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/api/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use poem_openapi::{types::ToJSON, Object};
use reqwest;
use serde::{Deserialize, Serialize};

const CONSENSUS_API: &str = "https://consensus.app/api/paper_search/";
const CONSENSUS_DETAIL_API: &str = "https://consensus.app/api/papers/details/";

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Object)]
pub struct PublicationRecords {
pub records: Vec<Publication>,
Expand Down Expand Up @@ -43,14 +40,21 @@ impl Publication {
pub async fn fetch_publication(
id: &str,
) -> Result<PublicationDetail, anyhow::Error> {
let api_token = match std::env::var("CONSENSUS_API_TOKEN") {
let api_token = match std::env::var("GUIDESCOPER_API_TOKEN") {
Ok(token) => token,
Err(_) => {
return Err(anyhow::anyhow!("GUIDESCOPER_API_TOKEN not found"));
}
};

let detail_api = match std::env::var("GUIDESCOPER_DETAIL_API") {
Ok(token) => token,
Err(_) => {
return Err(anyhow::anyhow!("CONSENSUS_API_TOKEN not found"));
return Err(anyhow::anyhow!("GUIDESCOPER_DETAIL_API not found"));
}
};

let url = format!("{}{}", CONSENSUS_DETAIL_API, id);
let url = format!("{}{}", detail_api, id);
let cookie = format!("_session={}", api_token);
let client = reqwest::Client::new();
let res = client.get(&url).header("Cookie", cookie).send().await?;
Expand Down Expand Up @@ -96,10 +100,17 @@ impl Publication {
page: Option<u64>,
page_size: Option<u64>,
) -> Result<PublicationRecords, anyhow::Error> {
let api_token = match std::env::var("CONSENSUS_API_TOKEN") {
let api_token = match std::env::var("GUIDESCOPER_API_TOKEN") {
Ok(token) => token,
Err(_) => {
return Err(anyhow::anyhow!("GUIDESCOPER_API_TOKEN not found"));
}
};

let guidescoper_api = match std::env::var("GUIDESCOPER_API") {
Ok(token) => token,
Err(_) => {
return Err(anyhow::anyhow!("CONSENSUS_API_TOKEN not found"));
return Err(anyhow::anyhow!("GUIDESCOPER_API not found"));
}
};

Expand All @@ -111,7 +122,7 @@ impl Publication {
let mut records = Vec::new();
let url = format!(
"{}?query={}&page={}&size={}",
CONSENSUS_API, query_str, page, page_size
guidescoper_api, query_str, page, page_size
);
let cookie = format!("_session={}", api_token);
let client = reqwest::Client::new();
Expand Down
10 changes: 10 additions & 0 deletions src/api/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ impl BiomedgpsApi {
let page = page.0;
let page_size = page_size.0;

info!("Fetch publications with query: {}", query_str);
let pairs = query_str.split("#").collect::<Vec<&str>>();
let query_str = if pairs.len() != 2 {
let err = format!("Invalid query string: {}", query_str);
warn!("{}", err);
return GetPublicationsResponse::bad_request(err);
} else {
format!("what's the relation between {} and {}?", pairs[0], pairs[1])
};

match Publication::fetch_publications(&query_str, page, page_size).await {
Ok(records) => GetPublicationsResponse::ok(records),
Err(e) => {
Expand Down

0 comments on commit d45427a

Please sign in to comment.