refactor(router): Introduce ApiKeyId id type (#6324)

This commit is contained in:
Anurag Thakur
2024-10-21 19:19:31 +05:30
committed by GitHub
parent 58296ffae6
commit b3ce373f8e
14 changed files with 166 additions and 75 deletions

View File

@ -45,6 +45,9 @@ pub enum ApiEventsType {
BusinessProfile {
profile_id: id_type::ProfileId,
},
ApiKey {
key_id: id_type::ApiKeyId,
},
User {
user_id: String,
},
@ -130,10 +133,6 @@ impl_api_event_type!(
(
String,
id_type::MerchantId,
(id_type::MerchantId, String),
(id_type::MerchantId, &String),
(&id_type::MerchantId, &String),
(&String, &String),
(Option<i64>, Option<i64>, String),
(Option<i64>, Option<i64>, id_type::MerchantId),
bool

View File

@ -3,6 +3,7 @@
use std::{borrow::Cow, fmt::Debug};
mod api_key;
mod customer;
mod merchant;
mod merchant_connector_account;
@ -14,6 +15,7 @@ mod routing;
#[cfg(feature = "v2")]
mod global_id;
pub use api_key::ApiKeyId;
pub use customer::CustomerId;
use diesel::{
backend::Backend,

View File

@ -0,0 +1,46 @@
crate::id_type!(
ApiKeyId,
"A type for key_id that can be used for API key IDs"
);
crate::impl_id_type_methods!(ApiKeyId, "key_id");
// This is to display the `ApiKeyId` as ApiKeyId(abcd)
crate::impl_debug_id_type!(ApiKeyId);
crate::impl_try_from_cow_str_id_type!(ApiKeyId, "key_id");
crate::impl_serializable_secret_id_type!(ApiKeyId);
crate::impl_queryable_id_type!(ApiKeyId);
crate::impl_to_sql_from_sql_id_type!(ApiKeyId);
impl ApiKeyId {
/// Generate Api Key Id from prefix
pub fn generate_key_id(prefix: &'static str) -> Self {
Self(crate::generate_ref_id_with_default_length(prefix))
}
}
impl crate::events::ApiEventMetric for ApiKeyId {
fn get_api_event_type(&self) -> Option<crate::events::ApiEventsType> {
Some(crate::events::ApiEventsType::ApiKey {
key_id: self.clone(),
})
}
}
impl crate::events::ApiEventMetric for (super::MerchantId, ApiKeyId) {
fn get_api_event_type(&self) -> Option<crate::events::ApiEventsType> {
Some(crate::events::ApiEventsType::ApiKey {
key_id: self.1.clone(),
})
}
}
impl crate::events::ApiEventMetric for (&super::MerchantId, &ApiKeyId) {
fn get_api_event_type(&self) -> Option<crate::events::ApiEventsType> {
Some(crate::events::ApiEventsType::ApiKey {
key_id: self.1.clone(),
})
}
}
crate::impl_default_id_type!(ApiKeyId, "key");