chilloutvr 0.6.0

Unofficial rust types of ChilloutVR's API
Documentation
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! An optional API client feature using `reqwest`
//!
//! Besides using this, you could instead easily implement your own client using
//! a different HTTP library with the [`racal::Queryable`](racal::Queryable)
//! trait. Though this does additionally support unwrapping the message/data of
//! the CVR API responses.
//!
//! If you're implementing your own API client, you need to implement two
//! possible API states:
//!
//! 1. [`chilloutvr::query::NoAuthentication`](crate::query::NoAuthentication)
//!
//! > Doesn't require authentication but still needs to be rate limited
//! > properly.
//!
//! 2. [`chilloutvr::query::SavedLoginCredentials`](crate::model::SavedLoginCredentials)
//!
//! > Requires the `Username` and `AccessKey` headers in addition to the rate
//! > limiting.
//!
//! The WebSocket API client is more messy, in this implementation the
//! connection is opened lazily (on first use) and never manually closed again
//! afterwards.

#[cfg(feature = "http_client")]
use std::num::NonZeroU32;

#[cfg(feature = "http_client")]
use governor::{
	Quota,
	RateLimiter,
	clock::DefaultClock,
	middleware::NoOpMiddleware,
	state::{InMemoryState, NotKeyed},
};
use http::{HeaderName, HeaderValue, header::InvalidHeaderValue};
#[cfg(feature = "http_client")]
pub use racal::reqwest::ApiClient;
#[cfg(feature = "http_client")]
use reqwest::{Client, RequestBuilder, header::HeaderMap};

#[cfg(feature = "http_client")]
use crate::query::NoAuthentication;
use crate::query::SavedLoginCredentials;

#[cfg(feature = "ws_client")]
mod ws;

/// Configuration for the API client
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
pub struct ApiConfiguration {
	/// The user agent of the API client
	pub user_agent: String,
	/// If the API client should indicate it has the mature access DLC enabled
	pub mature_content_enabled: bool,
	/// What platform the API client should indicate it's using (almost always
	/// should remain `pc_standalone`)
	pub platform: String,
	/// A comma separated string of compatible (API?) versions
	pub compatible_versions: String,
}

impl ApiConfiguration {
	/// Creates a new API client configuration
	#[must_use]
	pub fn new(user_agent: String) -> Self {
		Self {
			user_agent,
			mature_content_enabled: false,
			platform: "pc_standalone".to_string(),
			compatible_versions: "0,1,2".to_string(),
		}
	}

	fn to_headers(
		&self,
	) -> Result<Vec<(HeaderName, HeaderValue)>, InvalidHeaderValue> {
		Ok(vec![
			(http::header::USER_AGENT, HeaderValue::try_from(&self.user_agent)?),
			(
				"MatureContentDlc".parse().unwrap(),
				HeaderValue::try_from(self.mature_content_enabled.to_string())?,
			),
			("Platform".parse().unwrap(), HeaderValue::try_from(&self.platform)?),
			(
				"CompatibleVersions".parse().unwrap(),
				HeaderValue::try_from(&self.compatible_versions)?,
			),
		])
	}
}

impl SavedLoginCredentials {
	fn to_headers(
		&self,
	) -> Result<Vec<(HeaderName, HeaderValue)>, InvalidHeaderValue> {
		Ok(vec![
			("Username".parse().unwrap(), self.username.parse()?),
			("AccessKey".parse().unwrap(), self.access_key.parse()?),
		])
	}
}

/// An error that may happen with an API query
#[derive(Debug)]
pub enum ApiError {
	/// An error happened with serialization
	Serde(serde_json::Error),
	/// An error happened with the HTTPS request
	#[cfg(feature = "http_client")]
	Http(reqwest::Error),
	/// An error happened with the WS connection
	#[cfg(feature = "ws_client")]
	WebSocket(ezsockets::Error),
}

impl From<serde_json::Error> for ApiError {
	fn from(err: serde_json::Error) -> Self { Self::Serde(err) }
}

#[cfg(feature = "http_client")]
impl From<reqwest::Error> for ApiError {
	fn from(err: reqwest::Error) -> Self { Self::Http(err) }
}

#[cfg(feature = "http_client")]
impl From<racal::reqwest::ApiError> for ApiError {
	fn from(err: racal::reqwest::ApiError) -> Self {
		match err {
			racal::reqwest::ApiError::Reqwest(e) => Self::Http(e),
			racal::reqwest::ApiError::Serde(e) => Self::Serde(e),
		}
	}
}

