use crate::config::{self, Creds};
use anyhow::{Context, Result};
use base64::Engine;
use serde_json::Value;
use std::time::Duration;
const API: &str = "https://api.bitbucket.org/2.0";
pub struct Client {
agent: ureq::Agent,
auth: String,
}
pub fn url(path: &str) -> String {
if path.starts_with("http://") || path.starts_with("https://") {
path.to_string()
} else {
format!("{API}/{}", path.trim_start_matches('/'))
}
}
impl Client {
pub fn new() -> Result<Client> {
let (creds, _) = config::load().map_err(|e| crate::exit::Exit::new(crate::exit::AUTH_REQUIRED, format!("{e:#}")))?;
Ok(Client::with_creds(&creds))
}
pub fn with_creds(creds: &Creds) -> Client {
let raw = format!("{}:{}", creds.email, creds.token);
Client {
agent: ureq::AgentBuilder::new()
.timeout_connect(Duration::from_secs(15))
.redirect_auth_headers(ureq::RedirectAuthHeaders::SameHost)
.build(),
auth: format!("Basic {}", base64::engine::general_purpose::STANDARD.encode(raw)),
}
}
pub fn request(&self, method: &str, path: &str, body: Option<&Value>) -> Result<String> {
let req = self.agent.request(method, &url(path)).set("Authorization", &self.auth);
let res = match body {
Some(b) => req.send_json(b),
None => req.call(),
};
match res {
Ok(r) => Ok(r.into_string()?),
Err(ureq::Error::Status(code, r)) => Err(status_error(code, &r.into_string().unwrap_or_default())),
Err(e) => Err(e).context("request failed"),
}
}
pub fn send(&self, method: &str, path: &str, headers: &[(String, String)], body: Option<&Value>) -> Result<Response> {
let mut req = self.agent.request(method, &url(path)).set("Authorization", &self.auth);
for (k, v) in headers {
req = req.set(k, v);
}
let res = match body {
Some(b) => req.send_json(b),
None => req.call(),
};
let r = match res {
Ok(r) | Err(ureq::Error::Status(_, r)) => r,
Err(e) => return Err(e).context("request failed"),
};
let mut names = r.headers_names();
names.dedup();
let headers = names.iter().flat_map(|n| r.all(n).into_iter().map(move |v| (n.clone(), v.to_string()))).collect();
Ok(Response {
status: r.status(),
status_line: format!("{} {} {}", r.http_version(), r.status(), r.status_text()),
headers,
body: r.into_string()?,
})
}
fn json(&self, method: &str, path: &str, body: Option<&Value>) -> Result<Value> {
let text = self.request(method, path, body)?;
if text.trim().is_empty() {
return Ok(Value::Null);
}
serde_json::from_str(&text).context("response was not JSON")
}
pub fn get(&self, path: &str) -> Result<Value> {
self.json("GET", path, None)
}
pub fn get_text(&self, path: &str) -> Result<String> {
self.request("GET", path, None)
}
pub fn post(&self, path: &str, body: Option<&Value>) -> Result<Value> {
self.json("POST", path, body)
}
#[allow(dead_code)]
pub fn put(&self, path: &str, body: &Value) -> Result<Value> {
self.json("PUT", path, Some(body))
}
pub fn delete(&self, path: &str) -> Result<Value> {
self.json("DELETE", path, None)
}
pub fn paginate(&self, path: &str, limit: usize) -> Result<Vec<Value>> {
Ok(self.list(path, limit)?.items)
}
pub fn list(&self, path: &str, limit: usize) -> Result<Listing> {
let mut out = Listing { items: Vec::new(), total: None, truncated: false };
let mut next = Some(path.to_string());
while let Some(p) = next {
let mut page = self.get(&p)?;
out.total = out.total.or(page["size"].as_u64());
if let Some(vals) = page["values"].as_array_mut() {
out.items.append(vals);
}
next = page["next"].as_str().map(String::from);
if out.items.len() >= limit {
out.truncated = out.items.len() > limit || next.is_some();
out.items.truncate(limit);
break;
}
}
Ok(out)
}
}
pub fn status_error(code: u16, text: &str) -> anyhow::Error {
let msg = serde_json::from_str::<Value>(text)
.ok()
.and_then(|v| v["error"]["message"].as_str().map(String::from))
.unwrap_or_else(|| text.to_string());
anyhow::anyhow!("HTTP {code}: {}", msg.trim())
}
pub struct Response {
pub status: u16,
pub status_line: String,
pub headers: Vec<(String, String)>,
pub body: String,
}
pub struct Listing {
pub items: Vec<Value>,
pub total: Option<u64>,
pub truncated: bool,
}
pub fn limit_arg(limit: Option<usize>, default: usize) -> (usize, bool) {
match limit {
None => (default, false),
Some(0) => (usize::MAX, true),
Some(n) => (n, true),
}
}
pub fn enc(s: &str) -> String {
s.bytes()
.map(|b| match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => (b as char).to_string(),
_ => format!("%{b:02X}"),
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn url_and_enc() {
assert_eq!(url("/user"), "https://api.bitbucket.org/2.0/user");
assert_eq!(url("user"), "https://api.bitbucket.org/2.0/user");
assert_eq!(url("https://x/y"), "https://x/y");
assert_eq!(enc("state=\"OPEN\" a/b{1}"), "state%3D%22OPEN%22%20a%2Fb%7B1%7D");
}
#[test]
fn limit_args() {
assert_eq!(limit_arg(None, 30), (30, false));
assert_eq!(limit_arg(Some(5), 30), (5, true));
assert_eq!(limit_arg(Some(0), 30), (usize::MAX, true));
}
}