-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from cyberstudio/add-insight-schemas-api
Add insight schemas API
- Loading branch information
Showing
11 changed files
with
365 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Use this section of API to access Insight schemas. | ||
""" | ||
|
||
from .api import InsightAPI, InsightAsyncAPI | ||
from .schemas import ( | ||
SchemaAPI, | ||
SchemaAsyncAPI, | ||
SchemaView, | ||
SchemaCommonView, | ||
SchemaRegistrationView, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from ..internal import BaseAPI, BaseAsyncAPI | ||
from .schemas import SchemaAPI, SchemaAsyncAPI | ||
|
||
|
||
class InsightAPI(BaseAPI): | ||
"""Insight API.""" | ||
|
||
@property | ||
def schemas(self) -> SchemaAPI: | ||
"""Get Insight schemas handle.""" | ||
return SchemaAPI(self._connector) | ||
|
||
|
||
class InsightAsyncAPI(BaseAsyncAPI): | ||
"""Insight asynchronous API.""" | ||
|
||
@property | ||
def schemas(self) -> SchemaAsyncAPI: | ||
"""Schemas asynchronous API handle.""" | ||
return SchemaAsyncAPI(self._connector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
from typing import Optional | ||
|
||
from ..api import Tag | ||
from ..internal import BaseAPI, BaseAsyncAPI, JsonObject, JsonObjectView | ||
from ..pagination import AsyncPage, Cursor, Page | ||
from ..view import _TaggedView | ||
|
||
_PATH = "/insight/schemas" | ||
|
||
|
||
class SchemaAPI(BaseAPI): | ||
"""Schema API.""" | ||
|
||
def register(self, schema: JsonObject) -> "SchemaRegistrationView": | ||
"""Register an enrichment task result object schema. | ||
Note: | ||
Calls `POST /insight/schemas`. | ||
Args: | ||
schema: JSON schema of the enrichment task result object. | ||
See :ref:`enrichment_result_schemas` | ||
for information about schema structure. | ||
Returns: | ||
Schema registration view. | ||
Raises: | ||
:class:`~cybsi.cloud.error.InvalidRequestError`: | ||
Provided values are invalid (see form value requirements). | ||
:class:`~cybsi.cloud.error.ConflictError`: Form contains conflict errors. | ||
Note: | ||
Conflict error codes specific for this method: | ||
* :attr:`~cybsi.cloud.error.ConflictErrorCodes.DuplicateSchema` | ||
""" | ||
resp = self._connector.do_post(path=_PATH, json=schema) | ||
return SchemaRegistrationView(resp.json()) | ||
|
||
def update( | ||
self, | ||
*, | ||
schema_id: str, | ||
tag: Tag, | ||
schema: JsonObject, | ||
) -> None: | ||
"""Update the enrichment task result object schema. | ||
Note: | ||
Calls `PUT /insight/schemas/{schema_id}`. | ||
Args: | ||
schema_id: URL friendly string, uniquely identifies json schema. | ||
tag: :attr:`SchemaView.tag` value. Use :meth:`view` to retrieve it. | ||
schema: JSON schema of the object. See :ref:`enrichment_result_schemas` | ||
for information about schema structure. | ||
Raises: | ||
:class:`~cybsi.cloud.error.InvalidRequestError`: | ||
Provided values are invalid (see form value requirements). | ||
:class:`~cybsi.cloud.error.SemanticError`: Form contains logic errors. | ||
:class:`~cybsi.cloud.error.ResourceModifiedError`: | ||
Object schema changed since last request. Update tag and retry. | ||
:class:`~cybsi.cloud.error.NotFoundError`: Object schema not found. | ||
Note: | ||
Semantic error codes specific for this method: | ||
* :attr:`~cybsi.cloud.error.SemanticErrorCodes.InvalidSchemaID` | ||
""" | ||
|
||
path = f"{_PATH}/{schema_id}" | ||
self._connector.do_put(path=path, tag=tag, json=schema) | ||
|
||
def view(self, schema_id: str) -> "SchemaView": | ||
"""Get the enrichment task result object schema view. | ||
Note: | ||
Calls `GET /insight/schemas/{schema_id}`. | ||
Args: | ||
schema_id: URL friendly string, uniquely identifies json schema. | ||
Returns: | ||
Schema view. | ||
Raises: | ||
:class:`~cybsi.cloud.error.NotFoundError`: Object schema not found. | ||
""" | ||
|
||
path = f"{_PATH}/{schema_id}" | ||
resp = self._connector.do_get(path=path) | ||
return SchemaView(resp) | ||
|
||
def filter( | ||
self, | ||
*, | ||
cursor: Optional[Cursor] = None, | ||
limit: Optional[int] = None, | ||
) -> Page["SchemaCommonView"]: | ||
"""Get an enrichment task result object schemas filtration list. | ||
Note: | ||
Calls `GET /insight/schemas`. | ||
Args: | ||
cursor: Page cursor. | ||
limit: Page limit. | ||
Returns: | ||
Page with schema common views and next page cursor. | ||
Raises: | ||
:class:`~cybsi.cloud.error.InvalidRequestError`: | ||
Provided values are invalid (see form value requirements). | ||
""" | ||
|
||
params: JsonObject = {} | ||
if cursor is not None: | ||
params["cursor"] = str(cursor) | ||
if limit is not None: | ||
params["limit"] = limit | ||
resp = self._connector.do_get(path=_PATH, params=params) | ||
return Page(self._connector.do_get, resp, SchemaCommonView) | ||
|
||
|
||
class SchemaAsyncAPI(BaseAsyncAPI): | ||
"""Schema asynchronous API.""" | ||
|
||
async def register(self, schema: JsonObject) -> "SchemaRegistrationView": | ||
"""Register an enrichment task result object schema. | ||
Note: | ||
Calls `POST /insight/schemas`. | ||
Args: | ||
schema: JSON schema of the object. See :ref:`enrichment_result_schemas` | ||
for information about schema structure. | ||
Returns: | ||
Schema registration view. | ||
Raises: | ||
:class:`~cybsi.cloud.error.InvalidRequestError`: | ||
Provided values are invalid (see form value requirements). | ||
:class:`~cybsi.cloud.error.ConflictError`: Form contains conflict errors. | ||
Note: | ||
Conflict error codes specific for this method: | ||
* :attr:`~cybsi.cloud.error.ConflictErrorCodes.DuplicateSchema` | ||
""" | ||
resp = await self._connector.do_post(path=_PATH, json=schema) | ||
return SchemaRegistrationView(resp.json()) | ||
|
||
async def update( | ||
self, | ||
*, | ||
schema_id: str, | ||
tag: Tag, | ||
schema: JsonObject, | ||
) -> None: | ||
"""Update the enrichment task result object schema. | ||
Note: | ||
Calls `PUT /insight/schemas/{schema_id}`. | ||
Args: | ||
schema_id: URL friendly string, uniquely identifies json schema. | ||
tag: :attr:`SchemaView.tag` value. Use :meth:`view` to retrieve it. | ||
schema: JSON schema of the object. See :ref:`enrichment_result_schemas` | ||
for information about schema structure. | ||
Raises: | ||
:class:`~cybsi.cloud.error.InvalidRequestError`: | ||
Provided values are invalid (see form value requirements). | ||
:class:`~cybsi.cloud.error.SemanticError`: Form contains logic errors. | ||
:class:`~cybsi.cloud.error.ResourceModifiedError`: | ||
Object schema changed since last request. Update tag and retry. | ||
:class:`~cybsi.cloud.error.NotFoundError`: Object schema not found. | ||
Note: | ||
Semantic error codes specific for this method: | ||
* :attr:`~cybsi.cloud.error.SemanticErrorCodes.InvalidSchemaID` | ||
""" | ||
|
||
path = f"{_PATH}/{schema_id}" | ||
await self._connector.do_put(path=path, tag=tag, json=schema) | ||
|
||
async def view(self, schema_id: str) -> "SchemaView": | ||
"""Get the enrichment task result object schema view. | ||
Note: | ||
Calls `GET /insight/schemas/{schema_id}`. | ||
Args: | ||
schema_id: URL friendly string, uniquely identifies json schema. | ||
Returns: | ||
Schema view. | ||
Raises: | ||
:class:`~cybsi.cloud.error.NotFoundError`: Object schema not found. | ||
""" | ||
|
||
path = f"{_PATH}/{schema_id}" | ||
resp = await self._connector.do_get(path=path) | ||
return SchemaView(resp) | ||
|
||
async def filter( | ||
self, | ||
*, | ||
cursor: Optional[Cursor] = None, | ||
limit: Optional[int] = None, | ||
) -> AsyncPage["SchemaCommonView"]: | ||
"""Get an enrichment task result object schemas filtration list. | ||
Note: | ||
Calls `GET /insight/schemas`. | ||
Args: | ||
cursor: Page cursor. | ||
limit: Page limit. | ||
Returns: | ||
Page with schema common views and next page cursor. | ||
Raises: | ||
:class:`~cybsi.cloud.error.InvalidRequestError`: | ||
Provided values are invalid (see form value requirements). | ||
""" | ||
|
||
params: JsonObject = {} | ||
if cursor is not None: | ||
params["cursor"] = str(cursor) | ||
if limit is not None: | ||
params["limit"] = limit | ||
resp = await self._connector.do_get(path=_PATH, params=params) | ||
return AsyncPage(self._connector.do_get, resp, SchemaCommonView) | ||
|
||
|
||
class SchemaRegistrationView(JsonObjectView): | ||
"""Schema registration view""" | ||
|
||
@property | ||
def schema_id(self) -> str: | ||
"""URL friendly string, uniquely identifies json schema.""" | ||
return self._get("schemaID") | ||
|
||
|
||
class SchemaCommonView(JsonObjectView): | ||
"""Schema common view""" | ||
|
||
@property | ||
def schema_id(self) -> str: | ||
"""URL friendly string, uniquely identifies json schema.""" | ||
return self._get("schemaID") | ||
|
||
@property | ||
def title(self) -> str: | ||
"""The human-readable name of the json schema.""" | ||
return self._get("title") | ||
|
||
|
||
class SchemaView(_TaggedView): | ||
"""Schema view""" | ||
|
||
@property | ||
def schema_id(self) -> str: | ||
"""URL friendly string, uniquely identifies json schema.""" | ||
return self._get("schemaID") | ||
|
||
@property | ||
def schema(self) -> JsonObject: | ||
"""JSON schema of the enrichment task result object | ||
See :ref:`enrichment_result_schemas` for information about schema structure. | ||
""" | ||
return self.raw() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.. _insight: | ||
|
||
Cloud Enrichment Tasks | ||
======================== | ||
|
||
.. _enrichment_result_schemas: | ||
|
||
Enrichment result object schemas | ||
-------------------------------- | ||
|
||
You can make enrichment using object schema | ||
that defines attribute composition of the objects and data types of the attributes | ||
(see :ref:`data_model` for more information). | ||
|
||
To create enrichment tasks, you will need to specify the schema id of the objects that are the results of these tasks. | ||
|
||
In the example bellow we get list of enrichment result object schemas. | ||
|
||
.. literalinclude:: ../../examples/get_enrichment_results_schemas_chained.py | ||
|
||
And one more example of getting schema by ID. | ||
|
||
.. literalinclude:: ../../examples/get_enrichment_results_schema.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python3 | ||
from cybsi.cloud import Client, Config | ||
|
||
if __name__ == "__main__": | ||
config = Config(api_key="the cryptic string") | ||
|
||
with Client(config) as client: | ||
schema_id = "example-schema" | ||
|
||
# Retrieve schema. It describes all attributes of objects you can encounter | ||
# in the result object of the enrichment task with this schema. | ||
schema_view = client.insight.schemas.view(schema_id="example-schema") | ||
|
||
# Do something with the schema as SchemaView. | ||
print(schema_view) |
Oops, something went wrong.