oci_sdk/
nosql.rs

1use crate::base_client::{encode_body, oci_signer};
2use crate::config::AuthConfig;
3use chrono::{DateTime, Utc};
4use reqwest::header::HeaderMap;
5use reqwest::Response;
6use serde_json::json;
7
8pub struct QueryDetails {
9    pub compartment_id: String,
10    pub statement: String,
11}
12
13pub struct Nosql {
14    config: AuthConfig,
15    service_endpoint: String,
16}
17
18pub struct TableLimits {
19    pub max_read_units: u16,
20    pub max_write_units: u16,
21    pub max_storage_in_g_bs: u16,
22}
23
24pub struct CreateTableDetails {
25    pub name: String,
26    pub compartment_id: String,
27    pub ddl_statement: String,
28    pub table_limits: TableLimits,
29}
30
31impl Nosql {
32    ///Creates a new `Nosql` which is the client necessary to interact with this type of object on OCI.
33    ///
34    ///## Example 1
35    ///```no_run
36    ///use oci_sdk::{
37    ///    config::AuthConfig,
38    ///    nosql::{Nosql},
39    ///};
40    ///
41    ///let auth_config = AuthConfig::from_file(None, None);
42    ///let nosql = Nosql::new(auth_config, None);
43    ///```
44    ///
45    /// ## Example 2
46    ///
47    ///```rust
48    ///use oci_sdk::{
49    ///    config::AuthConfig,
50    ///    nosql::{Nosql},
51    ///};
52    ///
53    ///let auth_config = AuthConfig::from_file(Some("tests/assets/oci_config".to_string()), Some("DEFAULT".to_string()));
54    ///let nosql = Nosql::new(auth_config, None);
55    ///```
56    ///Returns the Nosql client.
57    pub fn new(config: AuthConfig, service_endpoint: Option<String>) -> Nosql {
58        let se = service_endpoint.unwrap_or(format!(
59            "https://nosql.{}.oci.oraclecloud.com",
60            config.region
61        ));
62        return Nosql {
63            config,
64            service_endpoint: se,
65        };
66    }
67
68    pub async fn create_table(
69        &self,
70        create_table_detais: CreateTableDetails,
71    ) -> Result<Response, Box<dyn std::error::Error + Send + Sync>> {
72        let client = reqwest::Client::new();
73
74        let mut headers = HeaderMap::new();
75
76        let body_json = json!({
77          "name": &create_table_detais.name,
78          "compartmentId": &create_table_detais.compartment_id,
79          "ddlStatement": &create_table_detais.ddl_statement,
80          "tableLimits": {
81            "maxReadUnits": create_table_detais.table_limits.max_read_units,
82            "maxWriteUnits": create_table_detais.table_limits.max_write_units,
83            "maxStorageInGBs": create_table_detais.table_limits.max_storage_in_g_bs
84          }
85        });
86
87        let body = body_json.to_string();
88
89        let now: DateTime<Utc> = Utc::now();
90        headers.insert(
91            "date",
92            now.to_rfc2822().replace("+0000", "GMT").parse().unwrap(),
93        );
94        headers.insert("x-content-sha256", encode_body(&body).parse().unwrap());
95        headers.insert("content-length", body.len().to_string().parse().unwrap());
96        headers.insert(
97            "content-type",
98            String::from("application/json").parse().unwrap(),
99        );
100
101        let path = format!("/20190828/tables");
102
103        oci_signer(
104            &self.config,
105            &mut headers,
106            String::from("post"),
107            &path,
108            &self.service_endpoint,
109        );
110
111        let response = client
112            .post(format!("{}{}", self.service_endpoint, path))
113            .body(body)
114            .headers(headers)
115            .send()
116            .await?;
117
118        return Ok(response);
119    }
120
121    pub async fn query(
122        &self,
123        query_details: QueryDetails,
124        limit: u16,
125    ) -> Result<Response, Box<dyn std::error::Error + Send + Sync>> {
126        let client = reqwest::Client::new();
127
128        let mut headers = HeaderMap::new();
129
130        let body_json = json!({
131          "compartmentId": &query_details.compartment_id,
132          "statement": &query_details.statement,
133        });
134
135        let body = body_json.to_string();
136
137        let now: DateTime<Utc> = Utc::now();
138        headers.insert(
139            "date",
140            now.to_rfc2822().replace("+0000", "GMT").parse().unwrap(),
141        );
142        headers.insert("x-content-sha256", encode_body(&body).parse().unwrap());
143        headers.insert("content-length", body.len().to_string().parse().unwrap());
144        headers.insert(
145            "content-type",
146            String::from("application/json").parse().unwrap(),
147        );
148
149        let path = format!("/20190828/query?limit={}", limit);
150
151        oci_signer(
152            &self.config,
153            &mut headers,
154            String::from("post"),
155            &path,
156            &self.service_endpoint,
157        );
158
159        let response = client
160            .post(format!("{}{}", self.service_endpoint, path))
161            .body(body)
162            .headers(headers)
163            .send()
164            .await?;
165
166        return Ok(response);
167    }
168}