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}
18
19impl Response {
20    /// Returns the associated request identifier.
21    pub(crate) fn from_incoming(request_id: RequestId, incoming: &IncomingMessage) -> Result<Self> {
22        let message = incoming
23            .message()
24            .ok_or_else(|| NetError::from(ErrorKind::InvalidInput))?
25            .clone();
26        Ok(Self {
27            request_id,
28            message,
29            received_at: incoming.received_at(),
30            connection_id: incoming.connection_id(),
31        })
32    }
33
34    /// Returns the associated request identifier.
35    pub fn request_id(&self) -> &RequestId {
36        &self.request_id
37    }
38
39    /// Borrows the response body.
40    pub fn message(&self) -> &Message {
41        &self.message
42    }
43
44    /// Returns the receive timestamp recorded by the SDK.
45    pub fn received_at(&self) -> SystemTime {
46        self.received_at
47    }
48
49    /// Returns the physical connection identity.
50    pub fn connection_id(&self) -> ConnectionId {
51        self.connection_id
52    }
53
54    /// Consumes the response and returns its body.
55    pub fn into_message(self) -> Message {
56        self.message
57    }
58}
59
60#[cfg(test)]
61#[path = "response_tests.rs"]
62mod tests;