Skip to content

Commit

Permalink
make the changes to the other mapping client
Browse files Browse the repository at this point in the history
  • Loading branch information
wilyle committed Sep 27, 2023
1 parent 5e112db commit 045ca39
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
3 changes: 3 additions & 0 deletions mapping_clients/mock_mapping_service_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ freyja-contracts = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

[build-dependencies]
freyja-build-common = { workspace = true }
23 changes: 11 additions & 12 deletions mapping_clients/mock_mapping_service_client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::{env, fs, path::Path};
use std::env;

const OUT_DIR: &str = "OUT_DIR";
const SAMPLE_CONFIG_FILE: &str = "res/mock_mapping_service_client_config.sample.json";
const CONFIG_FILE: &str = "mock_mapping_service_client_config.json";
use freyja_build_common::copy_to_build_out_dir;

fn main() {
// The current directory of the build script is the package's root directory
let config_path = env::current_dir().unwrap().join(SAMPLE_CONFIG_FILE);

let target_dir = env::var(OUT_DIR).unwrap();
let dest_path = Path::new(&target_dir).join(CONFIG_FILE);
const RES_DIR_NAME: &str = "res";
const DEFAULT_CONFIG_FILE: &str = "mock_mapping_client_config.default.json";

fs::copy(&config_path, dest_path).unwrap();
fn main() {
// Current directory of the build script is the package's root directory
let config_path = env::current_dir()
.unwrap()
.join(RES_DIR_NAME)
.join(DEFAULT_CONFIG_FILE);

println!("cargo:rerun-if-changed={}", config_path.to_str().unwrap());
copy_to_build_out_dir(config_path, DEFAULT_CONFIG_FILE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use serde::{Deserialize, Serialize};

pub(crate) const CONFIG_FILE: &str = "mock_mapping_service_client_config.json";

/// Configuration metadata for discovering Ibeji using Chariott
#[derive(Clone, Serialize, Deserialize)]
pub struct Config {
Expand Down
2 changes: 1 addition & 1 deletion mapping_clients/mock_mapping_service_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

mod config;
pub mod mock_mapping_service_client;
mod mock_mapping_service_client_config;
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT

use std::{fs, path::Path, time::Duration};
use std::time::Duration;

use async_trait::async_trait;
use reqwest::Client;

use crate::mock_mapping_service_client_config::{Config, CONFIG_FILE};
use freyja_common::retry_utils::execute_with_retry;
use crate::config::Config;
use freyja_common::{config_utils, retry_utils::execute_with_retry};
use freyja_contracts::mapping_client::*;

const CONFIG_FILE: &str = "mock_mapping_client_config";
const CONFIG_EXT: &str = "json";

/// Mocks a mapping provider in memory
pub struct MockMappingServiceClient {
/// The base URL for requests
Expand All @@ -27,7 +30,7 @@ pub struct MockMappingServiceClient {
}

impl MockMappingServiceClient {
/// Creates a new instance of a CloudAdapter using a config file.
/// Creates a new instance of a MockMappingServiceClient using a config file.
///
/// # Arguments
/// - `config`: the config
Expand All @@ -43,12 +46,15 @@ impl MockMappingServiceClient {

#[async_trait]
impl MappingClient for MockMappingServiceClient {
/// Creates a new instance of a CloudAdapter with default settings
/// Creates a new instance of a MockMappingServiceClient with default settings
fn create_new() -> Result<Self, MappingClientError> {
let config_contents = fs::read_to_string(Path::new(env!("OUT_DIR")).join(CONFIG_FILE))
.map_err(MappingClientError::io)?;
let config: Config = serde_json::from_str(config_contents.as_str())
.map_err(MappingClientError::deserialize)?;
let config = config_utils::read_from_files(
CONFIG_FILE,
CONFIG_EXT,
env!("OUT_DIR"),
MappingClientError::io,
MappingClientError::deserialize,
)?;

Ok(Self::from_config(config))
}
Expand Down

0 comments on commit 045ca39

Please sign in to comment.