remote_settings/
config.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//! This module defines the custom configurations that consumers can set.
6//! Those configurations override default values and can be used to set a custom server,
7//! collection name, and bucket name.
8//! The purpose of the configuration parameters are to allow consumers an easy debugging option,
9//! and the ability to be explicit about the server.
10
11use url::Url;
12
13use crate::error::warn;
14use crate::{ApiResult, Error, RemoteSettingsContext, Result};
15
16/// Remote settings configuration
17#[derive(Debug, Default, Clone, uniffi::Record)]
18pub struct RemoteSettingsConfig {
19    /// The Remote Settings server to use. Defaults to [RemoteSettingsServer::Prod],
20    #[uniffi(default = None)]
21    pub server: Option<RemoteSettingsServer>,
22    /// Bucket name to use, defaults to "main".  Use "main-preview" for a preview bucket
23    #[uniffi(default = None)]
24    pub bucket_name: Option<String>,
25    /// App context to use for JEXL filtering (when the `jexl` feature is present).
26    #[uniffi(default = None)]
27    pub app_context: Option<RemoteSettingsContext>,
28}
29
30/// The Remote Settings server that the client should use.
31#[derive(Debug, Clone, uniffi::Enum)]
32pub enum RemoteSettingsServer {
33    Prod,
34    Stage,
35    Dev,
36    Custom { url: String },
37}
38
39impl RemoteSettingsServer {
40    /// Get the [url::Url] for this server
41    #[error_support::handle_error(Error)]
42    pub fn url(&self) -> ApiResult<Url> {
43        self.get_url()
44    }
45
46    /// Get a BaseUrl for this server
47    pub fn get_base_url(&self) -> Result<BaseUrl> {
48        let base_url = BaseUrl::parse(self.raw_url())?;
49        // Custom URLs are weird and require a couple tricks for backwards compatibility.
50        // Normally we append `v1/` to match how this has historically worked.  However,
51        // don't do this for file:// schemes which normally don't make any sense, but it's
52        // what Nimbus uses to indicate they want to use the file-based client, rather than
53        // a remote-settings based one.
54        if base_url.url().scheme() != "file" {
55            Ok(base_url.join("v1"))
56        } else {
57            Ok(base_url)
58        }
59    }
60
61    /// get_url() that never fails
62    ///
63    /// If the URL is invalid, we'll log a warning and fall back to the production URL
64    pub fn get_base_url_with_prod_fallback(&self) -> BaseUrl {
65        match self.get_base_url() {
66            Ok(url) => url,
67            // The unwrap below will never fail, since prod is a hard-coded/valid URL.
68            Err(_) => {
69                warn!("Invalid Custom URL: {}", self.raw_url());
70                BaseUrl::parse(Self::Prod.raw_url()).unwrap()
71            }
72        }
73    }
74
75    fn raw_url(&self) -> &str {
76        match self {
77            Self::Prod => "https://firefox.settings.services.mozilla.com/v1",
78            Self::Stage => "https://firefox.settings.services.allizom.org/v1",
79            Self::Dev => "https://remote-settings-dev.allizom.org/v1",
80            Self::Custom { url } => url,
81        }
82    }
83
84    /// Internal version of `url()`.
85    ///
86    /// The difference is that it uses `Error` instead of `ApiError`.  This is what we need to use
87    /// inside the crate.
88    pub fn get_url(&self) -> Result<Url> {
89        Ok(match self {
90            Self::Prod => Url::parse("https://firefox.settings.services.mozilla.com/v1")?,
91            Self::Stage => Url::parse("https://firefox.settings.services.allizom.org/v1")?,
92            Self::Dev => Url::parse("https://remote-settings-dev.allizom.org/v1")?,
93            Self::Custom { url } => {
94                let mut url = Url::parse(url)?;
95                // Custom URLs are weird and require a couple tricks for backwards compatibility.
96                // Normally we append `v1/` to match how this has historically worked.  However,
97                // don't do this for file:// schemes which normally don't make any sense, but it's
98                // what Nimbus uses to indicate they want to use the file-based client, rather than
99                // a remote-settings based one.
100                if url.scheme() != "file" {
101                    url = url.join("v1")?
102                }
103                url
104            }
105        })
106    }
107}
108
109/// Url that's guaranteed safe to use as a base
110#[derive(Debug, Clone)]
111pub struct BaseUrl {
112    url: Url,
113}
114
115impl BaseUrl {
116    pub fn parse(url: &str) -> Result<Self> {
117        let url = Url::parse(url)?;
118        if url.cannot_be_a_base() {
119            Err(Error::UrlParsingError(
120                url::ParseError::RelativeUrlWithCannotBeABaseBase,
121            ))
122        } else {
123            Ok(Self { url })
124        }
125    }
126
127    pub fn url(&self) -> &Url {
128        &self.url
129    }
130
131    pub fn into_inner(self) -> Url {
132        self.url
133    }
134
135    pub fn join(&self, input: &str) -> BaseUrl {
136        Self {
137            // Unwrap is safe, because the join() docs say that it only will error for
138            // cannot-be-a-base URLs.
139            url: self.url.join(input).unwrap(),
140        }
141    }
142
143    pub fn path_segments_mut(&mut self) -> url::PathSegmentsMut<'_> {
144        // Unwrap is safe, because the path_segments_mut() docs say that it only will
145        // error for cannot-be-a-base URLs.
146        self.url.path_segments_mut().unwrap()
147    }
148
149    pub fn query_pairs_mut(&mut self) -> url::form_urlencoded::Serializer<'_, url::UrlQuery<'_>> {
150        self.url.query_pairs_mut()
151    }
152}