mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-01 19:42:27 +08:00
feat(config): add API route set_config (#1144)
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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))
|
||||
|
||||
@ -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>,
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -176,6 +176,8 @@ pub enum Flow {
|
||||
RetrieveFile,
|
||||
/// Dispute Evidence submission flow
|
||||
DisputesEvidenceSubmit,
|
||||
/// Create Config Key flow
|
||||
CreateConfigKey,
|
||||
/// Attach Dispute Evidence flow
|
||||
AttachDisputeEvidence,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user