morax_api/
request.rs

1// Copyright 2024 tison <wander4096@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::collections::BTreeMap;
16
17use serde::Deserialize;
18use serde::Serialize;
19
20use crate::property::StorageProperty;
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct PubsubMessage {
24    /// The server-assigned ID of each published message. Guaranteed to be unique within the topic.
25    /// It must not be populated by the publisher in a publish call.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub message_id: Option<String>,
28    /// The time at which the message was published, populated by the server. It must not be
29    /// populated by the publisher in a publish call.
30    #[serde(skip_serializing_if = "Option::is_none")]
31    pub publish_time: Option<jiff::Timestamp>,
32    /// Optional. Attributes for this message. If this field is empty, the message must contain
33    /// non-empty data. This can be used to filter messages on the subscription.
34    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
35    #[serde(default)]
36    pub attributes: BTreeMap<String, String>,
37    /// A padded, base64-encoded string of bytes, encoded with a URL and filename safe alphabet
38    /// (sometimes referred to as "web-safe" or "base64url"). Defined by [RFC4648].
39    ///
40    /// [RFC4648]: https://datatracker.ietf.org/doc/html/rfc4648
41    pub data: String,
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct ReceivedMessage {
46    /// This ID can be used to acknowledge the received message.
47    pub ack_id: String,
48    /// The message.
49    pub message: PubsubMessage,
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct CreateTopicRequest {
54    /// Optional. The [`StorageProperty`] for the topic. If not specified, the default storage
55    /// property will be used.
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub storage: Option<StorageProperty>,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize)]
61pub struct CreateTopicResponse {
62    /// Name of the topic.
63    pub name: String,
64}
65
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct PublishMessageRequest {
68    /// Required. The messages to publish.
69    pub messages: Vec<PubsubMessage>,
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct PublishMessageResponse {
74    /// The server-assigned ID of each published message, in the same order as the messages in the
75    /// request. IDs are guaranteed to be unique within the topic.
76    pub message_ids: Vec<String>,
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct CreateSubscriptionRequest {
81    /// Required. The name of the topic from which this subscription is receiving messages.
82    pub topic_name: String,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct CreateSubscriptionResponse {
87    /// The name of the topic from which this subscription is receiving messages.
88    pub topic: String,
89    /// Name of the subscription.
90    pub name: String,
91}
92
93#[derive(Debug, Clone, Serialize, Deserialize)]
94pub struct PullMessageRequest {
95    /// Required. The maximum number of messages to return for this request. Must be a positive
96    /// integer.
97    pub max_messages: i64,
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize)]
101pub struct PullMessageResponse {
102    /// Received Pub/Sub messages.
103    pub messages: Vec<ReceivedMessage>,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize)]
107pub struct AcknowledgeRequest {
108    /// Required. The acknowledgment ID for the messages being acknowledged. Must not be empty.
109    pub ack_ids: Vec<String>,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct AcknowledgeResponse {}