1#![allow(clippy::result_large_err)]
16#![allow(renamed_and_removed_lints)]
17
18use auth::ApiKey;
20use cx_api::proto::*;
21use error::SdkError;
22use http::{
23 HeaderMap,
24 HeaderName,
25 HeaderValue,
26};
27use std::{
28 fmt::Debug,
29 str::FromStr,
30};
31
32pub mod auth;
34
35pub mod client;
37
38pub mod error;
40
41mod util;
43
44mod metadata;
45
46const ENV_CORALOGIX_REGION: &str = "CORALOGIX_REGION";
47const AUTHORIZATION_HEADER_NAME: &str = "authorization";
48const SDK_VERSION_HEADER_NAME: &str = "x-cx-sdk-version";
49const SDK_LANGUAGE_HEADER_NAME: &str = "x-cx-sdk-language";
50const SDK_RUSTC_VERSION_HEADER_NAME: &str = "x-cx-sdk-rustc-version";
51const SDK_CORRELATION_ID_HEADER_NAME: &str = "x-cx-sdk-correlation-id";
52const RUSTC_VERSION: &str = env!("RUSTC_VERSION");
53
54pub const SDK_VERSION: &str = env!("CARGO_PKG_VERSION");
56
57#[derive(Debug, Clone)]
59pub enum CoralogixRegion {
60 US1,
62 US2,
64 EU1,
66 EU2,
68 AP1,
70 AP2,
72 AP3,
74 Custom(String),
76}
77
78impl CoralogixRegion {
79 pub fn grpc_endpoint(&self) -> String {
82 match self {
83 CoralogixRegion::US1 => "https://ng-api-grpc.coralogix.us".into(),
84 CoralogixRegion::US2 => "https://ng-api-grpc.cx498.coralogix.com".into(),
85 CoralogixRegion::EU1 => "https://ng-api-grpc.coralogix.com".into(),
86 CoralogixRegion::EU2 => "https://ng-api-grpc.eu2.coralogix.com".into(),
87 CoralogixRegion::AP1 => "https://ng-api-grpc.app.coralogix.in".into(),
88 CoralogixRegion::AP2 => "https://ng-api-grpc.coralogixsg.com".into(),
89 CoralogixRegion::AP3 => "https://ng-api-grpc.ap3.coralogix.com".into(),
90 CoralogixRegion::Custom(custom) => format!("https://ng-api-grpc.{custom}:443"),
91 }
92 }
93
94 pub fn rest_endpoint(&self) -> String {
97 match self {
98 CoralogixRegion::US1 => "https://ng-api-http.coralogix.us".into(),
99 CoralogixRegion::US2 => "https://ng-api-http.cx498.coralogix.com".into(),
100 CoralogixRegion::EU1 => "https://ng-api-http.coralogix.com".into(),
101 CoralogixRegion::EU2 => "https://ng-api-http.eu2.coralogix.com".into(),
102 CoralogixRegion::AP1 => "https://ng-api-http.app.coralogix.in".into(),
103 CoralogixRegion::AP2 => "https://ng-api-http.coralogixsg.com".into(),
104 CoralogixRegion::AP3 => "https://ng-api-http.ap3.coralogix.com".into(),
105 CoralogixRegion::Custom(custom) => format!("https://ng-api-http.{custom}"),
106 }
107 }
108
109 pub fn from_env() -> Result<Self, SdkError> {
111 let region = std::env::var(ENV_CORALOGIX_REGION)?;
112 Self::from_str(®ion)
113 }
114}
115
116impl FromStr for CoralogixRegion {
117 type Err = SdkError;
118
119 fn from_str(s: &str) -> Result<Self, Self::Err> {
120 match s.to_lowercase().as_str() {
121 "us1" => Ok(CoralogixRegion::US1),
122 "us2" => Ok(CoralogixRegion::US2),
123 "eu1" => Ok(CoralogixRegion::EU1),
124 "eu2" => Ok(CoralogixRegion::EU2),
125 "ap1" => Ok(CoralogixRegion::AP1),
126 "ap2" => Ok(CoralogixRegion::AP2),
127 "ap3" => Ok(CoralogixRegion::AP3),
128 custom => Ok(CoralogixRegion::Custom(custom.to_string())),
129 }
130 }
131}
132
133impl From<&ApiKey> for metadata::CallProperties {
134 fn from(value: &ApiKey) -> Self {
135 let value = bytes::Bytes::from(format!("Bearer {}", value.token()));
136 let mut value = HeaderValue::from_maybe_shared(value).unwrap();
137 value.set_sensitive(true);
138 metadata::CallProperties::new(HeaderMap::from_iter(
139 [
140 (HeaderName::from_static(AUTHORIZATION_HEADER_NAME), value),
141 (
142 HeaderName::from_static(SDK_RUSTC_VERSION_HEADER_NAME),
143 HeaderValue::from_str(RUSTC_VERSION).unwrap(),
144 ),
145 (
146 HeaderName::from_static(SDK_VERSION_HEADER_NAME),
147 HeaderValue::from_str(format!("sdk-{SDK_VERSION}").as_str()).unwrap(),
148 ),
149 (
150 HeaderName::from_static(SDK_LANGUAGE_HEADER_NAME),
151 HeaderValue::from_static("rust"),
152 ),
153 (
154 HeaderName::from_static(SDK_CORRELATION_ID_HEADER_NAME),
155 HeaderValue::from_str(&uuid::Uuid::new_v4().to_string()).unwrap(),
156 ),
157 ]
158 .iter()
159 .cloned(),
160 ))
161 }
162}