Skip to content

Commit

Permalink
fix: move env var to config & add static handler
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed May 14, 2024
1 parent 08253db commit b5907ae
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
3 changes: 3 additions & 0 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
3 changes: 1 addition & 2 deletions src/endpoints/crosschain/ethereum/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub async fn handler(State(state): State<Arc<AppState>>, 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,
Expand Down Expand Up @@ -215,7 +215,6 @@ pub async fn handler(State(state): State<Arc<AppState>>, query: Query) -> impl I
}
}
}

}
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/endpoints/crosschain/ethereum/text_records.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,6 +15,7 @@ use crate::config::{Config, EvmRecordVerifier};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum HandlerType {
Static,
GetDiscordName,
GetGithubName,
GetTwitterName,
Expand All @@ -33,15 +34,16 @@ struct DiscordUser {
impl EvmRecordVerifier {
pub async fn execute_handler(&self, config: &Config, id: FieldElement) -> Result<String> {
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<String> {
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)
Expand All @@ -58,9 +60,9 @@ impl EvmRecordVerifier {

Ok(resp.username)
}
async fn get_github_name(&self, id: FieldElement) -> Result<String> {
async fn get_github_name(&self, config: &Config, id: FieldElement) -> Result<String> {
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()
Expand Down Expand Up @@ -88,7 +90,11 @@ impl EvmRecordVerifier {
async fn get_twitter_name(&self, config: &Config, id: FieldElement) -> Result<String> {
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)])
Expand Down

0 comments on commit b5907ae

Please sign in to comment.