Skip to content

Commit

Permalink
Adding platform-pages routes
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez committed Apr 30, 2024
1 parent aa807e8 commit 880bef4
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ futures-util = "0.3.28"
handlebars = "4.4.0"
http = "1.1.0"
http-serde-ext = "1.0.2"
integrationos-domain = "3.0.3"
integrationos-domain = "3.0.5"
js-sandbox-ios = "0.1.0"
jsonpath_lib = "0.3.0"
jsonwebtoken = "8.3.0"
Expand Down
80 changes: 80 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The following CRUD endopints are implemented for
- [`connection-model-schemas`](#v1connection-model-schemas-connection-model-schemas)
- [`connection-oauth-definitions`](#v1connection-oauth-definitions-connection-oauth-definitions)
- [`common-models`](#v1common-models-common-models)
- [`platform-pages`](#v1platform-pages-platform-pages)
- [`platforms`](#v1platforms-platforms)

### `GET` Requests

Expand Down Expand Up @@ -755,3 +757,81 @@ The response will have a format similar to the following:
"deprecated": false
}
```

### `/v1/platform-pages` Platform Pages

The `GET`, `DELETE`, and `POST` responses have the format:

```json
{
"platformId": "tx::5JOk3Ptuwsc::4FIiZNdGt11VaSRvi6HKzg",
"connectionDefinitionId": "crs::0UO2FOIhQvI::UcHIZjMQI_rlDtM9n18Vbw",
"platformName": "ryVRWXi",
"type": "actionUngenerated",
"connectionModelSchemaId": "conn_def::TBCRnVANRc0::KB2OmzcJGR6621_E3vMDdQ",
"url": "zOxoIA",
"modelName": "nKtwljKNHtNIw",
"content": "PPtBI",
"ownership": {
"type": "user",
"buildableId": "5dij4k0gGudZ",
"clientId": "DgS3fOmJxUszR8",
"organizationId": null,
"projectId": "hBjvkzc9Qx",
"userId": null
},
"analyzed": false
}
```

The `POST` and `PATCH` payloads have the format:

```json
{
"_id": "plf_pg::F5CKoR4XcxA::BrkeW5YFRxaXL1TPSdxawg",
"platformId": "tx::5JOk3Ptuwsc::4FIiZNdGt11VaSRvi6HKzg",
"platformName": "ryVRWXi",
"connectionDefinitionId": "crs::0UO2FOIhQvI::UcHIZjMQI_rlDtM9n18Vbw",
"type": "actionUngenerated",
"connectionModelSchemaId": "conn_def::TBCRnVANRc0::KB2OmzcJGR6621_E3vMDdQ",
"url": "zOxoIA",
"modelName": "nKtwljKNHtNIw",
"content": "PPtBI",
"createdAt": 1698009484117,
"updatedAt": 1698009484117,
"updated": false,
"version": "1.0.0",
"lastModifiedBy": "system",
"deleted": false,
"changeLog": {},
"tags": [],
"active": true,
"deprecated": false,
"ownership": {
"type": "user",
"buildableId": "5dij4k0gGudZ",
"clientId": "DgS3fOmJxUszR8",
"organizationId": null,
"projectId": "hBjvkzc9Qx",
"userId": null
},
"analyzed": false
}
```

The `POST` and `PATCH` payloads have the format:

```json
{
"connectionDefinitionId": null,
"name": "djp3rK",
"url": "yEYQqCm85rV",
"version": "6.18.17",
"ownership": {
"type": "system",
"entity": "KycF9dwCsnv",
"isInternal": true
},
"analyzed": false
}
```
1 change: 1 addition & 0 deletions api/src/endpoints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod openapi;
pub mod passthrough;
pub mod pipeline;
pub mod platform;
pub mod platform_page;
pub mod transactions;
pub mod unified;

Expand Down
134 changes: 134 additions & 0 deletions api/src/endpoints/platform_page.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use super::{delete, read, update, ApiResult, CrudHook, CrudRequest};
use crate::{
bad_request, internal_server_error,
server::{AppState, AppStores},
};
use axum::{
extract::State,
routing::{patch, post},
Extension, Json, Router,
};
use convert_case::Case;
use integrationos_domain::{
algebra::{MongoStore, StoreExt},
event_access::EventAccess,
hashed_secret::HashedSecret,
id::{prefix::IdPrefix, Id},
ownership::Owners,
page::PlatformPage,
r#type::PageType,
};
use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::sync::Arc;
use tracing::error;

pub fn get_router() -> Router<Arc<AppState>> {
Router::new()
.route(
"/",
post(create_platform_page).get(read::<CreateRequest, PlatformPage>),
)
.route(
"/:id",
patch(update::<CreateRequest, PlatformPage>)
.delete(delete::<CreateRequest, PlatformPage>),
)
}

#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize)]
#[cfg_attr(feature = "dummy", derive(fake::Dummy))]
#[serde(rename_all = "camelCase")]
pub struct CreateRequest {
pub platform_id: Id,
pub connection_definition_id: Id,
pub platform_name: String,
#[serde(flatten)]
pub r#type: PageType,
pub url: String,
pub model_name: String,
pub content: String,
pub ownership: Owners,
pub analyzed: bool,
}

impl CrudHook<PlatformPage> for CreateRequest {}

pub async fn create_platform_page(
event_access: Option<Extension<Arc<EventAccess>>>,
State(state): State<Arc<AppState>>,
Json(req): Json<CreateRequest>,
) -> ApiResult<PlatformPage> {
let output = if let Some(Extension(event_access)) = event_access {
req.clone().access(event_access)
} else {
req.clone().from()
};

let mut output = match output {
Some(output) => output,
None => return Err(bad_request!("Invalid request")),
};

output.model_name = output.model_name.to_case(Case::Pascal);

let res = match CreateRequest::get_store(state.app_stores.clone())
.create_one(&output)
.await
{
Ok(_) => Ok(Json(output)),
Err(e) => {
error!("Error creating object: {e}");
Err(internal_server_error!())
}
}?;

Ok(res)
}

impl CrudRequest for CreateRequest {
type Output = PlatformPage;

fn output(&self) -> Option<Self::Output> {
let hash_value = json!({
"platform_id": self.platform_id,
"platform_name": self.platform_name,
"model_name": self.model_name,
"page_type": self.r#type,
"content": self.content
});

let hashed = HashedSecret::try_from(hash_value).ok()?;

Some(Self::Output {
id: Id::now(IdPrefix::PlatformPage),
platform_id: self.platform_id,
platform_name: self.platform_name.clone(),
connection_definition_id: self.connection_definition_id,
r#type: self.r#type.clone(),
url: self.url.clone(),
model_name: self.model_name.clone(),
content: self.content.clone(),
hashed_content: hashed.into_inner(),
ownership: self.ownership.clone(),
analyzed: self.analyzed,
record_metadata: Default::default(),
})
}

fn update(&self, record: &mut Self::Output) {
record.platform_id = self.platform_id;
record.connection_definition_id = self.connection_definition_id;
record.r#type = self.r#type.clone();
record.url = self.url.clone();
record.model_name = self.model_name.clone();
record.content = self.content.clone();
record.ownership = self.ownership.clone();
record.analyzed = self.analyzed;
}

fn get_store(stores: AppStores) -> MongoStore<Self::Output> {
stores.platform_page.clone()
}
}
3 changes: 2 additions & 1 deletion api/src/routes/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
endpoints::{
common_model, connection_definition,
connection_model_definition::{self, test_connection_model_definition},
connection_model_schema, connection_oauth_definition, openapi, platform,
connection_model_schema, connection_oauth_definition, openapi, platform, platform_page,
},
middleware::jwt_auth::{self, JwtState},
server::AppState,
Expand Down Expand Up @@ -35,6 +35,7 @@ pub async fn get_router(state: &Arc<AppState>) -> Router<Arc<AppState>> {
connection_model_schema::get_router(),
)
.nest("/platforms", platform::get_router())
.nest("/platform-pages", platform_page::get_router())
.nest("/common-models", common_model::get_router());

routes
Expand Down

0 comments on commit 880bef4

Please sign in to comment.