Python SDK
The Python SDK generated from the OpenAPI document (bimworks on the BIM.works Python index) — the package, connecting and logging in, the four steps of every example, the API classes and models, errors, files and uploads.
The Python SDK is generated from the OpenAPI document: one API class per group of operations, one method per operation, and a pydantic model per request and response. It talks HTTPS through urllib3.
Package#
| Package | bimworks on https://pypi.bim.works/simple/, version equal to your BIM.works server in its Python spelling (3.0.0b120) (Python) |
| Python | 3.9 or later |
| Module | bimworks: every API class and model is importable from it (bimworks.IFCApi, bimworks.QueryRequest) |
| Dependencies | pydantic 2, urllib3 2, python-dateutil and typing-extensions, from pypi.org |
Connect and log in#
An ApiClient holds a Configuration with the address of your instance's OpenAPI root (BIM_WORKS_ADDRESS/openapi); every API class is made from it. The login, LoginWithApiToken with your API token, answers an AuthResult, a wrapper whose actual_instance is the Auth object; its var_base64 (the JSON's base64) is the auth token. Setting it as the configuration's access_token makes every later call send it.
import json
import bimworks
# 1. Connect to BIM.works and log in with your API token
client = bimworks.ApiClient(bimworks.Configuration(host="https://bim.works/openapi"))
auth = bimworks.AuthApi(client).login_with_api_token(
bimworks.LoginWithApiTokenRequest(token="<your API token>"))
client.configuration.access_token = auth.actual_instance.var_base64
# 2. The request
# (GetRootNodeUuid takes no request)
# 3. The call
result = bimworks.FsApi(client).get_root_node_uuid()
# 4. The result
print(json.dumps(client.sanitize_for_serialization(result), indent=2))Every example in four steps#
Every example on these pages and on the operation pages of the API part is a complete script in the same four steps (reference/sdk#example-shape):
- Connect — the client, logged in as above. An operation that needs no login (the logins, GetApplicationVersion) skips the login.
- The request —
Model.from_json(json)makes the request from the JSON with the wire names that the operation's page shows, in a raw string, so nothing in it is escaped. Multipart operations take their form fields as keyword arguments instead; a file is its path. - The call —
bimworks.Api(client).method(request). - The result —
client.sanitize_for_serialization(result), which gives the wire names, printed as JSON; a file is written to disk, and a call without a result printsDone.
So the same JSON goes into every language, and copying an example is enough once the package is installed. With several calls, keep one client and repeat steps 2 to 4.
Classes#
A model's attributes are snake_case Python names; from_json, from_dict, to_dict and to_json read and write the wire names. Build a request with from_json rather than from a dict passed to the method: only from_json picks a model identifier's subclass by its type, and with a dict the identifier loses its uuid. A key the model does not have is dropped, so use the names on the models page.
Errors#
A call that the server answers with an error status raises bimworks.ApiException, or one of its subclasses in bimworks.exceptions (BadRequestException, UnauthorizedException, ForbiddenException, NotFoundException, ServiceException): status is the HTTP status and body the server's error (Errors).
try:
bimworks.IFCApi(client).query(request)
except bimworks.ApiException as e:
print(e.status, e.body)Files#
- Downloads return
bytes, the whole file in memory. For a large file, the…_without_preload_contentvariant of a method (download_without_preload_content) returns the HTTP response to stream from; it does not raise on an error status, so checkstatusyourself. - Uploads take a path (or
bytes); the SDK reads the whole file into memory.
Pages#
- Java SDKThe Java SDK generated from the OpenAPI document (Maven nl.bimbase.bimworks:sdk) — the artifact, connecting and logging in, the four steps of every example, the API classes and models, errors, files and uploads.
- .NET SDKThe .NET SDK generated from the OpenAPI document (NuGet BIMworks.Sdk) — the package, connecting and logging in, the four steps of every example, the API classes and models, responses and errors, files and uploads.
- TypeScript SDKThe TypeScript and JavaScript SDK generated from the OpenAPI document (npm @bimworks/sdk) — installation, connecting and logging in, the four steps of every example, the API classes and models, errors, files and uploads.
- PHP SDKThe PHP SDK generated from the OpenAPI document (Composer bimbase/bimworks) — the package, connecting and logging in, the four steps of every example, the API classes and models, errors, files and uploads.
- Python SDKThe Python SDK generated from the OpenAPI document (bimworks on the BIM.works Python index) — the package, connecting and logging in, the four steps of every example, the API classes and models, errors, files and uploads.