Skip to content

created the verify_custom_api endpoint #270

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

Merged
merged 1 commit into from
Sep 28, 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
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