Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
Add Meta and Transactional Data modules
Browse files Browse the repository at this point in the history
  • Loading branch information
system32uwu committed Mar 27, 2022
1 parent 3682d81 commit 5762d57
Show file tree
Hide file tree
Showing 10 changed files with 299 additions and 11 deletions.
40 changes: 35 additions & 5 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::{
db,
util::types::{self, LoginResponse},
util::types::{self, AccountsResponse, CreditCardsResponse, LoginResponse},
};
use core::panic;
use serde::Deserialize;
use std::time::Duration;

pub struct Api {
client: reqwest::Client,
base_url: String,
pub client: reqwest::Client,
pub api_key: Option<String>,
base_url: String,
}

impl Api {
Expand Down Expand Up @@ -80,6 +80,11 @@ impl Api {
}
}

pub async fn login(&self, form_data: Vec<(&str, &str)>) -> Result<LoginResponse, String> {
self.fetch::<types::LoginResponse>("login/", "POST+FORM", None, Some(form_data))
.await
}

pub async fn list_providers(&self) -> Vec<types::Provider> {
match self
.fetch::<types::ProvidersResponse>("provider/", "GET", None, None)
Expand All @@ -90,8 +95,33 @@ impl Api {
}
}

pub async fn login(&self, form_data: Vec<(&str, &str)>) -> Result<LoginResponse, String> {
self.fetch::<types::LoginResponse>("login/", "POST+FORM", None, Some(form_data))
pub async fn get_provider_details(&self, code: String) -> Result<types::Provider, String> {
match self
.fetch::<types::Provider>(&["provider", code.as_str()].join("/"), "GET", None, None)
.await
{
Ok(data) => Ok(data),
Err(e) => panic!("Unexpected error\n\n{}", e),
}
}

pub async fn get_accounts(&self, user_key: String) -> Result<AccountsResponse, String> {
self.fetch::<types::AccountsResponse>(
&[["account", "?key"].join("/"), user_key].join("="),
"GET",
None,
None,
)
.await
}

pub async fn get_credit_cards(&self, user_key: String) -> Result<CreditCardsResponse, String> {
self.fetch::<types::CreditCardsResponse>(
&[["credit-card", "?key"].join("/"), user_key].join("="),
"GET",
None,
None,
)
.await
}
}
9 changes: 6 additions & 3 deletions src/cli/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn menu_options() -> Vec<String> {
}
}

