Skip to main content

open_net/api/ws/
response.rs

1use super::{ConnectionId, IncomingMessage, Message, RequestId};
2use crate::error::{ErrorKind, NetError};
3use crate::Result;
4use std::time::SystemTime;
5
6/// Owned business response that can outlive its inbound message and registration.
7#[derive(Clone, Debug)]
8pub struct Response {
9    /// Identifier of the business request completed by this response.
10    request_id: RequestId,
11    /// Text or binary business body, excluding control frames.
12    message: Message,
13    /// Time at which the inbound message was received.
14    received_at: SystemTime,
15    /// Physical connection that received the response.
16    connection_id: ConnectionId,
17    retry_report: Option<crate::api::retry::RetryReport>,
18}
19
20impl Response {
21    /// Returns the associated request identifier.
22    pub(crate) fn from_incoming(request_id: RequestId, incoming: &IncomingMessage) -> Result<Self> {
23        let message = incoming
24            .message()
25            .ok_or_else(|| NetError::from(ErrorKind::InvalidInput))?
26            .clone();
27        Ok(Self {
28            request_id,
29            message,
30            received_at: incoming.received_at(),
31            connection_id: incoming.connection_id(),
32            retry_report: None,
33        })
34    }
35
36    /// Retry diagnostics for a completed logical request; absent on fixed single requests.
37    pub fn retry_report(&self) -> Option<&crate::api::retry::RetryReport> {
38        self.retry_report.as_ref()
39    }
40    pub(crate) fn with_retry_report(mut self, report: crate::api::retry::RetryReport) -> Self {
41        self.retry_report = Some(report);
42        self
43    }
44    /// Returns the associated request identifier.
45    pub fn request_id(&self) -> &RequestId {
46        &self.request_id
47    }
48
49    /// Borrows the response body.
50    pub fn message(&self) -> &Message {
51        &self.message
52    }
53
54    /// Returns the receive timestamp recorded by the SDK.
55    pub fn received_at(&self) -> SystemTime {
56        self.received_at
57    }
58
59    /// Returns the physical connection identity.
60    pub fn connection_id(&self) -> ConnectionId {
61        self.connection_id
62    }
63
64    /// Consumes the response and returns its body.
65    pub fn into_message(self) -> Message {
66        self.message
67    }
68}
69
70#[cfg(test)]
71#[path = "response_tests.rs"]
72mod tests;