cx_sdk/client/dataprime_query.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
// Copyright 2024 Coralogix Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::error::{
SdkApiError,
SdkError,
};
use crate::{
CoralogixRegion,
auth::AuthContext,
error::Result,
metadata::CallProperties,
util::make_request_with_metadata,
};
use cx_api::proto::com::coralogixapis::dataprime::v1::{
QueryRequest,
QueryResponse,
dataprime_query_service_client::DataprimeQueryServiceClient,
};
use std::str::FromStr;
use tokio::sync::Mutex;
use tonic::{
Streaming,
transport::ClientTlsConfig,
};
use tonic::{
metadata::MetadataMap,
transport::{
Channel,
Endpoint,
},
};
pub use cx_api::proto::com::coralogixapis::dataprime::v1::Metadata;
pub use cx_api::proto::com::coralogixapis::dataprime::v1::query_response::Message;
const DATAPRIME_FEATURE_GROUP_ID: &str = "dataprime";
/// The Dataprime Query API client.
/// Read more at [https://coralogix.com/docs/dataprime-query-language/]()
pub struct DataprimeQueryClient {
metadata_map: MetadataMap,
service_client: Mutex<DataprimeQueryServiceClient<Channel>>,
}
impl DataprimeQueryClient {
/// Creates a new client for the Dataprime Query API.
///
/// # Arguments
/// * `auth_context` - The to use for authentication.
/// * `region` - The region to connect to.
pub fn new(region: CoralogixRegion, auth_context: AuthContext) -> Result<Self> {
let enrichments_service_channel: Channel = Endpoint::from_str(®ion.grpc_endpoint())?
.tls_config(ClientTlsConfig::new().with_native_roots())?
.connect_lazy();
let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
Ok(Self {
metadata_map: request_metadata.to_metadata_map(),
service_client: Mutex::new(DataprimeQueryServiceClient::new(
enrichments_service_channel.clone(),
)),
})
}
/// Runs a dataprime query.
///
/// # Arguments
/// * `query` - The query to run.
pub async fn run(
&self,
query: String,
metadata: Option<Metadata>,
) -> Result<Streaming<QueryResponse>> {
let request = make_request_with_metadata(
QueryRequest {
query: Some(query),
metadata,
},
&self.metadata_map,
);
{
let mut client = self.service_client.lock().await;
Ok(client
.query(request)
.await
.map_err(|status| {
SdkError::ApiError(SdkApiError {
status,
endpoint: "/com.coralogixapis.dataprime.v1.DataprimeQueryService/Query"
.into(),
feature_group: DATAPRIME_FEATURE_GROUP_ID.into(),
})
})?
.into_inner())
}
}
}