diff options
Diffstat (limited to 'third_party/rust/remote_settings/src')
-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 |
5 files changed, 128 insertions, 20 deletions
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 { |