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