cx_sdk/
lib.rs

1// Copyright 2024 Coralogix Ltd.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![allow(clippy::result_large_err)]
16#![allow(renamed_and_removed_lints)]
17
18//! The Rust SDK for Coralogix  APIs.
19use 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
32/// This module contains the authentication primitives for the SDK.
33pub mod auth;
34
35/// This module contains the clients for the underlying Coralogix APIs.
36pub mod client;
37
38/// This module contains the error types for the SDK.
39pub mod error;
40
41/// This module contains the utility functions for the SDK.
42mod 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
54/// The SDK version.
55pub const SDK_VERSION: &str = env!("CARGO_PKG_VERSION");
56
57/// From <https://coralogix.com/docs/coralogix-domain/>
58#[derive(Debug, Clone)]
59pub enum CoralogixRegion {
60    /// US1 region. Associated to the endpoint `https://ng-api-grpc.coralogix.com`
61    US1,
62    /// US2 region. Associated to the endpoint `https://ng-api-grpc.cx498.coralogix.com`
63    US2,
64    /// EU1 region. Associated to the endpoint `https://ng-api-grpc.coralogix.com`
65    EU1,
66    /// EU2 region. Associated to the endpoint `https://ng-api-grpc.eu2.coralogix.com`
67    EU2,
68    /// AP1 region. Associated to the endpoint `https://ng-api-grpc.app.coralogix.in`
69    AP1,
70    /// AP2 region. Associated to the endpoint `https://ng-api-grpc.coralogixsg.com`
71    AP2,
72    /// AP3 region. Associated to the endpoint `https://ng-api-grpc.ap3.coralogix.com`
73    AP3,
74    /// Custom region. It's associated with a custom endpoint.
75    Custom(String),
76}
77
78impl CoralogixRegion {
79    /// gRPC endpoint for the corresponding Coralogix region
80    /// <https://coralogix.com/docs/coralogix-domain/>
81    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    /// REST endpoint for the corresponding Coralogix region
95    /// <https://coralogix.com/docs/coralogix-domain/>
96    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    /// Creates a CoralogixRegion from the environment variable `ENV_CORALOGIX_REGION`.
110    pub fn from_env() -> Result<Self, SdkError> {
111        let region = std::env::var(ENV_CORALOGIX_REGION)?;
112        Self::from_str(&region)
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}