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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

use crate::message::{Action, Message, SerdeMessage};
use crate::request::{Request, RawRequest};
use crate::error::ApiError;

use stream::traits::ByteStream;
use stream::client::Connection;
pub use stream::client::{Config, ReconStrat};
use stream::packet::{Packet, PacketBytes};
pub use stream::packet::PlainBytes;
use stream::StreamError;

#[cfg(feature = "encrypted")]
pub use stream::packet::EncryptedBytes;
#[cfg(feature = "encrypted")]
use crypto::signature::PublicKey;

pub struct Client<A, B> {
	inner: Connection<Message<A, B>>
}

impl<A, B> Client<A, B> {

	pub async fn request<R>(&self, req: R) -> Result<R::Response, R::Error>
	where
		R: Request<A, B>,
		A: Action,
		B: PacketBytes
	{

		// serialize
		let mut msg = Message::new();
		msg.body_mut()
			.serialize(&req)
			.map_err(|e| R::Error::request(
				format!("malformed request: {}", e)
			))?;
		msg.header_mut().set_action(R::ACTION);

		let res = self.inner.request(msg).await;
		let res = match res {
			Ok(r) => r,
			Err(e) => return Err(match e {
				// those are probably not returned
				StreamError::ConnectionClosed => R::Error::connection_closed(),
				StreamError::RequestDropped => R::Error::request_dropped(),
				// stream closed does not occur in a request
				e => R::Error::other(format!("{}", e))
			})
		};

		// now deserialize the response
		if res.is_success() {
			res.body().deserialize()
				.map_err(|e| R::Error::response(
					format!("malformed response: {}", e)
				))
		} else {
			// deserialize the error or create an error
			let e = res.body().deserialize()
				.unwrap_or_else(|e| R::Error::response(
					format!("malformed error: {}", e)
				));
			Err(e)
		}
	}

	pub async fn raw_request<R>(
		&self,
		req: R
	) -> Result<R::Response, R::Error>
	where
		R: RawRequest<A, B>,
		A: Action,
		B: PacketBytes
	{

		let mut msg = req.into_message()?;
		msg.header_mut().set_action(R::ACTION);

		let res = self.inner.request(msg).await;
		let res = match res {
			Ok(r) => r,
			Err(e) => return Err(match e {
				// those are probably not returned
				StreamError::ConnectionClosed => R::Error::connection_closed(),
				StreamError::RequestDropped => R::Error::request_dropped(),
				// stream closed does not occur in a request
				e => R::Error::other(format!("{}", e))
			})
		};

		// now deserialize the response
		if res.is_success() {
			R::Response::from_message(res)
		} else {
			// deserialize the error or create an error
			let e = res.body().deserialize()
				.unwrap_or_else(|e| R::Error::response(
					format!("malformed error: {}", e)
				));
			Err(e)
		}
	}

	pub async fn close(self) {
		let _ = self.inner.close().await;
	}

}

impl<A> Client<A, PlainBytes>
where A: Action + Send + 'static {

	// plain
	pub fn new<S>(
		stream: S,
		cfg: Config,
		recon_strat: Option<ReconStrat<S>>
	) -> Self
	where S: ByteStream {
		Self {
			inner: Connection::new(stream, cfg, recon_strat)
		}
	}

}

#[cfg(feature = "encrypted")]
impl<A> Client<A, EncryptedBytes>
where A: Action + Send + 'static {

	pub fn new<S>(
		stream: S,
		cfg: Config,
		recon_strat: Option<ReconStrat<S>>,
		pub_key: PublicKey
	) -> Self
	where S: ByteStream {
		Self {
			inner: Connection::new_encrypted(stream, cfg, recon_strat, pub_key)
		}
	}

}