pub async fn login(api: &Api) {
async fn login(api: &Api) {
let providers = api.list_providers().await;

let provider = Select::with_theme(&ColorfulTheme::default())
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn logout() {
}

pub async fn menu(api: &Api) {
let mut selection: usize;
let selection: usize;

match api.api_key {
Some(_) => loop {
Expand All @@ -98,7 +98,10 @@ pub async fn menu(api: &Api) {
.unwrap();

match selection {
0 => login(api).await,
0 => {
login(api).await;
break;
}
1 => {
logout();
break;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn menu_options() -> Vec<String> {
vec![String::from(set_api_option), String::from("↵ Back")]
}

pub fn set_api_key(api: &mut Api) {
fn set_api_key(api: &mut Api) {
let input = Input::with_theme(&ColorfulTheme::default())
.with_prompt("Paste your API Key: ")
.validate_with({
Expand Down
2 changes: 2 additions & 0 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub async fn menu() {

match selection {
0 => cli::auth::menu(&api).await,
1 => cli::transactional_data::menu(&api).await,
2 => cli::meta::menu(&api).await,
3 => cli::config::menu(&mut api),
4 | _ => break,
};
Expand Down
81 changes: 81 additions & 0 deletions src/cli/meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::{
api::Api,
util::{self, types::Provider},
};
use colored::*;
use dialoguer::{theme::ColorfulTheme, Select};
use std::{thread, time::Duration};

fn menu_options() -> Vec<String> {
return vec![
String::from("List providers"),
String::from("Get Provider details"),
String::from("↵ Back"),
];
}

async fn list_providers(api: &Api) -> Vec<Provider> {
return api.list_providers().await;
}

async fn get_provider_details(providers: &Vec<Provider>) -> &Provider {
let provider_selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select a provider (🠕/🠗)")
.default(0)
.items(&providers[..])
.interact()
.unwrap();

return &providers[provider_selection];
}

pub async fn menu(api: &Api) {
let mut selection: usize;
println!("Fetching cached providers...");
let mut cached_providers: Vec<Provider> = list_providers(api).await;

match api.api_key {
Some(_) => loop {
util::clear_console();
util::print_top_message();

selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select an option (🠕/🠗)")
.default(0)
.items(&menu_options()[..])
.interact()
.unwrap();

match selection {
0 => {
cached_providers = list_providers(api).await;
for provider in &cached_providers {
println!("{}", provider)
}
util::back_menu();
}
1 => {
let provider_details = get_provider_details(&cached_providers).await;
println!(
"{:#?}",
serde_json::from_str::<Provider>(
&serde_json::to_string(&provider_details)
.unwrap()
.to_string()
)
.unwrap()
);
util::back_menu();
}
2 | _ => break,
};
},
None => {
println!(
"{}",
"API Key is not set. Please do so before using this module.".red()
);
thread::sleep(Duration::from_millis(1700));
}
}
}
2 changes: 2 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod auth;
pub mod config;
pub mod main;
pub mod meta;
pub mod transactional_data;

pub async fn run() {
main::menu().await;
Expand Down
90 changes: 90 additions & 0 deletions src/cli/transactional_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use crate::{
api::Api,
db,
util::{self, types::Account},
};
use colored::*;
use dialoguer::{theme::ColorfulTheme, Select};
use std::{thread, time::Duration};

fn menu_options() -> Vec<String> {
return vec![
String::from("Get accounts"),
String::from("List credit cards"),
String::from("↵ Back"),
];
}

async fn get_accounts(api: &Api, user_key: String) -> Vec<Account> {
match api.get_accounts(user_key).await {
Ok(response) => response.accounts,
Err(e) => {
println!("{}", e);
vec![]
}
}
}

async fn get_credit_cards(api: &Api, user_key: String) -> Vec<util::types::CreditCard> {
match api.get_credit_cards(user_key).await {
Ok(response) => response.credit_cards,
Err(e) => {
println!("{}", e);
vec![]
}
}
}

pub async fn menu(api: &Api) {
let mut selection: usize;

match api.api_key {
Some(_) => match db::get_user_key() {
Some(user_key) => loop {
util::clear_console();
util::print_top_message();

selection = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select an option (🠕/🠗)")
.default(0)
.items(&menu_options()[..])
.interact()
.unwrap();

match selection {
0 => {
let accounts = get_accounts(api, user_key.clone()).await;
util::clear_console();
for account in &accounts {
println!("{}\n", account)
}
util::back_menu();
}
1 => {
let credit_cards = get_credit_cards(api, user_key.clone()).await;
util::clear_console();
for cc in &credit_cards {
println!("{}\n", cc)
}
util::back_menu();
}
2 | _ => break,
};
},
None => {
println!(
"{}",
"You're not logged in. Please do so before using this module.".red()
);
thread::sleep(Duration::from_millis(1700));
}
},
None => {
println!(
"{}",
"API Key is not set. Please do so before using this module.".red()
);
thread::sleep(Duration::from_millis(1700));
}
}
}
7 changes: 7 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ pub fn get_user() -> Option<String> {
}
}

pub fn get_user_key() -> Option<String> {
match db().get::<String>("USER_KEY") {
Ok(user) => Some(user),
Err(_) => None,
}
}

pub fn set_pref(key: String, value: String) {
match db().save_with_id(&value, &key) {
Err(e) => panic!("Error {:?}", e),
Expand Down
11 changes: 10 additions & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io::Read;
use colored::*;
use dialoguer::{theme::ColorfulTheme, Select};
use std::io::Read;

pub mod types;

Expand All @@ -25,3 +26,11 @@ pub fn clear_console() {
.unwrap()
.success());
}

pub fn back_menu() {
Select::with_theme(&ColorfulTheme::default())
.default(0)
.items(&["↵ Back"])
.interact()
.unwrap();
}
Loading

0 comments on commit 5762d57

Please sign in to comment.