|
| 1 | +# SysML v2 Python Client |
| 2 | + |
| 3 | +A basic Python client library for interacting with a SysML v2 API server, specifically tested against the OpenMBEE Flexo implementation. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +* Provides methods for core SysML v2 operations: |
| 8 | + * **Projects:** Get List, Create, Get by ID |
| 9 | + * **Commits:** Create (handles element create/update/delete), Get by ID, List |
| 10 | + * **Branches:** List, Create, Get by ID, Delete |
| 11 | + * **Tags:** List, Create, Get by ID, Delete |
| 12 | + * **Elements:** Get by ID, List All in Commit, Get Owned by Element |
| 13 | + * **Relationships:** List for Element |
| 14 | +* Handles authentication using Bearer tokens. |
| 15 | +* Includes basic error handling and custom exceptions. |
| 16 | + |
| 17 | +## Setup |
| 18 | + |
| 19 | +### 1. Run Flexo SysMLv2 Service Locally |
| 20 | + |
| 21 | +This client is designed to work with a running instance of the OpenMBEE Flexo SysMLv2 service. |
| 22 | + |
| 23 | +* **Prerequisites:** Docker and Docker Compose installed. |
| 24 | +* **Get Setup Files:** The necessary `docker-compose.yml` and configuration files are located in the `flexo-setup/docker-compose/` directory within this project (downloaded from `Open-MBEE/flexo-mms-sysmlv2`). |
| 25 | +* **Start Services:** Navigate to the `flexo-setup/docker-compose/` directory in your terminal and run: |
| 26 | + ```bash |
| 27 | + docker compose up -d |
| 28 | + ``` |
| 29 | +* **Initial Org Setup (Potential Manual Step):** For a fresh database, you may need to perform an initial organization setup using Postman as described in `flexo-setup/docker-compose/README.md`. |
| 30 | +* **Authentication Token:** The required Bearer token for the client is found in `flexo-setup/docker-compose/env/flexo-sysmlv2.env` under the `FLEXO_AUTH` variable. Copy this entire value (including `Bearer `). |
| 31 | + |
| 32 | +### 2. Install Client (Development) |
| 33 | + |
| 34 | + |
| 35 | +```python |
| 36 | +# Example: Add src to path if running scripts/notebooks from project root |
| 37 | +import sys |
| 38 | +import os |
| 39 | +sys.path.insert(0, os.path.abspath('./src')) |
| 40 | +
|
| 41 | +from sysmlv2_client import SysMLV2Client |
| 42 | +``` |
| 43 | + |
| 44 | +## Basic Usage |
| 45 | + |
| 46 | +```python |
| 47 | +from sysmlv2_client import SysMLV2Client, SysMLV2Error |
| 48 | +from pprint import pprint |
| 49 | +import uuid # For element IDs |
| 50 | +
|
| 51 | +BASE_URL = "http://localhost:8083" |
| 52 | +# Replace with the actual token from flexo-setup/docker-compose/env/flexo-sysmlv2.env |
| 53 | +BEARER_TOKEN = "Bearer YOUR_TOKEN_HERE" |
| 54 | +
|
| 55 | +try: |
| 56 | + client = SysMLV2Client(base_url=BASE_URL, bearer_token=BEARER_TOKEN) |
| 57 | + print("Client initialized.") |
| 58 | +
|
| 59 | + # Get projects |
| 60 | + print("\\n--- Getting Projects ---") |
| 61 | + projects = client.get_projects() |
| 62 | + print(f"Found {len(projects)} projects.") |
| 63 | + for project in projects: |
| 64 | + print(f" - Name: {project.get('name', 'N/A')}, ID: {project.get('@id', 'N/A')}") |
| 65 | +
|
| 66 | + # Create a project (example data) |
| 67 | + print("\\n--- Creating Project ---") |
| 68 | + new_proj_data = {"@type": "Project", "name": "Client README Example"} |
| 69 | + created_proj = client.create_project(new_proj_data) |
| 70 | + print(f"Created project:") |
| 71 | + pprint(created_proj) |
| 72 | + project_id = created_proj.get('@id') |
| 73 | +
|
| 74 | + if project_id: |
| 75 | + # Create a commit that also creates an element |
| 76 | + print(f"\\n--- Creating Commit with Element in Project {project_id} ---") |
| 77 | + element_id = str(uuid.uuid4()) |
| 78 | + commit_data = { |
| 79 | + "@type": "Commit", |
| 80 | + "description": "Add initial block", |
| 81 | + "change": [{ |
| 82 | + "@type": "DataVersion", |
| 83 | + "payload": { |
| 84 | + "@id": element_id, |
| 85 | + "@type": "BlockDefinition", # Example type |
| 86 | + "name": "MyExampleBlock" |
| 87 | + } |
| 88 | + }] |
| 89 | + } |
| 90 | + created_commit = client.create_commit(project_id, commit_data) |
| 91 | + print("Commit created:") |
| 92 | + pprint(created_commit) |
| 93 | + commit_id = created_commit.get('@id') |
| 94 | +
|
| 95 | + if commit_id: |
| 96 | + # List elements in the new commit |
| 97 | + print(f"\\n--- Listing Elements in Commit {commit_id} ---") |
| 98 | + elements = client.list_elements(project_id, commit_id) |
| 99 | + print(f"Found {len(elements)} elements:") |
| 100 | + pprint(elements) |
| 101 | +
|
| 102 | +except SysMLV2Error as e: |
| 103 | + print(f"An API error occurred: {e}") |
| 104 | +except ValueError as e: |
| 105 | + print(f"Initialization error: {e}") |
| 106 | +except Exception as e: |
| 107 | + print(f"An unexpected error occurred: {e}") |
| 108 | +
|
| 109 | +``` |
| 110 | + |
| 111 | +See the [examples/basic_usage.ipynb](examples/basic_usage.ipynb) Jupyter Notebook for more detailed examples covering commits, branches, tags, and element retrieval/modification via commits. |
| 112 | + |
| 113 | +## Running Tests |
| 114 | + |
| 115 | +Unit tests are implemented using `pytest` and `requests-mock`. |
| 116 | + |
| 117 | +1. **Install Dependencies:** |
| 118 | + ```bash |
| 119 | + pip install pytest requests requests-mock |
| 120 | + ``` |
| 121 | +2. **Run Tests:** Navigate to the project root directory in your terminal and run: |
| 122 | + ```bash |
| 123 | + pytest |
| 124 | + ``` |
0 commit comments