diff options
Diffstat (limited to 'third_party/rust/remote_settings')
-rw-r--r-- | third_party/rust/remote_settings/.cargo-checksum.json | 2 | ||||
-rw-r--r-- | third_party/rust/remote_settings/src/client.rs | 89 | ||||
-rw-r--r-- | third_party/rust/remote_settings/src/config.rs | 30 | ||||
-rw-r--r-- | third_party/rust/remote_settings/src/error.rs | 2 | ||||
-rw-r--r-- | third_party/rust/remote_settings/src/lib.rs | 17 | ||||
-rw-r--r-- | third_party/rust/remote_settings/src/remote_settings.udl | 10 |
6 files changed, 129 insertions, 21 deletions
diff --git a/third_party/rust/remote_settings/.cargo-checksum.json b/third_party/rust/remote_settings/.cargo-checksum.json index 619a9d13ca..96d0e51acd 100644 --- a/third_party/rust/remote_settings/.cargo-checksum.json +++ b/third_party/rust/remote_settings/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.toml":"1029f571c66d33c4dfc5e9fc55287a780329ce183f5d2b672de79737155c4227","build.rs":"4326f03729cf8f1673e4228e6dc111de1ea4d8bcc06351f7ae563efb2613f866","src/client.rs":"7510ae0d5bcb9fbaa2c43c4773aa0fd518edc78fe0f396c0e1d6dd442446f429","src/config.rs":"7bb678addfae3b4ed5f2892d32263e5b33cc05e5a12a250f664150e78211f94a","src/error.rs":"192ca42af7c6b882f3129378c23b45dab8a0d2b179e23a8813a335ffd56b21dc","src/lib.rs":"416e99894e152f6cea7418ad2fabfd94bc3d907efd9f33fbd2a83fb99452b2df","src/remote_settings.udl":"2e71491ad3894d17e5bde0663d9490bfea6294d99cdbe9d67a36137faeedc593","uniffi.toml":"f8ec8dc593e0d501c2e9e40368ec93ec33b1edd8608e29495e0a54b63144e880"},"package":null}
\ No newline at end of file +{"files":{"Cargo.toml":"1029f571c66d33c4dfc5e9fc55287a780329ce183f5d2b672de79737155c4227","build.rs":"4326f03729cf8f1673e4228e6dc111de1ea4d8bcc06351f7ae563efb2613f866","src/client.rs":"666ef6536a81b107cdd6047b56ffb53a052c0a615b1fa827e630892c0e528a5d","src/config.rs":"52a209256acd8b1fada2b91e9d9f669df0ee6e9609baad7ec34a2111ed2a6541","src/error.rs":"b0cb36fb105d7ae8a2d8c2e5f98f410c106f1fd45c2e3e9bd8e865c0d2eb8726","src/lib.rs":"655559b1b0f09ad221ceba462ace73d9216a6551d70062126ffc8a085d8b89bb","src/remote_settings.udl":"1ffeb10385e4db63606cda79bb59e77170af1d2ca0028da8ab2c4d7622969734","uniffi.toml":"f8ec8dc593e0d501c2e9e40368ec93ec33b1edd8608e29495e0a54b63144e880"},"package":null}
\ No newline at end of file diff --git a/third_party/rust/remote_settings/src/client.rs b/third_party/rust/remote_settings/src/client.rs index 2cf26b7319..601b5c451a 100644 --- a/third_party/rust/remote_settings/src/client.rs +++ b/third_party/rust/remote_settings/src/client.rs @@ -4,7 +4,7 @@ use crate::config::RemoteSettingsConfig; use crate::error::{RemoteSettingsError, Result}; -use crate::UniffiCustomTypeConverter; +use crate::{RemoteSettingsServer, UniffiCustomTypeConverter}; use parking_lot::Mutex; use serde::Deserialize; use std::{ @@ -31,11 +31,17 @@ pub struct Client { impl Client { /// Create a new [Client] with properties matching config. pub fn new(config: RemoteSettingsConfig) -> Result<Self> { - let server_url = config - .server_url - .unwrap_or_else(|| String::from("https://firefox.settings.services.mozilla.com")); + let server = match (config.server, config.server_url) { + (Some(server), None) => server, + (None, Some(server_url)) => RemoteSettingsServer::Custom { url: server_url }, + (None, None) => RemoteSettingsServer::Prod, + (Some(_), Some(_)) => Err(RemoteSettingsError::ConfigError( + "`RemoteSettingsConfig` takes either `server` or `server_url`, not both".into(), + ))?, + }; + let bucket_name = config.bucket_name.unwrap_or_else(|| String::from("main")); - let base_url = Url::parse(&server_url)?; + let base_url = server.url()?; Ok(Self { base_url, @@ -519,6 +525,7 @@ mod test { #[test] fn test_defaults() { let config = RemoteSettingsConfig { + server: None, server_url: None, bucket_name: None, collection_name: String::from("the-collection"), @@ -532,6 +539,33 @@ mod test { } #[test] + fn test_deprecated_server_url() { + let config = RemoteSettingsConfig { + server: None, + server_url: Some("https://example.com".into()), + bucket_name: None, + collection_name: String::from("the-collection"), + }; + let client = Client::new(config).unwrap(); + assert_eq!(Url::parse("https://example.com").unwrap(), client.base_url); + } + + #[test] + fn test_invalid_config() { + let config = RemoteSettingsConfig { + server: Some(RemoteSettingsServer::Prod), + server_url: Some("https://example.com".into()), + bucket_name: None, + collection_name: String::from("the-collection"), + }; + match Client::new(config) { + Ok(_) => panic!("Wanted config error; got client"), + Err(RemoteSettingsError::ConfigError(_)) => {} + Err(err) => panic!("Wanted config error; got {}", err), + } + } + + #[test] fn test_attachment_can_be_downloaded() { viaduct_reqwest::use_reqwest_backend(); let server_info_m = mock("GET", "/") @@ -552,7 +586,10 @@ mod test { .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, collection_name: String::from("the-collection"), bucket_name: None, }; @@ -588,7 +625,10 @@ mod test { .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, collection_name: String::from("the-collection"), bucket_name: None, }; @@ -617,7 +657,10 @@ mod test { .with_header("etag", "\"1000\"") .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, collection_name: String::from("the-collection"), bucket_name: Some(String::from("the-bucket")), }; @@ -644,7 +687,10 @@ mod test { .with_header("Retry-After", "60") .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, collection_name: String::from("the-collection"), bucket_name: Some(String::from("the-bucket")), }; @@ -686,7 +732,10 @@ mod test { .with_header("etag", "\"1000\"") .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, collection_name: String::from("the-collection"), bucket_name: Some(String::from("the-bucket")), }; @@ -810,7 +859,10 @@ mod test { .with_header("etag", "\"1000\"") .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, collection_name: String::from("the-collection"), bucket_name: Some(String::from("the-bucket")), }; @@ -850,7 +902,10 @@ mod test { .with_header("etag", "\"1000\"") .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, collection_name: String::from("the-collection"), bucket_name: Some(String::from("the-bucket")), }; @@ -953,7 +1008,10 @@ mod test { .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, bucket_name: Some(String::from("the-bucket")), collection_name: String::from("the-collection"), }; @@ -982,7 +1040,10 @@ mod test { .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, bucket_name: Some(String::from("the-bucket")), collection_name: String::from("the-collection"), }; diff --git a/third_party/rust/remote_settings/src/config.rs b/third_party/rust/remote_settings/src/config.rs index 33fab1b500..d9051d06d0 100644 --- a/third_party/rust/remote_settings/src/config.rs +++ b/third_party/rust/remote_settings/src/config.rs @@ -3,19 +3,45 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ //! This module defines the custom configurations that consumers can set. -//! Those configurations override default values and can be used to set a custom server url, +//! Those configurations override default values and can be used to set a custom server, //! collection name, and bucket name. //! The purpose of the configuration parameters are to allow consumers an easy debugging option, //! and the ability to be explicit about the server. +use url::Url; + +use crate::Result; + /// Custom configuration for the client. /// Currently includes the following: -/// - `server_url`: The optional url for the settings server. If not specified, the standard server will be used. +/// - `server`: The Remote Settings server to use. If not specified, defaults to the production server (`RemoteSettingsServer::Prod`). +/// - `server_url`: An optional custom Remote Settings server URL. Deprecated; please use `server` instead. /// - `bucket_name`: The optional name of the bucket containing the collection on the server. If not specified, the standard bucket will be used. /// - `collection_name`: The name of the collection for the settings server. #[derive(Debug, Clone)] pub struct RemoteSettingsConfig { + pub server: Option<RemoteSettingsServer>, pub server_url: Option<String>, pub bucket_name: Option<String>, pub collection_name: String, } + +/// The Remote Settings server that the client should use. +#[derive(Debug, Clone)] +pub enum RemoteSettingsServer { + Prod, + Stage, + Dev, + Custom { url: String }, +} + +impl RemoteSettingsServer { + pub fn url(&self) -> Result<Url> { + Ok(match self { + Self::Prod => Url::parse("https://firefox.settings.services.mozilla.com").unwrap(), + Self::Stage => Url::parse("https://firefox.settings.services.allizom.org").unwrap(), + Self::Dev => Url::parse("https://remote-settings-dev.allizom.org").unwrap(), + Self::Custom { url } => Url::parse(url)?, + }) + } +} diff --git a/third_party/rust/remote_settings/src/error.rs b/third_party/rust/remote_settings/src/error.rs index 120681871b..32563800f0 100644 --- a/third_party/rust/remote_settings/src/error.rs +++ b/third_party/rust/remote_settings/src/error.rs @@ -22,6 +22,8 @@ pub enum RemoteSettingsError { ResponseError(String), #[error("This server doesn't support attachments")] AttachmentsUnsupportedError, + #[error("Error configuring client: {0}")] + ConfigError(String), } pub type Result<T, E = RemoteSettingsError> = std::result::Result<T, E>; diff --git a/third_party/rust/remote_settings/src/lib.rs b/third_party/rust/remote_settings/src/lib.rs index 9aa6ecbf1a..9380a3fbc7 100644 --- a/third_party/rust/remote_settings/src/lib.rs +++ b/third_party/rust/remote_settings/src/lib.rs @@ -11,7 +11,7 @@ pub use client::{ RsJsonObject, SortOrder, }; pub mod config; -pub use config::RemoteSettingsConfig; +pub use config::{RemoteSettingsConfig, RemoteSettingsServer}; uniffi::include_scaffolding!("remote_settings"); @@ -70,7 +70,10 @@ mod test { .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, bucket_name: Some(String::from("the-bucket")), collection_name: String::from("the-collection"), }; @@ -98,7 +101,10 @@ mod test { .create(); let config = RemoteSettingsConfig { - server_url: Some(mockito::server_url()), + server: Some(RemoteSettingsServer::Custom { + url: mockito::server_url(), + }), + server_url: None, bucket_name: Some(String::from("the-bucket")), collection_name: String::from("the-collection"), }; @@ -119,7 +125,10 @@ mod test { fn test_download() { viaduct_reqwest::use_reqwest_backend(); let config = RemoteSettingsConfig { - server_url: Some("http://localhost:8888".to_string()), + server: Some(RemoteSettingsServer::Custom { + url: "http://localhost:8888".into(), + }), + server_url: None, bucket_name: Some(String::from("the-bucket")), collection_name: String::from("the-collection"), }; diff --git a/third_party/rust/remote_settings/src/remote_settings.udl b/third_party/rust/remote_settings/src/remote_settings.udl index d830b6778f..1da52b833e 100644 --- a/third_party/rust/remote_settings/src/remote_settings.udl +++ b/third_party/rust/remote_settings/src/remote_settings.udl @@ -7,10 +7,19 @@ typedef string RsJsonObject; namespace remote_settings {}; +[Enum] +interface RemoteSettingsServer { + Prod(); + Stage(); + Dev(); + Custom(string url); +}; + dictionary RemoteSettingsConfig { string collection_name; string? bucket_name = null; string? server_url = null; + RemoteSettingsServer? server = null; }; dictionary RemoteSettingsResponse { @@ -43,6 +52,7 @@ enum RemoteSettingsError { "BackoffError", "ResponseError", "AttachmentsUnsupportedError", + "ConfigError", }; interface RemoteSettings { |