Skip to content

Commit

Permalink
chore: adding environment to unification body parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez committed Jul 4, 2024
1 parent a1c6460 commit 184ae7c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 36 deletions.
7 changes: 5 additions & 2 deletions integrationos-api/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use envconfig::Envconfig;
use integrationos_domain::cache::CacheConfig;
use integrationos_domain::{cache::CacheConfig, environment::Environment};
use integrationos_domain::{
database::DatabaseConfig, openai::OpenAiConfig, secrets::SecretsConfig,
};
Expand Down Expand Up @@ -85,6 +85,8 @@ pub struct Config {
pub cache_config: CacheConfig,
#[envconfig(from = "RATE_LIMIT_ENABLED", default = "true")]
pub rate_limit_enabled: bool,
#[envconfig(from = "ENVIRONMENT", default = "development")]
pub environment: Environment,
}

impl Display for Config {
Expand Down Expand Up @@ -146,7 +148,8 @@ impl Display for Config {
writeln!(f, "{}", self.db_config)?;
writeln!(f, "{}", self.openai_config)?;
writeln!(f, "{}", self.cache_config)?;
writeln!(f, "RATE_LIMIT_ENABLED: {}", self.rate_limit_enabled)
writeln!(f, "RATE_LIMIT_ENABLED: {}", self.rate_limit_enabled)?;
writeln!(f, "ENVIRONMENT: {}", self.environment)
}
}

Expand Down
12 changes: 6 additions & 6 deletions integrationos-api/src/endpoints/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ use bson::doc;
use convert_case::{Case, Casing};
use http::{HeaderMap, HeaderName};
use integrationos_domain::{
ApplicationError, InternalError,
{
connection_model_definition::CrudAction, destination::Action,
encrypted_access_key::EncryptedAccessKey, encrypted_data::PASSWORD_LENGTH,
event_access::EventAccess, AccessKey, Event,
},
connection_model_definition::CrudAction, destination::Action,
encrypted_access_key::EncryptedAccessKey, encrypted_data::PASSWORD_LENGTH, environment,
event_access::EventAccess, AccessKey, ApplicationError, Event, InternalError,
};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
Expand Down Expand Up @@ -223,12 +220,15 @@ pub async fn process_request(
connection.platform, connection.platform_version, model_name, action_name,
);

// let environment = state.config.connection_definition_cache_ttl_secs

let mut response = state
.extractor_caller
.send_to_destination_unified(
connection.clone(),
action,
include_passthrough,
state.config.environment,
headers,
query_params,
body,
Expand Down
9 changes: 9 additions & 0 deletions integrationos-domain/src/domain/configuration/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ pub enum Environment {
Production,
}

impl Environment {
pub fn is_production(&self) -> bool {
match self {
Environment::Production | Environment::Live => true,
_ => false,
}
}
}

impl TryFrom<&str> for Environment {
type Error = IntegrationOSError;

Expand Down
64 changes: 36 additions & 28 deletions integrationos-unified/src/unified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use integrationos_domain::{
connection_model_schema::ConnectionModelSchema,
database::DatabaseConfig,
destination::{Action, Destination},
environment::{self, Environment},
error::InternalError,
get_secret_request::GetSecretRequest,
hashed_secret::HashedSecret,
Expand Down Expand Up @@ -221,6 +222,7 @@ impl UnifiedDestination {
connection: Arc<Connection>,
action: Action,
include_passthrough: bool,
environment: Environment,
mut headers: HeaderMap,
mut query_params: HashMap<String, String>,
mut body: Option<Value>,
Expand Down Expand Up @@ -675,37 +677,43 @@ impl UnifiedDestination {
error!("Could not select body at response path {path}: {e}");
ApplicationError::bad_request(&e.to_string(), None)
})?;
if bodies.is_empty() {
let error_string = format!(
if environment.is_production()
&& matches!(config.action_name, CrudAction::GetMany | CrudAction::GetOne)
{
if bodies.is_empty() {
let error_string = format!(
"Could not map unified model. 3rd party Connection returned an invalid response. Expected model at path {path} but found none.",
);
let mut res = Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(json!({
"message": error_string,
"passthrough": wrapped_body
}))
.map_err(|e| {
error!("Could not create response from builder for missing body");
IntegrationOSError::from_err_code(
StatusCode::UNPROCESSABLE_ENTITY,
&e.to_string(),
None,
)
})?;
*res.headers_mut() = headers;
return Ok(res);
}
if bodies.len() != 1 {
return Err(InternalError::invalid_argument(
&format!(
"Invalid number of selected bodies ({}) at response path {path}",
bodies.len()
),
None,
));
let mut res = Response::builder()
.status(StatusCode::UNPROCESSABLE_ENTITY)
.body(json!({
"message": error_string,
"passthrough": wrapped_body
}))
.map_err(|e| {
error!("Could not create response from builder for missing body");
IntegrationOSError::from_err_code(
StatusCode::UNPROCESSABLE_ENTITY,
&e.to_string(),
None,
)
})?;
*res.headers_mut() = headers;
return Ok(res);
}
if bodies.len() != 1 {
return Err(InternalError::invalid_argument(
&format!(
"Invalid number of selected bodies ({}) at response path {path}",
bodies.len()
),
None,
));
}
Some(bodies.remove(0).clone())
} else {
None
}
Some(bodies.remove(0).clone())
} else {
None
};
Expand Down

0 comments on commit 184ae7c

Please sign in to comment.