#[cfg(feature = "ws_client")]
impl From<ezsockets::Error> for ApiError {
	fn from(err: ezsockets::Error) -> Self { Self::WebSocket(err) }
}

#[cfg(feature = "http_client")]
type NormalRateLimiter =
	RateLimiter<NotKeyed, InMemoryState, DefaultClock, NoOpMiddleware>;

#[cfg(feature = "http_client")]
#[must_use]
fn http_rate_limiter() -> NormalRateLimiter {
	// ~5 seconds per request sustained over one minute, allowing up to a request
	// per second in bursts.
	RateLimiter::direct(
		Quota::per_minute(NonZeroU32::try_from(12).unwrap())
			.allow_burst(NonZeroU32::try_from(5).unwrap()),
	)
}

/// The main API client without authentication
#[cfg(feature = "http_client")]
pub struct UnauthenticatedCVR {
	config: ApiConfiguration,
	http: Client,
	http_rate_limiter: NormalRateLimiter,
}

#[cfg(feature = "http_client")]
#[async_trait::async_trait]
impl racal::reqwest::ApiClient<NoAuthentication> for UnauthenticatedCVR {
	fn state(&self) -> &NoAuthentication { &NoAuthentication {} }

	fn client(&self) -> &reqwest::Client { &self.http }

	async fn before_request(
		&self, req: RequestBuilder,
	) -> Result<RequestBuilder, racal::reqwest::ApiError> {
		self.http_rate_limiter.until_ready().await;
		Ok(req)
	}
}

/// The main API client with authentication
pub struct AuthenticatedCVR {
	config: ApiConfiguration,
	auth: SavedLoginCredentials,
	#[cfg(feature = "http_client")]
	http: Client,
	#[cfg(feature = "http_client")]
	http_rate_limiter: NormalRateLimiter,
	#[cfg(feature = "ws_client")]
	ws: tokio::sync::RwLock<Option<ws::Client>>,
}

#[cfg(feature = "http_client")]
#[async_trait::async_trait]
impl racal::reqwest::ApiClient<SavedLoginCredentials> for AuthenticatedCVR {
	fn state(&self) -> &SavedLoginCredentials { &self.auth }

	fn client(&self) -> &reqwest::Client { &self.http }

	async fn before_request(
		&self, req: RequestBuilder,
	) -> Result<RequestBuilder, racal::reqwest::ApiError> {
		self.http_rate_limiter.until_ready().await;
		Ok(req)
	}
}

impl AuthenticatedCVR {
	/// Creates an API client
	#[cfg(feature = "http_client")]
	fn http_client(
		config: &ApiConfiguration, auth: &SavedLoginCredentials,
	) -> Result<Client, ApiError> {
		use serde::ser::Error;

		let builder = Client::builder();
		let mut headers: Vec<(HeaderName, HeaderValue)> =
			config.to_headers().map_err(|e| {
				serde_json::Error::custom(
					"Couldn't parse config into headers: ".to_string() + &e.to_string(),
				)
			})?;
		headers.append(&mut auth.to_headers().map_err(|e| {
			serde_json::Error::custom(
				"Couldn't parse auth into headers: ".to_string() + &e.to_string(),
			)
		})?);

		let headers = HeaderMap::from_iter(headers);

		Ok(builder.default_headers(headers).build()?)
	}

	/// Removes authentication to the API client
	///
	/// # Errors
	///
	/// If deserializing user agent fails.# Panics
	///
	/// If there's an internal programming error, aka should never panic.ails
	pub fn new(
		config: ApiConfiguration, auth: impl Into<SavedLoginCredentials> + Send,
	) -> Result<Self, ApiError> {
		let auth = auth.into();
		Ok(Self {
			#[cfg(feature = "http_client")]
			http: Self::http_client(&config, &auth)?,
			#[cfg(feature = "http_client")]
			http_rate_limiter: http_rate_limiter(),
			#[cfg(feature = "ws_client")]
			ws: tokio::sync::RwLock::new(None),
			auth,
			config,
		})
	}

	/// Opens the WebSocket connection if it wasn't already open
	///
	/// # Errors
	///
	/// If opening the WS connection fails
	#[cfg(feature = "ws_client")]
	pub async fn ws_connect(&self) -> Result<(), ApiError> {
		{
			let lock = self.ws.read().await;
			if lock.is_some() {
				return Ok(());
			}
		}

		#[cfg(feature = "http_client")]
		self.http_rate_limiter.until_ready().await;
		let client = ws::Client::new(&self.config, &self.auth).await?;
		{
			let mut lock = self.ws.write().await;
			*lock = Some(client);
		}

		Ok(())
	}

