Skip to content

Commit

Permalink
using dotenv macro
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetbout committed Sep 6, 2023
1 parent 76e5ee6 commit 1c57b72
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 47 deletions.
1 change: 0 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
BEARER_TOKEN=
NODE_PROVIDER_API_KEY=
COINCAP_API_KEY=
TWITTER_OAUTH2_CLIENT_ID=
Expand Down
30 changes: 30 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ serde_json = "1.0.105"
bigdecimal = "0.4.1"
log = "0.4.20"
chrono = "0.4.29"
dotenv_codegen = "0.15.0"
14 changes: 6 additions & 8 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use starknet::providers::Provider;
use starknet::providers::{jsonrpc::HttpTransport, JsonRpcClient};
use std::time::Duration;

use crate::{COINCAP_API_KEY, NODE_PROVIDER_API_KEY};

#[derive(Debug, Serialize, Deserialize)]
struct FetchCoinResponse {
data: Data,
Expand Down Expand Up @@ -50,8 +48,7 @@ pub struct Token {
}

pub async fn fetch_coin(coin_id: &str) -> Result<f64, reqwest::Error> {
dotenv().ok();
let token = std::env::var(COINCAP_API_KEY).expect("COINCAP_API_KEY must be set");
let token = dotenv!("COINCAP_API_KEY");

let mut headers = reqwest::header::HeaderMap::new();
let auth = String::from(format!("Bearer {token}"));
Expand Down Expand Up @@ -86,7 +83,7 @@ pub async fn fetch_coin(coin_id: &str) -> Result<f64, reqwest::Error> {

pub async fn fetch_events(token: Token) -> Result<(), reqwest::Error> {
dotenv().ok();
let api_key = std::env::var(NODE_PROVIDER_API_KEY).expect("COINCAP_API_KEY must be set");
let api_key = dotenv!("NODE_PROVIDER_API_KEY");

let rpc_url = format!("https://starknet-mainnet.infura.io/v3/{api_key}");
let rpc_client = JsonRpcClient::new(HttpTransport::new(Url::parse(&rpc_url).unwrap()));
Expand Down Expand Up @@ -131,9 +128,7 @@ pub async fn fetch_events(token: Token) -> Result<(), reqwest::Error> {
}

async fn address_to_domain(address: FieldElement, contract_addr: FieldElement) {
dotenv().ok();
let api_key = std::env::var(NODE_PROVIDER_API_KEY).expect("COINCAP_API_KEY must be set");

let api_key = dotenv!("NODE_PROVIDER_API_KEY");
let rpc_url = format!("https://starknet-mainnet.infura.io/v3/{api_key}");
let rpc_client = JsonRpcClient::new(HttpTransport::new(Url::parse(&rpc_url).unwrap()));
let repsonse = rpc_client
Expand Down Expand Up @@ -227,6 +222,9 @@ fn extract_stars(mut domain: &str) -> (&str, usize) {
(domain, k)
}

fn get_coincap_api_key() -> &'static str {
dotenv!("COINCAP_API_KEY")
}
#[cfg(test)]
mod tests {
use super::{fetch_coin, fetch_events, Token};
Expand Down
9 changes: 8 additions & 1 deletion src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
use log::{Level, Metadata, Record};
use log::{Level, LevelFilter, Metadata, Record};

// use log::{debug, error, info, trace, warn};
pub struct SimpleLogger;

pub fn init() {
log::set_logger(&SimpleLogger)
.map(|()| log::set_max_level(LevelFilter::Trace))
.expect("Couldn't setup the logger");
}

impl log::Log for SimpleLogger {
fn enabled(&self, _metadata: &Metadata) -> bool {
true
Expand Down
40 changes: 8 additions & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,28 @@
use dotenv::dotenv;
use log::{debug, error, info, trace, warn};
use logger::SimpleLogger;
use std::error::Error;
use twitter::tweet;
#[macro_use]
extern crate dotenv_codegen;

mod api;
mod db;
mod logger;
mod twitter;

const COINCAP_API_KEY: &str = "COINCAP_API_KEY";
const NODE_PROVIDER_API_KEY: &str = "NODE_PROVIDER_API_KEY";
const TWITTER_OAUTH2_CLIENT_ID: &str = "TWITTER_OAUTH2_CLIENT_ID";
const TWITTER_OAUTH2_CLIENT_SECRET: &str = "TWITTER_OAUTH2_CLIENT_SECRET";

use log::{LevelFilter, SetLoggerError};

pub fn init() -> Result<(), SetLoggerError> {
log::set_logger(&SimpleLogger).map(|()| log::set_max_level(LevelFilter::Trace))
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
init().unwrap();
logger::init();
check_valid_env();
info!("info");
trace!("trace");
warn!("warn");
debug!("debug");
error!("error");
// tweet("Someteaeazzhing".to_string()).await;
Ok(())
}

fn check_valid_env() {
dotenv().ok();
std::env::var(COINCAP_API_KEY).expect("COINCAP_API_KEY must be set.");
std::env::var(NODE_PROVIDER_API_KEY).expect("NODE_PROVIDER_API_KEY must be set.");
std::env::var("COINCAP_API_KEY").expect("COINCAP_API_KEY must be set.");
std::env::var("NODE_PROVIDER_API_KEY").expect("NODE_PROVIDER_API_KEY must be set.");
// TODO These 2 are only required when doing the login, so they prob can be taken out?
// Maybe this can even turn into another crate?
std::env::var(TWITTER_OAUTH2_CLIENT_ID).expect("TWITTER_OAUTH2_CLIENT_ID must be set.");
std::env::var(TWITTER_OAUTH2_CLIENT_SECRET).expect("TWITTER_OAUTH2_CLIENT_SECRET must be set.");
}
/*
mod rpc;
mod starknet;
fn main() -> sc_cli::Result<()> {
command::run()
std::env::var("TWITTER_OAUTH2_CLIENT_ID").expect("TWITTER_OAUTH2_CLIENT_ID must be set.");
std::env::var("TWITTER_OAUTH2_CLIENT_SECRET")
.expect("TWITTER_OAUTH2_CLIENT_SECRET must be set.");
}
*/
7 changes: 2 additions & 5 deletions src/twitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ use twitter_v2::{

// TODO Update gitignore when all js gone
const PATH_TO_TOKEN_FILE: &str = "./db/token.json";
use crate::{TWITTER_OAUTH2_CLIENT_ID, TWITTER_OAUTH2_CLIENT_SECRET};
pub async fn tweet(text_to_tweet: String) {
dotenv().ok();

let client_id =
std::env::var(TWITTER_OAUTH2_CLIENT_ID).expect("TWITTER_OAUTH2_CLIENT_ID must be set");
let client_secret = std::env::var(TWITTER_OAUTH2_CLIENT_SECRET)
.expect("TWITTER_OAUTH2_CLIENT_SECRET must be set");
let client_id = dotenv!("TWITTER_OAUTH2_CLIENT_ID");
let client_secret = dotenv!("TWITTER_OAUTH2_CLIENT_SECRET");

let oauth2_client: Oauth2Client = Oauth2Client::new(
client_id,
Expand Down

0 comments on commit 1c57b72

Please sign in to comment.