stcloud/apis/
monitoring_app_api.rs

1/* 
2 * Sematext Cloud API
3 *
4 * API Explorer provides access and documentation for Sematext REST API. The REST API requires the API Key to be sent as part of `Authorization` header. E.g.: `Authorization : apiKey e5f18450-205a-48eb-8589-7d49edaea813`.
5 *
6 * OpenAPI spec version: v3
7 * 
8 * Generated by: https://github.com/swagger-api/swagger-codegen.git
9 */
10#![allow(unused_imports)]
11use std::sync::Arc;
12use std::borrow::Borrow;
13use std::borrow::Cow;
14use std::collections::HashMap;
15
16use hyper;
17use serde_json;
18use serde_json::Value;
19use tokio::runtime::Runtime;
20use futures;
21use futures::{Future, Stream};
22use bigdecimal::BigDecimal;
23
24use hyper::Body;
25use hyper::body::Bytes;
26use hyper::body::HttpBody;
27use std::str::FromStr;
28use chrono::{Date, NaiveDateTime, DateTime, FixedOffset, Utc, SecondsFormat};
29use crate::{OutlinePrint};
30use crate::models::*;
31use super::{Error, configuration};
32use headers::{Authorization, Header};
33use headers::authorization::Credentials;
34
35pub struct MonitoringAppApiClient<C: hyper::client::connect::Connect + Clone + Send + Sync> {
36    configuration: Arc<configuration::Configuration<C>>,
37}
38
39impl<C: hyper::client::connect::Connect + Clone + Send + Sync + 'static> MonitoringAppApiClient<C> {
40    pub fn new(configuration: Arc<configuration::Configuration<C>>) -> MonitoringAppApiClient<C> {
41        MonitoringAppApiClient {
42            configuration: configuration,
43        }
44    }
45}
46
47#[async_trait::async_trait]
48pub trait MonitoringAppApi {
49    async fn create_spm_application1(&self, body: crate::models::CreateAppInfo) -> Result<AppsResponse, Error<serde_json::Value>>;
50}
51
52#[async_trait::async_trait]
53impl<C: hyper::client::connect::Connect + Clone + Send + Sync + 'static>MonitoringAppApi for MonitoringAppApiClient<C> {
54 ///
55 /// Create Monitoring App
56 ///
57 /// 
58 ///
59 /// # Arguments
60 ///
61 /// * `body` - Create Monitoring App
62 /// 
63 ///
64    async fn create_spm_application1(&self, body: crate::models::CreateAppInfo) -> Result<AppsResponse, Error<serde_json::Value>> {
65        let configuration: &configuration::Configuration<C> = self.configuration.borrow();
66
67        let mut auth_headers = HashMap::<String, String>::new();
68        let mut auth_query = HashMap::<String, String>::new();
69        if let Some(ref apikey) = configuration.api_key {
70            let key = apikey.key.clone();
71            let val = match apikey.prefix {
72                Some(ref prefix) => format!("{} {}", prefix, key),
73                None => key,
74            };
75            auth_headers.insert("Authorization".to_owned(), val);
76        };
77        let method = hyper::Method::POST;
78
79        let query_string = {
80            let mut query = ::url::form_urlencoded::Serializer::new(String::new());
81            let has_query_params = false;
82            for (key, val) in &auth_query {
83                query.append_pair(key, val);
84            }
85            if has_query_params || auth_query.len()>0  {
86                format!("/?{}", query.finish())
87            } else {
88                "".to_string()
89            }
90        };
91        let uri_str = format!("{}spm-reports/api/v3/apps{}", configuration.base_path, query_string);
92
93        // TODO(farcaller): handle error
94        // if let Err(e) = uri {
95        //     return Box::new(futures::future::err(e));
96        // }
97        //dbg!(&uri_str);
98
99        let uri: hyper::Uri = uri_str.parse().unwrap();
100
101        let mut req =
102            hyper::Request::builder()
103                .method(method)
104                .uri(uri);
105
106        let headers = req.headers_mut().unwrap();
107
108        if let Some(ref user_agent) = configuration.user_agent {
109            headers.insert(hyper::header::USER_AGENT, user_agent.parse().unwrap());
110        }
111
112
113        for (key, val) in auth_headers {
114            headers.insert(
115                hyper::header::HeaderName::from_str(key.as_ref()).unwrap(),
116                val.parse().unwrap(),
117            );
118        }
119
120        let somebody = Body::empty();
121        let serialized = serde_json::to_string(&body).unwrap();
122        //Pretty print request body if needed for some dbg reasons
123        //let value_result: serde_json::Value = serde_json::from_str(&serialized).unwrap();
124        //eprintln!("\n{}\n", serde_json::to_string_pretty(&value_result).unwrap());
125        headers.insert(hyper::header::CONTENT_TYPE, "application/json".parse().unwrap());
126        headers.insert(hyper::header::CONTENT_LENGTH, format!("{}", serialized.len()).parse().unwrap());
127        let somebody = Body::from(serialized);
128
129        let req = req.body(somebody).unwrap();
130
131        let res = configuration
132            .client.request(req)
133            .await
134            .map_err(|e| -> Error<serde_json::Value> { Error::from(e) });
135
136        let mut res = res?;
137
138        let status = res.status();
139        let mut res_body: Vec<u8> = vec![];
140
141        while let Some(chunk) = res.body_mut().data().await {
142            let mut chunk_vec = chunk.unwrap().to_vec();
143            res_body.append(chunk_vec.as_mut());
144        }
145
146        //Uncomment to see what went wrong
147/*
148        let string_result = std::str::from_utf8(&res_body).unwrap();
149        let value_result: Result<serde_json::Value, serde_json::Error> = serde_json::from_str(&string_result);
150        if let Ok(json_value) = value_result {
151            //Valid json, invalid structure, pretty-printed output
152            eprintln!("{}", serde_json::to_string_pretty(&json_value).unwrap());
153        } else {
154            //Invalid json, raw output
155            dbg!(&string_result);
156        }
157*/
158        let res_body =
159            if status.is_success() {
160                Ok(res_body)
161            } else {
162                Err(Error::from((status, res_body.borrow())))
163            };
164
165        let mut res_body = res_body?;
166
167        let res_body =
168            serde_json::from_slice(res_body.borrow())
169            .map_err(|e| -> Error<serde_json::Value> { Error::from(e) });
170
171        res_body
172    }
173
174}