Gitlab updates will be applied at 1:30pm, (17:30 UTC), today. No downtime expected.

Commit fa3cff58 authored by Brad King's avatar Brad King Committed by Kitware Robot

Merge topic 'reuse-client'

03d60fd9 Revert "Merge topic 'no-persist-client'"
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Reviewed-by: Brad King's avatarBrad King <brad.king@kitware.com>
Merge-request: !97
parents a84836ab 03d60fd9
Pipeline #65317 passed with stage
in 0 seconds
......@@ -13,10 +13,10 @@ keywords = ["gitlab", "api"]
categories = ["web-programming", "api-bindings"]
[dependencies]
hyper = { version = "~0.10", default-features = false }
hyper = { version = "~0.11", default-features = false }
itertools = "~0.6"
log = "~0.3"
reqwest = "~0.6"
reqwest = "~0.7"
serde_derive = "^1.0"
url = "^1.4"
......
......@@ -6,9 +6,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use crates::reqwest;
use crates::serde_json::Value;
error_chain! {
foreign_links {
Reqwest(reqwest::Error)
#[doc = "An error from the reqwest crate."];
}
errors {
/// Error occurred when communicating with Gitlab.
Communication {
......
......@@ -25,6 +25,8 @@ use std::fmt::{self, Display, Debug};
///
/// Separate users should use separate instances of this.
pub struct Gitlab {
/// The client to use for API calls.
client: Client,
/// The base URL to use for API calls.
base_url: Url,
/// The secret token to use when communicating with Gitlab.
......@@ -93,6 +95,7 @@ impl Gitlab {
.chain_err(|| ErrorKind::UrlParse)?;
let api = Gitlab {
client: Client::new()?,
base_url: base_url,
token: token,
};
......@@ -458,10 +461,10 @@ impl Gitlab {
}
/// Refactored code which talks to Gitlab and transforms error messages properly.
fn _comm<T>(&self, req: RequestBuilder) -> Result<T>
fn _comm<T>(&self, mut req: RequestBuilder) -> Result<T>
where T: DeserializeOwned,
{
let req = req.header(GitlabPrivateToken(self.token.to_string()));
req.header(GitlabPrivateToken(self.token.to_string()));
let rsp = req.send().chain_err(|| ErrorKind::Communication)?;
if !rsp.status().is_success() {
let v = serde_json::from_reader(rsp).chain_err(|| ErrorKind::Deserialize)?;
......@@ -492,7 +495,7 @@ impl Gitlab {
V: AsRef<str>,
{
let full_url = self._mk_url_with_param(url, param)?;
let req = Client::new().chain_err(|| ErrorKind::Communication)?.get(full_url);
let req = self.client.get(full_url)?;
self._comm(req)
}
......@@ -510,7 +513,8 @@ impl Gitlab {
U: Serialize,
{
let full_url = self._mk_url(url)?;
let req = Client::new().chain_err(|| ErrorKind::Communication)?.post(full_url).form(&param);
let mut req = self.client.post(full_url)?;
req.form(&param)?;
self._comm(req)
}
......@@ -528,7 +532,8 @@ impl Gitlab {
U: Serialize,
{
let full_url = self._mk_url(url)?;
let req = Client::new().chain_err(|| ErrorKind::Communication)?.request(Method::Put, full_url).form(&param);
let mut req = self.client.request(Method::Put, full_url)?;
req.form(&param)?;
self._comm(req)
}
......@@ -561,7 +566,7 @@ impl Gitlab {
let mut page_url = full_url.clone();
page_url.query_pairs_mut()
.extend_pairs(&[("page", page_str), ("per_page", per_page_str)]);
let req = Client::new().chain_err(|| ErrorKind::Communication)?.get(page_url);
let req = self.client.get(page_url)?;
let page: Vec<T> = self._comm(req)?;
let page_len = page.len();
......
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