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 std::sync::Arc;
9
10use crate::error::{NimbusError, Result};
11use crate::Experiment;
12use fs_client::FileSystemClient;
13use null_client::NullClient;
14use remote_settings::RemoteSettingsService;
15use url::Url;
16
17pub(crate) fn create_client(
18    rs_service: Option<Arc<RemoteSettingsService>>,
19    collection_name: Option<String>,
20) -> Result<Box<dyn SettingsClient + Send>> {
21    Ok(match (rs_service, collection_name) {
22        (Some(rs_service), Some(collection_name)) => {
23            let url = Url::parse(&rs_service.client_url())?; // let call this the server url
24            match url.scheme() {
25                "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                }
35                _ => Box::new(rs_service.make_client(collection_name)),
36            }
37        }
38        (Some(_), None) => return Err(NimbusError::InternalError("collection name required")),
39        (None, _) => Box::new(NullClient::new()),
40    })
41}
42
43// The trait used to fetch experiments.
44pub(crate) trait SettingsClient {
45    #[allow(dead_code)]
46    fn get_experiments_metadata(&self) -> Result<String>;
47    fn fetch_experiments(&self) -> Result<Vec<Experiment>>;
48}