mod handler;
mod request;
mod service;
mod types;
mod utils;
pub mod prelude {
pub use crate::request::Auth as ServiceAuth;
pub use crate::service::*;
pub use crate::types::*;
pub use crate::utils::test_utils;
}
use prelude::*;
pub struct Client {
username: String,
api_key: String,
host: String,
}
impl Default for Client {
fn default() -> Self {
Self::new()
}
}
impl Client {
pub fn new() -> Self {
Client {
username: "".to_string(),
api_key: "".to_string(),
host: "".to_string(),
}
}
pub fn username(mut self, username: &str) -> Self {
self.username = username.to_string();
self
}
pub fn api_key(mut self, api_key: &str) -> Self {
self.api_key = api_key.to_string();
self
}
pub fn host(mut self, host: &str) -> Self {
self.host = host.to_string();
self
}
pub fn auth(&self) -> Service<Authenticated> {
Service::<Unauthenticated>::with_resource(Resource::Auth)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn block(&self) -> Service<Authenticated> {
Service::<Unauthenticated>::with_resource(Resource::Blocks)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn cart(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Cart)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn call_request(&self) -> Service<Authenticated> {
Service::with_resource(Resource::CallRequest)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn category(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Category)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn discussion(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Discussion)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn language(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Languages)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn langvars(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Langvars)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn order(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Order)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn page(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Pages)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn payment_method(&self) -> Service<Authenticated> {
Service::with_resource(Resource::PaymentMethod)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn product(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Product)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn product_feature(&self) -> Service<Authenticated> {
Service::with_resource(Resource::ProductFeature)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn product_option(&self) -> Service<Authenticated> {
Service::with_resource(Resource::ProductOption)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn settings(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Settings)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn shipment(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Shipment)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn shipment_method(&self) -> Service<Authenticated> {
Service::with_resource(Resource::ShipmentMethod)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn status(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Status)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn tax(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Tax)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn user(&self) -> Service<Authenticated> {
Service::with_resource(Resource::User)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn user_group(&self) -> Service<Authenticated> {
Service::with_resource(Resource::UserGroups)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
pub fn vendor(&self) -> Service<Authenticated> {
Service::with_resource(Resource::Vendor)
.host(self.host.as_str())
.auth(ServiceAuth::from(&self.username, &self.api_key))
}
fn get_username(&self) -> &str {
&self.username
}
fn get_api_key(&self) -> &str {
&self.api_key
}
fn get_host(&self) -> &str {
&self.host
}
}
#[cfg(test)]
mod tests {
use super::*;
use dotenv::dotenv;
#[test]
fn it_creates_api_client() {
dotenv().ok();
let api_key = std::env::var("CSCART_API_KEY").expect("No api key found");
let username = std::env::var("CSCART_USERNAME").expect("No username found");
let host = std::env::var("CSCART_HOST").expect("No host found");
let client = Client::new()
.host(host.as_str())
.username(username.as_str())
.api_key(api_key.as_str());
assert_eq!(client.get_username(), username);
assert_eq!(client.get_api_key(), api_key);
assert_eq!(client.get_host(), host);
}
}