	/// Closes the WebSocket connection if it is open
	#[cfg(feature = "ws_client")]
	pub async fn ws_disconnect(&self) {
		{
			let mut lock = self.ws.write().await;
			*lock = None;
		}
	}

	/// If the WS client is connected
	#[cfg(feature = "ws_client")]
	pub async fn ws_is_connected(&self) -> bool {
		let lock = self.ws.read().await;
		lock.is_some()
	}

	// Clippy seems to be wrong, or at least haven't been able to figure out how
	// this could be cleaned up more...
	#[allow(clippy::significant_drop_tightening)]
	/// Sends a WS message to the CVR API.
	///
	/// # Errors
	///
	/// If something with the request failed,
	/// or if the WS connection wasn't already open and creating it failed.
	///
	/// # Panics
	///
	/// If there's an internal programming error, aka should never panic.
	#[cfg(feature = "ws_client")]
	pub async fn send(
		&self,
		requestable: impl crate::query::Requestable + serde::ser::Serialize + Send,
	) -> Result<(), ApiError> {
		{
			let lock = self.ws.read().await;
			if let Some(ws_client) = &*lock {
				return ws_client.send(requestable);
			}
		}

		#[cfg(feature = "http_client")]
		self.http_rate_limiter.until_ready().await;
		let client = ws::Client::new(&self.config, &self.auth).await?;
		let mut lock = self.ws.write().await;
		*lock = Some(client);
		let lock = lock.downgrade();
		(*lock)
			.as_ref()
			.expect("client should exist as lock was never dropped")
			.send(requestable)
	}

	// Clippy seems to be wrong, or at least haven't been able to figure out how
	// this could be cleaned up more...
	#[allow(clippy::significant_drop_tightening)]
	/// Listens to events from the WS connection
	///
	/// # Errors
	///
	/// If creating the client fails,
	/// or if the WS connection wasn't already open and creating it failed.
	///
	/// # Panics
	///
	/// If there's an internal programming error, aka should never panic.
	#[cfg(feature = "ws_client")]
	pub async fn listen(&self) -> Result<ws::ReceiverContainer, ApiError> {
		{
			let lock = self.ws.read().await;
			if let Some(ws_client) = &*lock {
				return Ok(ws_client.listen());
			}
		}

		#[cfg(feature = "http_client")]
		self.http_rate_limiter.until_ready().await;
		let client = ws::Client::new(&self.config, &self.auth).await?;
		let mut lock = self.ws.write().await;
		*lock = Some(client);
		let lock = lock.downgrade();
		Ok(
			(*lock)
				.as_ref()
				.expect("client should exist as lock was never dropped")
				.listen(),
		)
	}

	/// Removes the authentication from the API client
	///
	/// # Errors
	///
	/// If locking fails
	pub async fn downgrade(self) -> Result<UnauthenticatedCVR, ApiError> {
		{
			let mut lock = self.ws.write().await;
			*lock = None;
		}
		let http = UnauthenticatedCVR::http_client(&self.config.user_agent)?;
		Ok(UnauthenticatedCVR {
			config: self.config,
			http,
			http_rate_limiter: self.http_rate_limiter,
		})
	}
}

#[cfg(feature = "http_client")]
impl UnauthenticatedCVR {
	/// Creates an unauthenticated API client
	fn http_client(user_agent: &str) -> Result<Client, ApiError> {
		Ok(Client::builder().user_agent(user_agent).build()?)
	}

	/// Adds authentication to the API client
	///
	/// # Errors
	///
	/// If deserializing user agent or authentication fails.
	pub fn upgrade(
		self, auth: impl Into<SavedLoginCredentials> + Send,
	) -> Result<AuthenticatedCVR, ApiError> {
		let auth = auth.into();
		Ok(AuthenticatedCVR {
			http: AuthenticatedCVR::http_client(&self.config, &auth)?,
			http_rate_limiter: self.http_rate_limiter,
			#[cfg(feature = "ws_client")]
			ws: tokio::sync::RwLock::new(None),
			auth,
			config: self.config,
		})
	}

	/// Creates a new CVR API client
	///
	/// # Errors
	///
	/// If deserializing user agent into a header fails,
	/// or if WS API is enabled & the connection establishment fails.
	pub fn new(config: ApiConfiguration) -> Result<Self, ApiError> {
		Ok(Self {
			http: Self::http_client(&config.user_agent)?,
			http_rate_limiter: http_rate_limiter(),
			config,
		})
	}
}