Skip to content

Commit

Permalink
fix snapshot client api
Browse files Browse the repository at this point in the history
  • Loading branch information
dkijania committed Jun 12, 2021
1 parent fa0bac5 commit 7d30761
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion snapshot-trigger-service/src/client/args.rs
Expand Up @@ -86,7 +86,7 @@ pub struct StatusCommand {
}

impl StatusCommand {
pub fn exec(self, rest: SnapshotRestClient) -> Result<State, Error> {
pub fn exec(self, rest: SnapshotRestClient) -> Result<Result<State,crate::context::Error>, Error> {
rest.job_status(self.job_id).map_err(Into::into)
}
}
Expand Down
27 changes: 12 additions & 15 deletions snapshot-trigger-service/src/client/rest.rs
Expand Up @@ -2,7 +2,6 @@ use crate::config::JobParameters;
use crate::context::State;
use crate::file_lister::FolderDump;
use jortestkit::{prelude::Wait, process::WaitError};
use reqwest::blocking::Response;
use std::io::Write;
use std::path::Path;
use thiserror::Error;
Expand Down Expand Up @@ -39,14 +38,6 @@ impl SnapshotRestClient {
format!("{}/{}", self.address, path.into())
}

fn post<S: Into<String>>(&self, local_path: S) -> Result<Response, Error> {
let path = self.path(local_path);
println!("Calling: {}", path);
let client = reqwest::blocking::Client::new();
let request = self.set_header(client.post(&path));
request.send().map_err(Into::into)
}

fn get<S: Into<String>>(&self, local_path: S) -> Result<String, Error> {
let path = self.path(local_path);
println!("Calling: {}", path);
Expand Down Expand Up @@ -98,15 +89,19 @@ impl SnapshotRestClient {

pub fn job_new(&self, params: JobParameters) -> Result<String, Error> {
let client = reqwest::blocking::Client::new();
let request = self.set_header(client.post("api/job/new"));
request.json(&params).send()?.text().map_err(Into::into)
let path = self.path("api/job/new");
println!("Calling: {}", path);
let request = self.set_header(client.post(&path));
request.json(&params).send()?.text().map_err(Into::into).map(|text| text.replace("\"",""))
}

pub fn job_status<S: Into<String>>(&self, id: S) -> Result<State, Error> {
let content = self.post(format!("api/job/status/{}", id.into()))?.text()?;
pub fn job_status<S: Into<String>>(&self, id: S) -> Result<Result<State,crate::context::Error>, Error> {
let content = self.get(format!("api/job/status/{}", id.into()))?;
serde_yaml::from_str(&content).map_err(Into::into)
}



pub fn wait_for_job_finish<S: Into<String>>(
&self,
id: S,
Expand All @@ -115,8 +110,10 @@ impl SnapshotRestClient {
let job_id = id.into();
loop {
let response = self.job_status(job_id.clone())?;
if let State::Finished { .. } = response {
return Ok(response);
if let Ok(response) = response {
if let State::Finished { .. } = response {
return Ok(response);
}
}
wait.check_timeout()?;
wait.advance();
Expand Down
2 changes: 1 addition & 1 deletion snapshot-trigger-service/src/lib.rs
Expand Up @@ -7,4 +7,4 @@ pub mod rest;
pub mod service;

pub use args::{Error, TriggerServiceCommand};
pub use context::Context;
pub use context::{Context,State};

0 comments on commit 7d30761

Please sign in to comment.