From 8e182c0a556a1700b56e679a8d2ddced7539b8ce Mon Sep 17 00:00:00 2001 From: PARK BYUNG JUN Date: Fri, 5 Aug 2022 04:39:20 +0000 Subject: [PATCH] repositories are added --- .vscode/launch.json | 8 +- Cargo.toml | 1 + src/repositories/account/mod.rs | 7 + src/repositories/account/models.rs | 59 +++++++ src/repositories/account/repository.rs | 183 +++++++++++++++++++++ src/repositories/betting/mod.rs | 7 + src/repositories/betting/models.rs | 57 +++++++ src/repositories/betting/repository.rs | 204 ++++++++++++++++++++++++ src/repositories/game/mod.rs | 7 + src/repositories/game/models.rs | 47 ++++++ src/repositories/game/repository.rs | 150 +++++++++++++++++ src/repositories/identity/mod.rs | 7 + src/repositories/identity/models.rs | 46 ++++++ src/repositories/identity/repository.rs | 145 +++++++++++++++++ src/repositories/mod.rs | 4 + 15 files changed, 928 insertions(+), 4 deletions(-) create mode 100644 src/repositories/account/mod.rs create mode 100644 src/repositories/account/models.rs create mode 100644 src/repositories/account/repository.rs create mode 100644 src/repositories/betting/mod.rs create mode 100644 src/repositories/betting/models.rs create mode 100644 src/repositories/betting/repository.rs create mode 100644 src/repositories/game/mod.rs create mode 100644 src/repositories/game/models.rs create mode 100644 src/repositories/game/repository.rs create mode 100644 src/repositories/identity/mod.rs create mode 100644 src/repositories/identity/models.rs create mode 100644 src/repositories/identity/repository.rs diff --git a/.vscode/launch.json b/.vscode/launch.json index 38fedcc..68409c6 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -22,8 +22,8 @@ "env": { "URL_BROKER": "nats://192.168.50.200:4222", "QUEUE_BROKER": "bet.beteran", - "K_SECRET": "bet.beteran", - "K_USERNAME": "bet.beteran", + "K_SECRET": "c23d770b873b2ce95747abc57052beb0", + "K_USERNAME": "Turbo77", }, "args": [], "cwd": "${workspaceFolder}" @@ -47,8 +47,8 @@ "env": { "URL_BROKER": "nats://192.168.50.200:4222", "QUEUE_BROKER": "bet.beteran", - "K_SECRET": "bet.beteran", - "K_USERNAME": "bet.beteran", + "K_SECRET": "c23d770b873b2ce95747abc57052beb0", + "K_USERNAME": "Turbo77", }, "args": [], "cwd": "${workspaceFolder}" diff --git a/Cargo.toml b/Cargo.toml index 9e09575..4a926ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ futures = { version = "0", default-features = false, features = [ ] } nats = { version = "0" } prost = { version = "0" } +reqwest = { version = "0" } serde = { version = "1", features = ["derive"] } serde_json = { version = "1" } tokio = { version = "1", features = ["macros", "rt-multi-thread"] } diff --git a/src/repositories/account/mod.rs b/src/repositories/account/mod.rs new file mode 100644 index 0000000..2e71a26 --- /dev/null +++ b/src/repositories/account/mod.rs @@ -0,0 +1,7 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; diff --git a/src/repositories/account/models.rs b/src/repositories/account/models.rs new file mode 100644 index 0000000..f8f58a9 --- /dev/null +++ b/src/repositories/account/models.rs @@ -0,0 +1,59 @@ +use serde::{Deserialize, Serialize}; + +pub struct Error { + pub code: i64, + pub msg: Option, +} + +pub struct FindBalanceRequest { + pub username: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindBalanceResponse { + pub code: i64, + pub msg: Option, + pub balance: i64, + pub balance_bota: i64, + pub balance_sum: i64, + pub companies: i64, +} + +pub struct FindPartnerBalanceRequest {} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindPartnerBalanceResponse { + pub code: i64, + pub msg: Option, + pub balance: i64, + pub balance_bota: i64, +} + +pub struct SaveDepositRequest { + pub username: String, + pub cash_type: Option, + pub amount: i64, + pub request_key: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SaveDepositResponse { + pub code: i64, + pub msg: Option, + pub balance: i64, +} + +pub struct SaveWithdrawRequest { + pub username: String, + pub request_key: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SaveWithdrawResponse { + pub code: i64, + pub msg: Option, + pub balance: i64, + pub balance_cash: i64, + pub balance_cash_bota: i64, + pub amount: i64, +} diff --git a/src/repositories/account/repository.rs b/src/repositories/account/repository.rs new file mode 100644 index 0000000..e95c6f0 --- /dev/null +++ b/src/repositories/account/repository.rs @@ -0,0 +1,183 @@ +use super::models; +use std::collections::HashMap; + +/// +pub struct Repository { + client: reqwest::Client, + k_secret: String, + k_username: String, +} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of api.kgon.identity").finish() + } +} + +impl Repository { + /// + pub fn new(k_secret: &str, k_username: &str) -> Repository { + Repository { + client: reqwest::Client::new(), + k_secret: k_secret.to_string(), + k_username: k_username.to_string(), + } + } + + /// + pub async fn find_balance( + &self, + data: models::FindBalanceRequest, + ) -> Result { + let mut params = HashMap::new(); + + params.insert("username", data.username); + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/balance") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn find_partner_balance( + &self, + data: models::FindPartnerBalanceRequest, + ) -> Result { + let res = match self + .client + .post("https://dev-mw.kgonapi.com/partner/balance") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn save_deposit( + &self, + data: models::SaveDepositRequest, + ) -> Result { + let mut params = HashMap::new(); + + params.insert("username", data.username); + if let Some(cash_type) = data.cash_type { + params.insert("cashType", cash_type); + } + params.insert("amount", data.amount.to_string()); + if let Some(request_key) = data.request_key { + params.insert("requestKey", request_key); + } + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/deposit") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn save_withdraw( + &self, + data: models::SaveWithdrawRequest, + ) -> Result { + let mut params = HashMap::new(); + + params.insert("username", data.username); + if let Some(request_key) = data.request_key { + params.insert("requestKey", request_key); + } + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/withdraw") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } +} diff --git a/src/repositories/betting/mod.rs b/src/repositories/betting/mod.rs new file mode 100644 index 0000000..2e71a26 --- /dev/null +++ b/src/repositories/betting/mod.rs @@ -0,0 +1,7 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; diff --git a/src/repositories/betting/models.rs b/src/repositories/betting/models.rs new file mode 100644 index 0000000..0320047 --- /dev/null +++ b/src/repositories/betting/models.rs @@ -0,0 +1,57 @@ +use serde::{Deserialize, Serialize}; + +pub struct Error { + pub code: i64, + pub msg: Option, +} + +pub struct FindTransactionsRequest { + pub vendor_key: Option, + pub sdate: Option, + pub edate: Option, + pub group_key: Option, + pub username: Option, + pub limit: i64, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindTransactionsResponse { + pub code: i64, + pub msg: Option, + pub transactions: Vec, +} + +pub struct FindPragmaticHistoryRequest { + pub id: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindPragmaticHistoryResponse { + pub code: i64, + pub msg: Option, + pub url: Option, +} + +pub struct FindEvolutionTransactionDetailRequest { + pub ids: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindEvolutionTransactionDetailResponse { + pub code: i64, + pub msg: Option, + pub transactions: Vec, +} + +pub struct FindStatisticRequest { + pub vendor_key: Option, + pub group_key: Option, + pub date: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindStatisticResponse { + pub code: i64, + pub msg: Option, + pub data: Option, +} diff --git a/src/repositories/betting/repository.rs b/src/repositories/betting/repository.rs new file mode 100644 index 0000000..da1ea01 --- /dev/null +++ b/src/repositories/betting/repository.rs @@ -0,0 +1,204 @@ +use super::models; +use std::{collections::HashMap, time::Duration}; + +/// +pub struct Repository { + client: reqwest::Client, + k_secret: String, + k_username: String, +} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of api.kgon.identity").finish() + } +} + +impl Repository { + /// + pub fn new(k_secret: &str, k_username: &str) -> Repository { + Repository { + client: reqwest::Client::new(), + k_secret: k_secret.to_string(), + k_username: k_username.to_string(), + } + } + + /// + pub async fn find_transactions( + &self, + data: models::FindTransactionsRequest, + ) -> Result { + let mut params = HashMap::new(); + + if let Some(vendor_key) = data.vendor_key { + params.insert("vendorKey", vendor_key); + } + if let Some(sdate) = data.sdate { + params.insert("sdate", sdate); + } + if let Some(edate) = data.edate { + params.insert("edate", edate); + } + if let Some(group_key) = data.group_key { + params.insert("groupKey", group_key); + } + if let Some(username) = data.username { + params.insert("username", username); + } + + params.insert("limit", data.limit.to_string()); + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/transaction") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn find_gragmatic_history( + &self, + data: models::FindPragmaticHistoryRequest, + ) -> Result { + let mut params = HashMap::new(); + + params.insert("id", data.id); + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/pragmatic/history") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn find_evolution_transaction_detail( + &self, + data: models::FindEvolutionTransactionDetailRequest, + ) -> Result { + let mut params = HashMap::new(); + + params.insert("ids", data.ids); + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/evolution/transaction/detail") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res + .json::() + .await + { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn find_statistic( + &self, + data: models::FindStatisticRequest, + ) -> Result { + let mut params = HashMap::new(); + + if let Some(vendor_key) = data.vendor_key { + params.insert("vendorKey", vendor_key); + } + if let Some(group_key) = data.group_key { + params.insert("groupKey", group_key); + } + if let Some(date) = data.date { + params.insert("date", date); + } + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/statistic") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } +} diff --git a/src/repositories/game/mod.rs b/src/repositories/game/mod.rs new file mode 100644 index 0000000..2e71a26 --- /dev/null +++ b/src/repositories/game/mod.rs @@ -0,0 +1,7 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; diff --git a/src/repositories/game/models.rs b/src/repositories/game/models.rs new file mode 100644 index 0000000..5df3e3d --- /dev/null +++ b/src/repositories/game/models.rs @@ -0,0 +1,47 @@ +use serde::{Deserialize, Serialize}; + +pub struct Error { + pub code: i64, + pub msg: Option, +} + +pub struct FindVendorsRequest {} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindVendorsResponse { + pub code: i64, + pub msg: Option, + pub vendors: Vec, +} + +pub struct FindGamesRequest { + pub vendor_key: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindGamesResponse { + pub code: i64, + pub msg: Option, + pub games: Vec, +} + +pub struct PlayRequest { + pub vendor_key: String, + pub game_key: String, + pub username: String, + pub nickname: String, + pub site_username: String, + pub group_key: Option, + pub amount: i64, + pub request_key: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct PlayResponse { + pub code: i64, + pub msg: Option, + #[serde(rename = "userId")] + pub user_id: String, + pub url: String, + pub balance: i64, +} diff --git a/src/repositories/game/repository.rs b/src/repositories/game/repository.rs new file mode 100644 index 0000000..dd4912a --- /dev/null +++ b/src/repositories/game/repository.rs @@ -0,0 +1,150 @@ +use super::models; +use std::{collections::HashMap, time::Duration}; + +/// +pub struct Repository { + client: reqwest::Client, + k_secret: String, + k_username: String, +} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of api.kgon.identity").finish() + } +} + +impl Repository { + /// + pub fn new(k_secret: &str, k_username: &str) -> Repository { + Repository { + client: reqwest::Client::new(), + k_secret: k_secret.to_string(), + k_username: k_username.to_string(), + } + } + + /// + pub async fn find_vendors( + &self, + data: models::FindVendorsRequest, + ) -> Result { + let res = match self + .client + .post("https://dev-mw.kgonapi.com/vendors") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn find_games( + &self, + data: models::FindGamesRequest, + ) -> Result { + let mut params = HashMap::new(); + + params.insert("vendorKey", data.vendor_key); + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/games") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn play( + &self, + data: models::PlayRequest, + ) -> Result { + let mut params = HashMap::new(); + + params.insert("vendorKey", data.vendor_key); + params.insert("gameKey", data.game_key); + params.insert("username", data.username); + params.insert("nickname", data.nickname); + params.insert("siteUsername", data.site_username); + + if let Some(group_key) = data.group_key { + params.insert("groupKey", group_key); + } + + params.insert("amount", data.amount.to_string()); + + if let Some(request_key) = data.request_key { + params.insert("requestKey", request_key); + } + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/play") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .timeout(Duration::from_secs(10)) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } +} diff --git a/src/repositories/identity/mod.rs b/src/repositories/identity/mod.rs new file mode 100644 index 0000000..2e71a26 --- /dev/null +++ b/src/repositories/identity/mod.rs @@ -0,0 +1,7 @@ +//! +//! + +/// +pub mod models; +/// +pub mod repository; diff --git a/src/repositories/identity/models.rs b/src/repositories/identity/models.rs new file mode 100644 index 0000000..fc0efe1 --- /dev/null +++ b/src/repositories/identity/models.rs @@ -0,0 +1,46 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct Error { + pub code: i64, + pub msg: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindMembersRequest { + pub group_key: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindMember { + pub id: i64, + pub username: String, + pub cash: i64, + pub cash_bota: i64, + pub nickname: String, + pub site_username: String, + pub group_key: Option, + pub oriental_play: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct FindMembersResponse { + pub code: i64, + pub msg: Option, + pub users: Vec, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SaveMemberRequest { + pub username: String, + pub nickname: String, + pub site_username: String, + pub group_key: Option, +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct SaveMemberResponse { + pub code: i64, + pub msg: Option, + pub id: Option, +} diff --git a/src/repositories/identity/repository.rs b/src/repositories/identity/repository.rs new file mode 100644 index 0000000..644aaf6 --- /dev/null +++ b/src/repositories/identity/repository.rs @@ -0,0 +1,145 @@ +use super::models; +use std::collections::HashMap; + +/// +pub struct Repository { + client: reqwest::Client, + k_secret: String, + k_username: String, +} + +impl std::fmt::Debug for Repository { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("Repository of api.kgon.identity").finish() + } +} + +impl Repository { + /// + pub fn new(k_secret: &str, k_username: &str) -> Repository { + Repository { + client: reqwest::Client::new(), + k_secret: k_secret.to_string(), + k_username: k_username.to_string(), + } + } + + /// + pub async fn find_members( + &self, + data: models::FindMembersRequest, + ) -> Result { + let mut params = HashMap::new(); + + if let Some(group_key) = data.group_key { + params.insert("groupKey", group_key); + } + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/users") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } + + /// + pub async fn save_member( + &self, + data: models::SaveMemberRequest, + ) -> Result { + let mut params = HashMap::new(); + + params.insert("username", data.username); + params.insert("nickname", data.nickname); + params.insert("siteUsername", data.site_username); + + if let Some(group_key) = data.group_key { + params.insert("groupKey", group_key); + } + + let res = match self + .client + .post("https://dev-mw.kgonapi.com/register") + .header(reqwest::header::ACCEPT, "application/json") + .header( + reqwest::header::CONTENT_TYPE, + "application/x-www-form-urlencoded", + ) + .header("k-secret", self.k_secret.as_str()) + .header("k-username", self.k_username.as_str()) + .form(¶ms) + .send() + .await + { + Ok(res) => res, + Err(err) => { + return Err(models::Error { code: 0, msg: None }); + } + }; + + match res.status() { + reqwest::StatusCode::OK => match res.json::().await { + Ok(r) => Ok(r), + Err(e) => Err(models::Error { code: 0, msg: None }), + }, + _ => Err(models::Error { code: 0, msg: None }), + } + } +} + +#[cfg(test)] +mod tests { + use super::super::models; + use super::*; + + #[tokio::test] + async fn find_members() { + let repository = Repository::new("c23d770b873b2ce95747abc57052beb0", "Turbo77"); + + let r = repository + .find_members(models::FindMembersRequest { group_key: None }) + .await + .expect(""); + + println!("members: {:?}", r); + } + + #[tokio::test] + async fn save_member() { + let repository = Repository::new("c23d770b873b2ce95747abc57052beb0", "Turbo77"); + + let r = repository + .save_member(models::SaveMemberRequest { + username: "".to_string(), + nickname: "".to_string(), + site_username: "".to_string(), + group_key: None, + }) + .await + .expect(""); + + println!("member: {:?}", r); + } +} diff --git a/src/repositories/mod.rs b/src/repositories/mod.rs index e69de29..6c0af02 100644 --- a/src/repositories/mod.rs +++ b/src/repositories/mod.rs @@ -0,0 +1,4 @@ +pub mod account; +pub mod betting; +pub mod game; +pub mod identity;