From b5907ae42ce538f8e645c2262c95eb28ec6a2843 Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 14 May 2024 13:18:34 +0200 Subject: [PATCH] fix: move env var to config & add static handler --- config.template.toml | 3 +++ src/config.rs | 3 +++ src/endpoints/crosschain/ethereum/resolve.rs | 3 +-- .../crosschain/ethereum/text_records.rs | 18 ++++++++++++------ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/config.template.toml b/config.template.toml index e3cb2b1..dc75e0c 100644 --- a/config.template.toml +++ b/config.template.toml @@ -23,7 +23,10 @@ rpc_url = "xxxxxx" refresh_delay = 60 # in seconds ipfs_gateway = "https://gateway.pinata.cloud/ipfs/" # or https://ipfs.io/ipfs/ discord_token = "xxxxxx" +discord_api_url = "https://discord.com/api" twitter_api_key = "xxxxxx" +twitter_api_url = "xxxxxx" +github_api_url = "https://api.github.com" [starkscan] api_url = "https://api-testnet.starkscan.co/api/v0" diff --git a/src/config.rs b/src/config.rs index 378b83e..2a8918a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -76,7 +76,10 @@ pub_struct!(Clone, Debug, Deserialize; Variables { refresh_delay: f64, ipfs_gateway: String, discord_token: String, + discord_api_url: String, twitter_api_key: String, + twitter_api_url: String, + github_api_url: String, }); #[derive(Deserialize)] diff --git a/src/endpoints/crosschain/ethereum/resolve.rs b/src/endpoints/crosschain/ethereum/resolve.rs index c402222..363ec41 100644 --- a/src/endpoints/crosschain/ethereum/resolve.rs +++ b/src/endpoints/crosschain/ethereum/resolve.rs @@ -174,7 +174,7 @@ pub async fn handler(State(state): State>, query: Query) -> impl I // we check if this data was added through a verifier // let record_config = state.conf.evm_records_verifiers.get(&record).unwrap(); match state.conf.evm_records_verifiers.get(&record) { - Some(record_config) => { + Some(record_config) => { let record_data = get_verifier_data( &state.conf, &provider, @@ -215,7 +215,6 @@ pub async fn handler(State(state): State>, query: Query) -> impl I } } } - } } } diff --git a/src/endpoints/crosschain/ethereum/text_records.rs b/src/endpoints/crosschain/ethereum/text_records.rs index 160c501..b146069 100644 --- a/src/endpoints/crosschain/ethereum/text_records.rs +++ b/src/endpoints/crosschain/ethereum/text_records.rs @@ -1,4 +1,4 @@ -use anyhow::{Context, Result, anyhow}; +use anyhow::{anyhow, Context, Result}; use reqwest::{Client, StatusCode}; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -15,6 +15,7 @@ use crate::config::{Config, EvmRecordVerifier}; #[derive(Serialize, Deserialize, Debug, Clone)] pub enum HandlerType { + Static, GetDiscordName, GetGithubName, GetTwitterName, @@ -33,15 +34,16 @@ struct DiscordUser { impl EvmRecordVerifier { pub async fn execute_handler(&self, config: &Config, id: FieldElement) -> Result { match self.handler { + HandlerType::Static => Ok(FieldElement::to_string(&id)), HandlerType::GetDiscordName => self.get_discord_name(config, id).await, - HandlerType::GetGithubName => self.get_github_name(id).await, + HandlerType::GetGithubName => self.get_github_name(config, id).await, HandlerType::GetTwitterName => self.get_twitter_name(config, id).await, } } async fn get_discord_name(&self, config: &Config, id: FieldElement) -> Result { let social_id = FieldElement::to_string(&id); - let url = format!("https://discord.com/api/users/{}", social_id); + let url = format!("{}/users/{}", config.variables.discord_api_url, social_id); let client = Client::new(); let resp = client .get(&url) @@ -58,9 +60,9 @@ impl EvmRecordVerifier { Ok(resp.username) } - async fn get_github_name(&self, id: FieldElement) -> Result { + async fn get_github_name(&self, config: &Config, id: FieldElement) -> Result { let social_id = FieldElement::to_string(&id); - let url = format!("https://api.github.com/user/{}", social_id); + let url = format!("{}/user/{}", config.variables.github_api_url, social_id); let client = Client::builder() .user_agent("request") .build() @@ -88,7 +90,11 @@ impl EvmRecordVerifier { async fn get_twitter_name(&self, config: &Config, id: FieldElement) -> Result { let social_id = FieldElement::to_string(&id); let client = Client::new(); - let response = client.get("https://twttrapi.p.rapidapi.com/get-user-by-id") + let response = client + .get(format!( + "{}/get-user-by-id", + config.variables.twitter_api_url + )) .header("X-RapidAPI-Key", config.variables.twitter_api_key.clone()) .header("X-RapidAPI-Host", "twttrapi.p.rapidapi.com") .query(&[("user_id", &social_id)])