#![allow(clippy::field_reassign_with_default)]
use std::str;
use futures::{future::BoxFuture, FutureExt};
use http::Response;
use hyper::Body;
use once_cell::sync::Lazy;
use schemars::JsonSchema;
use semver::Version;
use serde::{Deserialize, Serialize};
use tracing::info;
use warp_json_rpc::Builder;
use super::{docs::DocExample, Error, ReactorEventT, RpcRequest, RpcWithParams, RpcWithParamsExt};
use crate::{
components::{rpc_server::rpcs::ErrorCode, CLIENT_API_VERSION},
effect::EffectBuilder,
reactor::QueueKind,
types::{Deploy, DeployHash},
};
static PUT_DEPLOY_PARAMS: Lazy<PutDeployParams> = Lazy::new(|| PutDeployParams {
deploy: Deploy::doc_example().clone(),
});
static PUT_DEPLOY_RESULT: Lazy<PutDeployResult> = Lazy::new(|| PutDeployResult {
api_version: CLIENT_API_VERSION.clone(),
deploy_hash: *Deploy::doc_example().id(),
});
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PutDeployParams {
pub deploy: Deploy,
}
impl DocExample for PutDeployParams {
fn doc_example() -> &'static Self {
&*PUT_DEPLOY_PARAMS
}
}
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct PutDeployResult {
#[schemars(with = "String")]
pub api_version: Version,
pub deploy_hash: DeployHash,
}
impl DocExample for PutDeployResult {
fn doc_example() -> &'static Self {
&*PUT_DEPLOY_RESULT
}
}
pub struct PutDeploy {}
impl RpcWithParams for PutDeploy {
const METHOD: &'static str = "account_put_deploy";
type RequestParams = PutDeployParams;
type ResponseResult = PutDeployResult;
}
impl RpcWithParamsExt for PutDeploy {
fn handle_request<REv: ReactorEventT>(
effect_builder: EffectBuilder<REv>,
response_builder: Builder,
params: Self::RequestParams,
) -> BoxFuture<'static, Result<Response<Body>, Error>> {
async move {
let deploy_hash = *params.deploy.id();
let put_deploy_result = effect_builder
.make_request(
|responder| RpcRequest::SubmitDeploy {
deploy: Box::new(params.deploy),
responder,
},
QueueKind::Api,
)
.await;
match put_deploy_result {
Ok(_) => {
info!(%deploy_hash,
"deploy was stored"
);
let result = Self::ResponseResult {
api_version: CLIENT_API_VERSION.clone(),
deploy_hash,
};
Ok(response_builder.success(result)?)
}
Err(_) => {
info!(
%deploy_hash,
"the deploy submitted by the client was invalid",
);
Ok(response_builder.error(warp_json_rpc::Error::custom(
ErrorCode::InvalidDeploy as i64,
"invalid deploy",
))?)
}
}
}
.boxed()
}
}