Skip to content

Commit

Permalink
created the verify_custom_api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Dprof-in-tech committed Sep 27, 2024
1 parent 4063ddf commit e64f510
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/endpoints/admin/balance/create_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ pub async fn handler(
discord_guild_id: None,
quiz_name: None,
contracts: Some(parsed_contracts),
api_url: None,
regex: None,
};

// insert document to boost collection
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints/admin/custom/create_custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub async fn handler(
discord_guild_id: None,
quiz_name: None,
contracts: None,
api_url: None,
regex: None,
};

// insert document to boost collection
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints/admin/discord/create_discord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub async fn handler(
quiz_name: None,
verify_redirect: None,
contracts: None,
api_url: None,
regex: None,
};

// insert document to boost collection
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints/admin/domain/create_domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ pub async fn handler(
quiz_name: None,
verify_redirect: None,
contracts: None,
api_url: None,
regex: None,
};

// insert document to boost collection
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints/admin/quiz/create_quiz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub async fn handler(
discord_guild_id: None,
verify_redirect: None,
contracts: None,
api_url: None,
regex: None,
};

return match tasks_collection.insert_one(new_document, None).await {
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints/admin/twitter/create_twitter_fw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub async fn handler(
discord_guild_id: None,
quiz_name: None,
contracts: None,
api_url: None,
regex: None,
};

// insert document to boost collection
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints/admin/twitter/create_twitter_rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub async fn handler(
discord_guild_id: None,
quiz_name: None,
contracts: None,
api_url: None,
regex: None,
};

// insert document to boost collection
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/quests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod starknet;
pub mod starknetid;
pub mod uri;
pub mod verify_balance;
pub mod verify_custom_api;
pub mod verify_quiz;
pub mod verify_twitter_fw;
pub mod verify_twitter_rw;
78 changes: 78 additions & 0 deletions src/endpoints/quests/verify_custom_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::{sync::Arc, str::FromStr};
use crate::{
models::{AppState, QuestTaskDocument},
utils::{get_error, CompletedTasksTrait},
};
use axum::{
extract::{Query, State},
http::StatusCode,
response::IntoResponse,
Json,
};
use axum_auto_routes::route;
use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use serde_json::json;
use regex::Regex;
use reqwest::get;
use starknet::core::types::FieldElement;

#[derive(Debug, Serialize, Deserialize, Default)]
pub struct VerifyCustomApiQuery {
pub addr: String,
pub task_id: u32,
}

#[route(get, "/quests/verify_custom_api")]
pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<VerifyCustomApiQuery>,
) -> impl IntoResponse {
let task_id = query.task_id;

// Get task in db
let task_collection = state.db.collection("tasks");
let task: QuestTaskDocument = task_collection
.find_one(doc! {"id": task_id}, None)
.await
.unwrap()
.unwrap();

// Check if the task type is "custom_api"
if task.task_type != Some("custom_api".to_string()) {
return get_error("Invalid task type.".to_string());
}

// Check if the task has the required fields (api_url and regex)
let api_url = match &task.api_url {
Some(url) => url,
None => return get_error("API URL not found.".to_string()),
};

let regex_str = match &task.regex {
Some(rgx) => rgx,
None => return get_error("Regex not found.".to_string()),
};

// Call the specified API
let response = get(api_url).await;

match response {
Ok(res) => {
let res_text = res.text().await.unwrap();

// Check response against the regex
let re = Regex::new(regex_str).unwrap();
if re.is_match(&res_text) {
// Mark the task as completed
match state.upsert_completed_task(FieldElement::from_str(&query.addr).unwrap(), task_id).await {
Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(),
Err(e) => get_error(format!("{}", e)),
}
} else {
get_error("Response did not match the required pattern.".to_string())
}
}
Err(e) => get_error(format!("Failed to fetch API: {}", e)),
}
}
2 changes: 2 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ pub struct QuestTaskDocument {
pub verify_endpoint: String,
pub href: String,
pub verify_endpoint_type: String,
pub api_url: Option<String>,
pub regex: Option<String>,
#[serde(default)]
pub verify_redirect: Option<String>,
#[serde(default)]
Expand Down

0 comments on commit e64f510

Please sign in to comment.