aditjind_crate/
client.rs

1/*
2 * Licensed to Elasticsearch B.V. under one or more contributor
3 * license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright
5 * ownership. Elasticsearch B.V. licenses this file to you under
6 * the Apache License, Version 2.0 (the "License"); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *	http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied.  See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/*
21 * SPDX-License-Identifier: Apache-2.0
22 *
23 * The OpenSearch Contributors require contributions made to
24 * this file be licensed under the Apache-2.0 license or a
25 * compatible open source license.
26 *
27 * Modifications Copyright OpenSearch Contributors. See
28 * GitHub history for details.
29 */
30
31use crate::{
32    http::{headers::HeaderMap, request::Body, response::Response, transport::Transport, Method},
33    Error,
34};
35
36use serde::{Serialize, Serializer};
37use std::time::Duration;
38
39/// Serializes an `Option<&[Serialize]>` with
40/// `Some(value)` to a comma separated string of values.
41/// Used to serialize values within the query string
42pub(crate) fn serialize_coll_qs<S, T>(
43    value: &Option<&[T]>,
44    serializer: S,
45) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
46where
47    S: Serializer,
48    T: Serialize,
49{
50    let vec = value.expect("attempt to serialize Option::None value");
51
52    // TODO: There must be a better way of serializing a Vec<Serialize> to a comma-separated url encoded string...
53    // (mis)use serde_json to_string and trim the surrounding quotes...
54    let serialized = vec
55        .iter()
56        .map(|v| serde_json::to_string(v).unwrap())
57        .collect::<Vec<_>>();
58
59    let target = serialized
60        .iter()
61        .map(|s| s.trim_matches('"'))
62        .collect::<Vec<_>>()
63        .join(",");
64
65    serializer.serialize_str(&target)
66}
67
68/// Root client for top level APIs
69#[derive(Clone, Debug, Default)]
70pub struct OpenSearch {
71    transport: Transport,
72}
73
74impl OpenSearch {
75    /// Creates a new instance of the root client
76    pub fn new(transport: Transport) -> Self {
77        OpenSearch { transport }
78    }
79
80    /// Gets the transport of the client
81    pub fn transport(&self) -> &Transport {
82        &self.transport
83    }
84
85    /// Creates an asynchronous request that can be awaited
86    ///
87    /// Accepts the HTTP method and relative path to an API,
88    /// and optional query string and body.
89    pub async fn send<B, Q>(
90        &self,
91        method: Method,
92        path: &str,
93        headers: HeaderMap,
94        query_string: Option<&Q>,
95        body: Option<B>,
96        timeout: Option<Duration>,
97    ) -> Result<Response, Error>
98    where
99        B: Body,
100        Q: Serialize + ?Sized,
101    {
102        self.transport
103            .send(method, path, headers, query_string, body, timeout)
104            .await
105    }
106}