Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

general: Use json for requests on remote_install and local_install #267

Merged
merged 1 commit into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 28 additions & 10 deletions doc/agent-http.yaml
Expand Up @@ -65,12 +65,10 @@ paths:
Request the agent for installation of a local package.
requestBody:
required: true
description: "Path for the file to be installed"
content:
text/plain:
schema:
type: string
example: "/tmp/update.uhupkg"
application/json:
schema:
$ref: "#/components/schemas/LocalInstallRequest"
responses:
"200":
description: "Request accepted"
Expand All @@ -92,12 +90,10 @@ paths:
Request the agent for installation of a remote package.
requestBody:
required: true
description: "Remote url that offers the update pacakge"
content:
text/plain:
schema:
type: string
example: "https://some_remote_url.domain/update.uhupkg"
application/json:
schema:
$ref: "#/components/schemas/RemoteInstallRequest"
responses:
"200":
description: "Request accepted"
Expand Down Expand Up @@ -186,6 +182,8 @@ components:
ProbeCustomServer:
description: "Server address which the update procedure will use for this request"
type: object
required:
- custom_server
properties:
custom_server:
type: string
Expand Down Expand Up @@ -222,6 +220,26 @@ components:
type: string
example: "there is no download to be aborted"

LocalInstallRequest:
description: "The update file which will be used for this request"
type: object
required:
- file
properties:
file:
type: string
example: "/tmp/updatehub-image-qa-uh-qemu-x86-64.uhupkg"

RemoteInstallRequest:
description: "URL to directly download the update file which will be used for this request"
type: object
required:
- url
properties:
url:
type: string
example: "https://some_remote_url.domain/update.uhupkg"

AgentInfoSettings:
type: object
required:
Expand Down
24 changes: 18 additions & 6 deletions updatehub-sdk/src/client/mod.rs
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::{api, Error, Result};
use awc::http::{header::CONTENT_TYPE, StatusCode};
use awc::http::StatusCode;
use std::path::Path;

#[derive(Clone)]
Expand Down Expand Up @@ -50,11 +50,17 @@ impl Client {
}

pub async fn local_install(&self, file: &Path) -> Result<api::state::Response> {
use serde::Serialize;
#[derive(Clone, Debug, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Request<'a> {
pub file: &'a Path,
}

let mut response = self
.client
.post(&format!("{}/local_install", self.server_address))
.header(CONTENT_TYPE, "text/plain")
.send_body(format!("{}", file.display()))
.send_json(&Request { file })
.await?;

match response.status() {
Expand All @@ -66,12 +72,18 @@ impl Client {
}
}

pub async fn remote_install(&self, url: String) -> Result<api::state::Response> {
pub async fn remote_install(&self, url: &str) -> Result<api::state::Response> {
use serde::Serialize;
#[derive(Clone, Debug, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Request<'a> {
pub url: &'a str,
}

let mut response = self
.client
.post(&format!("{}/remote_install", self.server_address))
.header(CONTENT_TYPE, "text/plain")
.send_body(url)
.send_json(&Request { url })
.await?;

match response.status() {
Expand Down
2 changes: 1 addition & 1 deletion updatehub-sdk/tests/openapi_integration.rs
Expand Up @@ -88,7 +88,7 @@ async fn remote_install() {
let mock = MockServer::new();
let (addr, _guard) = &mock.start();
let client = sdk::Client::new(&addr);
let response = client.remote_install(String::from("http://foo.bar")).await;
let response = client.remote_install("http://foo.bar").await;
match dbg!(response) {
Ok(_) => {}
Err(sdk::Error::AgentIsBusy(_)) => {}
Expand Down
2 changes: 1 addition & 1 deletion updatehub/src/main.rs
Expand Up @@ -120,7 +120,7 @@ async fn client_main(cmd: ClientCommands) -> updatehub::Result<()> {
println!("{:#?}", client.local_install(&file).await)
}
ClientCommands::RemoteInstall(RemoteInstall { url }) => {
println!("{:#?}", client.remote_install(url).await)
println!("{:#?}", client.remote_install(&url).await)
}
}

Expand Down