Last active
June 23, 2021 08:33
-
-
Save soofstad/61a9d771b44c6389d51b24cb3554ed73 to your computer and use it in GitHub Desktop.
Example to search the Data Modeling Storage Service for entities using python. Requires Python version >= 3.6
This file contains hidden or 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
#! /usr/bin/env python3 | |
import pprint | |
import sys | |
from typing import Dict | |
import requests # https://docs.python-requests.org/en/latest/user/quickstart/ | |
print(f"Python version {sys.version}\n") | |
pretty_printer = pprint.PrettyPrinter() | |
DMSS_HOST = "localhost" # NOTE: If you are running none-default ports (80 & 443), append that here | |
def search(data_source: str, query: dict) -> Dict[str, dict]: | |
url: str = f"http://{DMSS_HOST}/dmss/api/v1/search/{data_source}" | |
request: Request = requests.post(url=url, json=query) | |
request.raise_for_status() # Raise exception if response code is not OK | |
return request.json() | |
def listEveryWindTurbineInDataSource(): | |
# Fetch every entity of the 'WindTurbine'-type in the 'EntityApp-DS' data source | |
query = {"type": "EntityApp-DS/DMT-demo/UncontainedDemo/WindTurbine"} | |
response = search(data_source="EntityApp-DS", query=query) | |
print(f"Found {len(response)} entitie(s)!") | |
for key, value in response.items(): | |
pretty_printer.pprint(value) | |
def listEveryDemoRootPackage(): | |
# Fetch every entity of the 'Package'-type in the 'EntityApp-DS' data source | |
# which matches regex-like '*demo*' for the 'name' attribute, and the 'isRoot' attribute is a boolean 'True' | |
query = { | |
"type": "system/SIMOS/Package", | |
"name": "demo", | |
"isRoot": "true" | |
} | |
response = search(data_source="EntityApp-DS", query=query) | |
print(f"\nFound {len(response)} entitie(s)!") | |
for key, value in response.items(): | |
pretty_printer.pprint(value) | |
if __name__ == '__main__': | |
listEveryWindTurbineInDataSource() | |
listEveryDemoRootPackage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment