Commit 479e8a74 authored by Brad King's avatar Brad King
Browse files

gitlab: Enable construction via builder pattern

The protocol, host name, and access token are required and are therefore
parameters of the `new` (and `new_insecure`) constructor.  Enable
addition of other optional configuration by introducing a builder
pattern.
parent 8c056425
......@@ -112,6 +112,14 @@ impl Gitlab {
Ok(api)
}
/// Create a new Gitlab API client builder.
pub fn builder<H, T>(host: H, token: T) -> GitlabBuilder
where H: ToString,
T: ToString,
{
GitlabBuilder::new(host, token)
}
/// The user the API is acting as.
pub fn current_user(&self) -> Result<UserPublic> {
self.get("user")
......@@ -607,3 +615,34 @@ impl Gitlab {
Ok(results)
}
}
pub struct GitlabBuilder {
protocol: &'static str,
host: String,
token: String,
}
impl GitlabBuilder {
/// Create a new Gitlab API client builder.
pub fn new<H, T>(host: H, token: T) -> Self
where H: ToString,
T: ToString,
{
Self {
protocol: "https",
host: host.to_string(),
token: token.to_string(),
}
}
/// Switch to an insecure protocol (http instead of https).
pub fn insecure(&mut self) -> &mut Self
{
self.protocol = "http";
self
}
pub fn build(&self) -> Result<Gitlab> {
Gitlab::new_impl(self.protocol, &self.host, self.token.clone())
}
}
......@@ -51,6 +51,7 @@ pub use error::*;
pub use gitlab::CommitStatusInfo;
pub use gitlab::MergeRequestStateFilter;
pub use gitlab::Gitlab;
pub use gitlab::GitlabBuilder;
pub use types::*;
#[cfg(test)]
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment