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;
8
9use std::sync::Arc;
10
11use remote_settings::RemoteSettingsService;
12use url::Url;
13
14use crate::Experiment;
15use crate::error::{NimbusError, Result};
16use crate::stateful::client::fs_client::FileSystemClient;
17use crate::stateful::client::null_client::NullClient;
18
19pub struct NimbusServerSettings {
20    pub rs_service: Arc<RemoteSettingsService>,
21    pub collection_name: String,
22}
23
24pub(crate) fn create_client(
25    rs_info: Option<NimbusServerSettings>,
26) -> Result<Box<dyn SettingsClient + Send>> {
27    Ok(match rs_info {
28        Some(NimbusServerSettings {
29            rs_service,
30            collection_name,
31        }) => {
32            let url = Url::parse(&rs_service.client_url())?; // server url
33            match url.scheme() {
34                "file" => {
35                    // Everything in `config` other than the url/path is ignored for the
36                    // file-system - we could insist on a sub-directory, but that doesn't
37                    // seem valuable for the use-cases we care about here.
38                    let path = match url.to_file_path() {
39                        Ok(path) => path,
40                        _ => return Err(NimbusError::InvalidPath(url.into())),
41                    };
42                    Box::new(FileSystemClient::new(path)?)
43                }
44                _ => Box::new(rs_service.make_client(collection_name)),
45            }
46        }
47        None => Box::new(NullClient::new()),
48    })
49}
50
51// The trait used to fetch experiments.
52pub(crate) trait SettingsClient {
53    #[allow(dead_code)]
54    fn get_experiments_metadata(&self) -> Result<String>;
55    fn fetch_experiments(&self) -> Result<Vec<Experiment>>;
56}