use auth::ApiKey;
use cx_api::proto::*;
use error::SdkError;
use http::{
HeaderMap,
HeaderName,
HeaderValue,
};
use std::{
fmt::Debug,
str::FromStr,
};
pub mod auth;
pub mod client;
pub mod error;
mod util;
mod metadata;
const ENV_CORALOGIX_REGION: &str = "CORALOGIX_REGION";
const AUTHORIZATION_HEADER_NAME: &str = "authorization";
const SDK_VERSION_HEADER_NAME: &str = "x-cx-sdk-version";
const SDK_LANGUAGE_HEADER_NAME: &str = "x-cx-sdk-language";
const SDK_RUSTC_VERSION_HEADER_NAME: &str = "x-cx-sdk-rustc-version";
const SDK_CORRELATION_ID_HEADER_NAME: &str = "x-cx-sdk-correlation-id";
const RUSTC_VERSION: &str = env!("RUSTC_VERSION");
pub const SDK_VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Debug, Clone)]
pub enum CoralogixRegion {
US1,
US2,
EU1,
EU2,
AP1,
AP2,
AP3,
Custom(String),
}
impl CoralogixRegion {
pub fn grpc_endpoint(&self) -> String {
match self {
CoralogixRegion::US1 => "https://ng-api-grpc.coralogix.us".into(),
CoralogixRegion::US2 => "https://ng-api-grpc.cx498.coralogix.com".into(),
CoralogixRegion::EU1 => "https://ng-api-grpc.coralogix.com".into(),
CoralogixRegion::EU2 => "https://ng-api-grpc.eu2.coralogix.com".into(),
CoralogixRegion::AP1 => "https://ng-api-grpc.app.coralogix.in".into(),
CoralogixRegion::AP2 => "https://ng-api-grpc.coralogixsg.com".into(),
CoralogixRegion::AP3 => "https://ng-api-grpc.ap3.coralogix.com".into(),
CoralogixRegion::Custom(custom) => format!("https://{}", custom),
}
}
pub fn rest_endpoint(&self) -> String {
match self {
CoralogixRegion::US1 => "https://ng-api-http.coralogix.us".into(),
CoralogixRegion::US2 => "https://ng-api-http.cx498.coralogix.com".into(),
CoralogixRegion::EU1 => "https://ng-api-http.coralogix.com".into(),
CoralogixRegion::EU2 => "https://ng-api-http.eu2.coralogix.com".into(),
CoralogixRegion::AP1 => "https://ng-api-http.app.coralogix.in".into(),
CoralogixRegion::AP2 => "https://ng-api-http.coralogixsg.com".into(),
CoralogixRegion::AP3 => "https://ng-api-http.ap3.coralogix.com".into(),
CoralogixRegion::Custom(custom) => {
format!("https://{}", custom.replace("grpc", "http"))
}
}
}
pub fn from_env() -> Result<Self, SdkError> {
let region = std::env::var(ENV_CORALOGIX_REGION)?;
Self::from_str(®ion)
}
}
impl FromStr for CoralogixRegion {
type Err = SdkError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"us1" => Ok(CoralogixRegion::US1),
"us2" => Ok(CoralogixRegion::US2),
"eu1" => Ok(CoralogixRegion::EU1),
"eu2" => Ok(CoralogixRegion::EU2),
"ap1" => Ok(CoralogixRegion::AP1),
"ap2" => Ok(CoralogixRegion::AP2),
"ap3" => Ok(CoralogixRegion::AP3),
custom => Ok(CoralogixRegion::Custom(custom.to_string())),
}
}
}
impl From<&ApiKey> for metadata::CallProperties {
fn from(value: &ApiKey) -> Self {
let value = bytes::Bytes::from(format!("Bearer {}", value.token()));
let mut value = HeaderValue::from_maybe_shared(value).unwrap();
value.set_sensitive(true);
metadata::CallProperties::new(HeaderMap::from_iter(
[
(HeaderName::from_static(AUTHORIZATION_HEADER_NAME), value),
(
HeaderName::from_static(SDK_RUSTC_VERSION_HEADER_NAME),
HeaderValue::from_str(RUSTC_VERSION).unwrap(),
),
(
HeaderName::from_static(SDK_VERSION_HEADER_NAME),
HeaderValue::from_str(SDK_VERSION).unwrap(),
),
(
HeaderName::from_static(SDK_LANGUAGE_HEADER_NAME),
HeaderValue::from_static("rust"),
),
(
HeaderName::from_static(SDK_CORRELATION_ID_HEADER_NAME),
HeaderValue::from_str(&uuid::Uuid::new_v4().to_string()).unwrap(),
),
]
.iter()
.cloned(),
))
}
}