feat(config): add API route set_config (#1144)

This commit is contained in:
Nishant Joshi
2023-05-15 15:34:49 +05:30
committed by GitHub
parent 54ff02d9dd
commit f31926b833
5 changed files with 42 additions and 1 deletions

View File

@ -1,3 +1,5 @@
use error_stack::ResultExt;
use crate::{
core::errors::{self, utils::StorageErrorExt, RouterResponse},
db::StorageInterface,
@ -5,6 +7,22 @@ use crate::{
types::{api, transformers::ForeignInto},
};
pub async fn set_config(
store: &dyn StorageInterface,
config: api::Config,
) -> RouterResponse<api::Config> {
let config = store
.insert_config(storage_models::configs::ConfigNew {
key: config.key,
config: config.value,
})
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Unknown error, while setting config key")?;
Ok(ApplicationResponse::Json(config.foreign_into()))
}
pub async fn read_config(store: &dyn StorageInterface, key: &str) -> RouterResponse<api::Config> {
let config = store
.find_config_by_key_cached(key)

View File

@ -389,6 +389,7 @@ impl Configs {
pub fn server(config: AppState) -> Scope {
web::scope("/configs")
.app_data(web::Data::new(config))
.service(web::resource("/").route(web::post().to(config_key_create)))
.service(
web::resource("/{key}")
.route(web::get().to(config_key_retrieve))

View File

@ -8,6 +8,26 @@ use crate::{
types::api as api_types,
};
#[instrument(skip_all, fields(flow = ?Flow::CreateConfigKey))]
pub async fn config_key_create(
state: web::Data<AppState>,
req: HttpRequest,
json_payload: web::Json<api_types::Config>,
) -> impl Responder {
let flow = Flow::CreateConfigKey;
let payload = json_payload.into_inner();
api::server_wrap(
flow,
state.get_ref(),
&req,
payload,
|state, _, data| configs::set_config(&*state.store, data),
&auth::AdminApiAuth,
)
.await
}
#[instrument(skip_all, fields(flow = ?Flow::ConfigKeyFetch))]
pub async fn config_key_retrieve(
state: web::Data<AppState>,

View File

@ -1,4 +1,4 @@
#[derive(Clone, serde::Serialize, Debug)]
#[derive(Clone, serde::Serialize, Debug, serde::Deserialize)]
pub struct Config {
pub key: String,
pub value: String,

View File

@ -176,6 +176,8 @@ pub enum Flow {
RetrieveFile,
/// Dispute Evidence submission flow
DisputesEvidenceSubmit,
/// Create Config Key flow
CreateConfigKey,
/// Attach Dispute Evidence flow
AttachDisputeEvidence,
}