Skip to main content

hanzo_client/apis/
pubsub_api.rs

1/*
2 * Hanzo Cloud API
3 *
4 * The Hanzo Cloud API as a customer calls it: every operation under /v1/ except the operator's admin product, relay routes, legacy spellings and capabilities still reached by flag. Tagged by product: the first path segment after /v1/.
5 *
6 * The version of the OpenAPI document: v1
7 * 
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`post_pubsub_publish`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum PostPubsubPublishError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`post_pubsub_request`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum PostPubsubRequestError {
29    UnknownValue(serde_json::Value),
30}
31
32
33/// Publish puts one message on the org's bus. When a stream captures the subject the write is DURABLE — the receipt names the stream and sequence only after JetStream has it on storage, and a repeated Nats-Msg-Id header within the dedup window answers duplicate instead of storing twice. When nothing captures it, the message goes out core NATS: delivered to current subscribers, receipt {ok}, nothing retained.
34pub async fn post_pubsub_publish(configuration: &configuration::Configuration, bus_publish: models::BusPublish) -> Result<models::BusAck, Error<PostPubsubPublishError>> {
35    // add a prefix to parameters to efficiently prevent name collisions
36    let p_bus_publish = bus_publish;
37
38    let uri_str = format!("{}/v1/pubsub/publish", configuration.base_path);
39    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
40
41    if let Some(ref user_agent) = configuration.user_agent {
42        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
43    }
44    if let Some(ref token) = configuration.bearer_access_token {
45        req_builder = req_builder.bearer_auth(token.to_owned());
46    };
47    req_builder = req_builder.json(&p_bus_publish);
48
49    let req = req_builder.build()?;
50    let resp = configuration.client.execute(req).await?;
51
52    let status = resp.status();
53    let content_type = resp
54        .headers()
55        .get("content-type")
56        .and_then(|v| v.to_str().ok())
57        .unwrap_or("application/octet-stream");
58    let content_type = super::ContentType::from(content_type);
59
60    if !status.is_client_error() && !status.is_server_error() {
61        let content = resp.text().await?;
62        match content_type {
63            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
64            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::BusAck`"))),
65            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::BusAck`")))),
66        }
67    } else {
68        let content = resp.text().await?;
69        let entity: Option<PostPubsubPublishError> = serde_json::from_str(&content).ok();
70        Err(Error::ResponseError(ResponseContent { status, content, entity }))
71    }
72}
73
74/// Request sends one request on the org's bus and waits for one reply — the synchronous half of pub/sub, for callers speaking to a responder subscribed on the NATS port. 404 when nobody is listening on the subject; 408 when a responder exists but no reply arrived within the timeout.
75pub async fn post_pubsub_request(configuration: &configuration::Configuration, bus_request: models::BusRequest) -> Result<models::BusMessage, Error<PostPubsubRequestError>> {
76    // add a prefix to parameters to efficiently prevent name collisions
77    let p_bus_request = bus_request;
78
79    let uri_str = format!("{}/v1/pubsub/request", configuration.base_path);
80    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
81
82    if let Some(ref user_agent) = configuration.user_agent {
83        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
84    }
85    if let Some(ref token) = configuration.bearer_access_token {
86        req_builder = req_builder.bearer_auth(token.to_owned());
87    };
88    req_builder = req_builder.json(&p_bus_request);
89
90    let req = req_builder.build()?;
91    let resp = configuration.client.execute(req).await?;
92
93    let status = resp.status();
94    let content_type = resp
95        .headers()
96        .get("content-type")
97        .and_then(|v| v.to_str().ok())
98        .unwrap_or("application/octet-stream");
99    let content_type = super::ContentType::from(content_type);
100
101    if !status.is_client_error() && !status.is_server_error() {
102        let content = resp.text().await?;
103        match content_type {
104            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
105            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::BusMessage`"))),
106            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::BusMessage`")))),
107        }
108    } else {
109        let content = resp.text().await?;
110        let entity: Option<PostPubsubRequestError> = serde_json::from_str(&content).ok();
111        Err(Error::ResponseError(ResponseContent { status, content, entity }))
112    }
113}
114