mod build_error;
pub use build_error::BuildError;
use super::Instance;
use std::time::Duration;
#[derive(Debug)]
pub struct InstanceBuilder {
pub token: Option<String>,
pub http_client: reqwest::Client,
pub api_url: String,
pub api_version: String,
pub time_between_requests: std::time::Duration,
}
impl PartialEq for InstanceBuilder {
fn eq(&self, other: &Self) -> bool {
self.token == other.token &&
self.api_url == other.api_url &&
self.api_version == other.api_version &&
self.time_between_requests == other.time_between_requests
}
}
impl InstanceBuilder {
pub fn new() -> InstanceBuilder {
InstanceBuilder::default()
}
pub fn token<T>(mut self, token: T) -> InstanceBuilder
where
T: ToString
{
self.token = Some(token.to_string());
self
}
pub fn http_client(mut self, http_client: reqwest::Client) -> InstanceBuilder {
self.http_client = http_client;
self
}
pub fn api_url<T>(mut self, api_url: T) -> InstanceBuilder
where
T: ToString
{
self.api_url = api_url.to_string();
self
}
pub fn api_version<T>(mut self, api_version: T ) -> InstanceBuilder
where
T: ToString
{
self.api_version = api_version.to_string();
self
}
pub fn time_between_requests(mut self, time_between_requests: std::time::Duration) -> InstanceBuilder {
self.time_between_requests = time_between_requests;
self
}
pub fn build(self) -> Result<Instance, BuildError> {
let token = match self.token {
Some(token) => token,
None => return Err(BuildError::MissingParameter(String::from("token"))),
};
Ok(Instance {
token,
http_client: self.http_client,
api_url: self.api_url,
api_version: self.api_version,
time_between_requests: self.time_between_requests,
})
}
}
impl Default for InstanceBuilder {
fn default() -> Self {
InstanceBuilder {
token: None,
http_client: reqwest::Client::new(),
api_url: String::from("https://api.vk.com/"),
api_version: String::from("5.103"),
time_between_requests: Duration::from_millis(334),
}
}
}
#[cfg(test)]
mod tests {
use reqwest::Client;
use super::*;
#[test]
fn missing_token() {
let instance = InstanceBuilder::new().build();
assert_eq!(
instance.err(),
Some(BuildError::MissingParameter(String::from("token")))
);
}
#[test]
fn custom_api_url() {
let instance = InstanceBuilder::new()
.api_url("https://example.com/")
.token(String::from("token"))
.build()
.unwrap();
assert_eq!(
instance,
Instance {
token: String::from("token"),
http_client: Client::new(),
api_url: String::from("https://example.com/"),
api_version: String::from("5.103"),
time_between_requests: Duration::from_millis(334)
}
);
}
#[test]
fn custom_all() {
let instance = InstanceBuilder::new()
.api_url("https://api.vk.ru/")
.api_version("5.143")
.token(String::from("123456789"))
.http_client(Client::new())
.time_between_requests(Duration::from_millis(500))
.build()
.unwrap();
assert_eq!(
instance,
Instance {
token: String::from("123456789"),
http_client: Client::new(),
api_url: String::from("https://api.vk.ru/"),
api_version: String::from("5.143"),
time_between_requests: Duration::from_millis(500)
}
);
}
#[test]
fn custom_token() {
let instance = InstanceBuilder::new()
.token(String::from("123456789"))
.build()
.unwrap();
assert_eq!(
instance,
Instance {
token: String::from("123456789"),
http_client: Client::new(),
api_url: String::from("https://api.vk.com/"),
api_version: String::from("5.103"),
time_between_requests: Duration::from_millis(334)
}
);
}
}