1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
// Copyright (C) 2019 O.S. Systems Sofware LTDA
//
// SPDX-License-Identifier: Apache-2.0
use crate::{api, Error, Result};
use reqwest::StatusCode;
use std::path::Path;
/// The `Client` allow for requests to be sent.
#[derive(Clone)]
pub struct Client {
server_address: String,
client: reqwest::Client,
}
impl Default for Client {
fn default() -> Self {
Client {
server_address: "http://localhost:8080".to_string(),
client: reqwest::Client::new(),
}
}
}
impl Client {
/// Constructs a new `Client`.
pub fn new(server_address: &str) -> Self {
Client { server_address: format!("http://{}", server_address), ..Self::default() }
}
/// Get the current state of the agent.
/// # Example
///
/// ```no_run
/// # async fn run() -> updatehub_sdk::Result<()> {
/// let client = updatehub_sdk::Client::default();
/// let response = client.info().await?;
/// # Ok(()) }
/// ```
///
/// # Errors
///
/// This method fails when cannot complete the request at the address or
/// cannot parse the body json as a `info::Response`.
pub async fn info(&self) -> Result<api::info::Response> {
let response = self.client.get(&format!("{}/info", self.server_address)).send().await?;
match response.status() {
StatusCode::OK => Ok(response.json().await?),
s => Err(Error::UnexpectedResponse(s)),
}
}
/// Probe the agent for update.
/// # Example
///
/// ```no_run
/// # async fn run() -> updatehub_sdk::Result<()> {
/// let client = updatehub_sdk::Client::default();
/// let response = client.probe(None).await?;
/// # Ok(()) }
/// ```
///
/// A **custom** address can be used:
///
/// ```no_run
/// # async fn run() -> updatehub_sdk::Result<()> {
/// let client = updatehub_sdk::Client::default();
/// let response = client.probe(Some("http://foo.bar".to_string())).await?;
/// # Ok(()) }
/// ```
///
/// # Errors
///
/// This method fails when cannot complete the request at the address or
/// cannot parse the body json as a `probe::Response`.
pub async fn probe(&self, custom: Option<String>) -> Result<api::probe::Response> {
let request = self.client.post(&format!("{}/probe", self.server_address));
let response = match custom {
Some(custom_server) => request.json(&api::probe::Request { custom_server }),
None => request,
}
.send()
.await?;
match response.status() {
StatusCode::OK => Ok(response.json().await?),
StatusCode::NOT_ACCEPTABLE => Err(Error::AgentIsBusy(response.json().await?)),
s => Err(Error::UnexpectedResponse(s)),
}
}
/// Request agent to install a local update package passing a path as
/// argument.
/// # Example
///
/// ```no_run
/// # async fn run() -> updatehub_sdk::Result<()> {
/// let path = std::path::Path::new("/tmp/my-update-package.uhupkg");
///
/// let client = updatehub_sdk::Client::default();
/// let response = client.local_install(path).await?;
/// # Ok(()) }
/// ```
///
/// # Errors
///
/// This method fails when cannot complete the request at the address or
/// cannot parse the body json as a `state::Response`.
pub async fn local_install(&self, file: &Path) -> Result<api::state::Response> {
let response = self
.client
.post(&format!("{}/local_install", self.server_address))
.json(&api::local_install::Request { file: file.to_owned() })
.send()
.await?;
match response.status() {
StatusCode::OK => Ok(response.json().await?),
StatusCode::NOT_ACCEPTABLE => Err(Error::AgentIsBusy(response.json().await?)),
s => Err(Error::UnexpectedResponse(s)),
}
}
/// Request agent to install a package from a URL.
/// # Example
///
/// ```no_run
/// # async fn run() -> updatehub_sdk::Result<()> {
/// let client = updatehub_sdk::Client::default();
/// let response = client.remote_install("http://foo.bar").await?;
/// # Ok(()) }
/// ```
///
/// # Errors
///
/// This method fails when cannot complete the request at the address or
/// cannot parse the body json as a `state::Response`.
pub async fn remote_install(&self, url: &str) -> Result<api::state::Response> {
let response = self
.client
.post(&format!("{}/remote_install", self.server_address))
.json(&api::remote_install::Request { url: url.to_owned() })
.send()
.await?;
match response.status() {
StatusCode::OK => Ok(response.json().await?),
s => Err(Error::UnexpectedResponse(s)),
}
}
/// Tells agent to abort the current download.
/// # Example
///
/// ```no_run
/// # async fn run() -> updatehub_sdk::Result<()> {
/// let client = updatehub_sdk::Client::default();
/// let response = client.abort_download().await?;
/// # Ok(()) }
/// ```
///
/// # Errors
///
/// This method fails when cannot complete the request at the address or
/// cannot parse the body json as a `state::Response`.
pub async fn abort_download(&self) -> Result<api::state::Response> {
let response = self
.client
.post(&format!("{}/update/download/abort", self.server_address))
.send()
.await?;
match response.status() {
StatusCode::OK => Ok(response.json().await?),
StatusCode::NOT_ACCEPTABLE => Err(Error::AbortDownloadRefused(response.json().await?)),
s => Err(Error::UnexpectedResponse(s)),
}
}
/// Get the available log entries for the last update.
/// # Example
///
/// ```no_run
/// # async fn run() -> updatehub_sdk::Result<()> {
/// let client = updatehub_sdk::Client::default();
/// let response = client.log().await?;
/// # Ok(()) }
/// ```
///
/// # Errors
///
/// This method fails when cannot complete the request at the address or
/// cannot parse the body json as a `log::Log`.
pub async fn log(&self) -> Result<api::log::Log> {
let response = self.client.get(&format!("{}/log", self.server_address)).send().await?;
match response.status() {
StatusCode::OK => Ok(response.json().await?),
s => Err(Error::UnexpectedResponse(s)),
}
}
}