nimbus/stateful/client/
mod.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 https://mozilla.org/MPL/2.0/. */
4
5mod fs_client;
6pub(crate) mod http_client;
7pub(crate) mod null_client;
8use crate::error::{NimbusError, Result};
9use crate::Experiment;
10use fs_client::FileSystemClient;
11use null_client::NullClient;
12use remote_settings::RemoteSettings;
13use remote_settings::RemoteSettingsConfig;
14
15pub(crate) fn create_client(
16    config: Option<RemoteSettingsConfig>,
17) -> Result<Box<dyn SettingsClient + Send>> {
18    Ok(match config {
19        Some(config) => {
20            assert!(config.server_url.is_none());
21            let Some(remote_settings_server) = config.server.as_ref() else {
22                return Ok(Box::new(RemoteSettings::new(config)?));
23            };
24            let url = remote_settings_server.url()?;
25            if url.scheme() == "file" {
26                // Everything in `config` other than the url/path is ignored for the
27                // file-system - we could insist on a sub-directory, but that doesn't
28                // seem valuable for the use-cases we care about here.
29                let path = match url.to_file_path() {
30                    Ok(path) => path,
31                    _ => return Err(NimbusError::InvalidPath(url.into())),
32                };
33                Box::new(FileSystemClient::new(path)?)
34            } else {
35                Box::new(RemoteSettings::new(config)?)
36            }
37        }
38        // If no server is provided, then we still want Nimbus to work, but serving
39        // an empty list of experiments.
40        None => Box::new(NullClient::new()),
41    })
42}
43
44// The trait used to fetch experiments.
45pub(crate) trait SettingsClient {
46    #[allow(dead_code)]
47    fn get_experiments_metadata(&self) -> Result<String>;
48    fn fetch_experiments(&self) -> Result<Vec<Experiment>>;
49}