ibmrpa-sdk 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ibmrpa_sdk-0.1.0/LICENSE +21 -0
- ibmrpa_sdk-0.1.0/PKG-INFO +255 -0
- ibmrpa_sdk-0.1.0/README.md +227 -0
- ibmrpa_sdk-0.1.0/pyproject.toml +48 -0
- ibmrpa_sdk-0.1.0/setup.cfg +4 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/__init__.py +6 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/async_client.py +132 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/auth.py +27 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/client.py +139 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/config.py +21 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/endpoints/__init__.py +1 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/endpoints/authentication.py +80 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/endpoints/bots.py +76 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/endpoints/process_management.py +49 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/endpoints/projects.py +52 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/endpoints/tenant_management.py +42 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/endpoints/workspace.py +19 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/exceptions.py +36 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/models/__init__.py +5 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/utils/logging.py +15 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk/utils/retry.py +40 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk.egg-info/PKG-INFO +255 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk.egg-info/SOURCES.txt +25 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk.egg-info/dependency_links.txt +1 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk.egg-info/requires.txt +11 -0
- ibmrpa_sdk-0.1.0/src/ibmrpa_sdk.egg-info/top_level.txt +1 -0
- ibmrpa_sdk-0.1.0/tests/test_client.py +34 -0
ibmrpa_sdk-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rodrigo Vitorino
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ibmrpa_sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Unofficial Python SDK for the IBM RPA SaaS REST APIs
|
|
5
|
+
Author: Rodrigo Vitorino
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: ibm,rpa,robotic process automation,sdk,api,httpx
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Requires-Dist: httpx>=0.24.0
|
|
18
|
+
Requires-Dist: pydantic>=2.0.0
|
|
19
|
+
Requires-Dist: tenacity>=8.2.0
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: pytest>=7.0.0; extra == "dev"
|
|
22
|
+
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
|
|
23
|
+
Requires-Dist: respx>=0.20.0; extra == "dev"
|
|
24
|
+
Requires-Dist: ruff>=0.1.0; extra == "dev"
|
|
25
|
+
Requires-Dist: mypy>=1.0.0; extra == "dev"
|
|
26
|
+
Requires-Dist: types-requests; extra == "dev"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# ibmrpa-sdk
|
|
30
|
+
|
|
31
|
+
Unofficial Python SDK for the **IBM RPA SaaS** REST APIs: authentication, bots, orchestration processes, projects,
|
|
32
|
+
tenants and workspace. Built on `httpx`, `pydantic` and `tenacity`. The client adds the bearer token, retries
|
|
33
|
+
transient failures and turns HTTP errors into exceptions.
|
|
34
|
+
|
|
35
|
+
> **Unofficial.** This project is not affiliated with, endorsed by or supported by IBM. IBM is a trademark of
|
|
36
|
+
> International Business Machines Corporation.
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install ibmrpa-sdk
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Requires Python 3.9 or newer. The import name is `ibmrpa_sdk`.
|
|
45
|
+
|
|
46
|
+
## Configuration
|
|
47
|
+
|
|
48
|
+
The client reads these environment variables, or takes the same values in its constructor:
|
|
49
|
+
|
|
50
|
+
| Variable | Purpose | Default |
|
|
51
|
+
|---|---|---|
|
|
52
|
+
| `IBMRPA_BASE_URL` | API server of your region, scheme and host only | required |
|
|
53
|
+
| `IBMRPA_TOKEN` | `accessToken` returned by `get_token` | no token |
|
|
54
|
+
| `IBMRPA_TIMEOUT` | Timeout per request, in seconds | `30` |
|
|
55
|
+
| `IBMRPA_VERIFY_SSL` | Verify the server TLS certificate | `true` |
|
|
56
|
+
| `IBMRPA_RETRY_MAX_ATTEMPTS` | Attempts on timeouts, connection errors and HTTP 429 | `3` |
|
|
57
|
+
| `IBMRPA_RETRY_BACKOFF` | Exponential backoff multiplier (waits between 1 s and 60 s) | `0.5` |
|
|
58
|
+
|
|
59
|
+
Regions, as returned by `list_regions` in November 2026:
|
|
60
|
+
|
|
61
|
+
| Region | Location | `IBMRPA_BASE_URL` |
|
|
62
|
+
|---|---|---|
|
|
63
|
+
| BR1 | Brazil South | `https://br1api.rpa.ibm.com` |
|
|
64
|
+
| BR2 | Brazil South (BR2) | `https://br2api.rpa.ibm.com` |
|
|
65
|
+
| EU1 | Germany West Central | `https://eu1api.rpa.ibm.com` |
|
|
66
|
+
| US1 | South Central US | `https://us1api.rpa.ibm.com` |
|
|
67
|
+
| AP1 | Southeast Asia | `https://ap1api.rpa.ibm.com` |
|
|
68
|
+
| UK1 | UK South | `https://uk1api.rpa.ibm.com` |
|
|
69
|
+
|
|
70
|
+
`list_regions` returns `apiUrl` values that end in `/v1.0/`. Use only the scheme and host as `base_url`, because the
|
|
71
|
+
SDK adds `/v1.0/...` or `/v2.0/...` to each call.
|
|
72
|
+
|
|
73
|
+
The SDK does not read usernames or passwords. The examples below use `IBMRPA_USERNAME` and `IBMRPA_PASSWORD`, which
|
|
74
|
+
you set yourself. Never hardcode credentials.
|
|
75
|
+
|
|
76
|
+
## Quickstart
|
|
77
|
+
|
|
78
|
+
```python
|
|
79
|
+
import os
|
|
80
|
+
from ibmrpa_sdk import IbmRpaClient
|
|
81
|
+
|
|
82
|
+
BASE_URL = os.environ["IBMRPA_BASE_URL"]
|
|
83
|
+
USERNAME = os.environ["IBMRPA_USERNAME"]
|
|
84
|
+
|
|
85
|
+
# The regions, account/tenant and token endpoints do not need a bearer token.
|
|
86
|
+
with IbmRpaClient(base_url=BASE_URL) as anon:
|
|
87
|
+
tenants = anon.authentication.list_tenants_by_user(USERNAME) # [{"id": ..., "name": ...}]
|
|
88
|
+
resp = anon.authentication.get_token(
|
|
89
|
+
username=USERNAME,
|
|
90
|
+
password=os.environ["IBMRPA_PASSWORD"],
|
|
91
|
+
tenant_id=tenants[0]["id"],
|
|
92
|
+
)
|
|
93
|
+
token = resp["accessToken"] # valid for resp["expiresIn"] seconds (about 24 h): reuse it
|
|
94
|
+
|
|
95
|
+
client = IbmRpaClient(base_url=BASE_URL, token=token)
|
|
96
|
+
workspace_id = client.workspace.get_active_workspace()[0]["id"] # workspace ID == tenant ID
|
|
97
|
+
|
|
98
|
+
job = client.bots.run_bot_by_unique_id(
|
|
99
|
+
workspace_id,
|
|
100
|
+
project_unique_id="my-project",
|
|
101
|
+
bot_unique_id="my-bot",
|
|
102
|
+
input_variables={"in_customer": "ACME", "in_order": 12345},
|
|
103
|
+
callback_url="https://example.com/rpa/callback", # optional
|
|
104
|
+
)
|
|
105
|
+
print(job["jobId"], job.get("statusName"))
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`client.bots.run_bot(workspace_id, project_id, bot_id, input_variables)` does the same with GUIDs. The keys of
|
|
109
|
+
`input_variables` are the names of the input variables of the bot's script. The API has no job-status endpoint, so
|
|
110
|
+
get the result through `callback_url`, or run the work as an orchestration process (see below).
|
|
111
|
+
|
|
112
|
+
The examples below reuse `client` and `workspace_id`. Call `client.close()` when you are done, or use
|
|
113
|
+
`with IbmRpaClient(...) as client:` to close the connection automatically.
|
|
114
|
+
|
|
115
|
+
## More examples
|
|
116
|
+
|
|
117
|
+
### List regions
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from urllib.parse import urlsplit
|
|
121
|
+
from ibmrpa_sdk import IbmRpaClient
|
|
122
|
+
|
|
123
|
+
with IbmRpaClient(base_url="https://br1api.rpa.ibm.com") as anon:
|
|
124
|
+
for region in anon.authentication.list_regions(): # [{"name", "description", "apiUrl"}]
|
|
125
|
+
api = urlsplit(region["apiUrl"])
|
|
126
|
+
print(region["name"], region["description"], f"{api.scheme}://{api.netloc}")
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Orchestration process: create an instance and poll it
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
import time
|
|
133
|
+
|
|
134
|
+
PROCESS_ID = "00000000-0000-0000-0000-000000000000"
|
|
135
|
+
|
|
136
|
+
instance_id = client.process.create_process_instance(
|
|
137
|
+
workspace_id, PROCESS_ID, payload={"customer": "ACME", "priority": "high"}
|
|
138
|
+
)["id"]
|
|
139
|
+
|
|
140
|
+
for _ in range(60): # about 10 minutes
|
|
141
|
+
info = client.process.get_process_instance(workspace_id, PROCESS_ID, instance_id)
|
|
142
|
+
if info.get("statusName") == "done":
|
|
143
|
+
print(info.get("outputs"))
|
|
144
|
+
break
|
|
145
|
+
time.sleep(10)
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Project OpenAPI document
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
doc = client.projects.get_openapi_by_unique_id(workspace_id, "my-project")
|
|
152
|
+
print(doc["content"] if isinstance(doc, dict) and "content" in doc else doc) # YAML text, or parsed JSON
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`client.projects.get_openapi_by_id(workspace_id, project_id)` does the same with the project GUID.
|
|
156
|
+
|
|
157
|
+
### Tenants (requires the Platform administrator role)
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
info = client.tenant.get_tenant(workspace_id)
|
|
161
|
+
print(info["name"], "suspended" if info.get("isDeactivated") else "active")
|
|
162
|
+
|
|
163
|
+
# Suspending blocks all use of the tenant. Run these only on purpose:
|
|
164
|
+
# client.tenant.suspend_tenant(workspace_id)
|
|
165
|
+
# client.tenant.resume_tenant(workspace_id)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Endpoints without a dedicated method
|
|
169
|
+
|
|
170
|
+
`client.request(method, path, **kwargs)` calls any endpoint with the token, retries and error handling applied.
|
|
171
|
+
Extra keyword arguments go to httpx (`params`, `json`, `data`, `headers`), and it returns an `httpx.Response`:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
projects = client.request("GET", f"/v2.0/workspace/{workspace_id}/projects").json()
|
|
175
|
+
page = client.request(
|
|
176
|
+
"GET", f"/v2.0/workspace/{workspace_id}/process", params={"Search": "billing", "Limit": 50}
|
|
177
|
+
).json()
|
|
178
|
+
print([p["uniqueId"] for p in projects], page["total"])
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Errors
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
from ibmrpa_sdk.exceptions import (
|
|
185
|
+
IbmRpaConnectionError,
|
|
186
|
+
IbmRpaHttpError,
|
|
187
|
+
IbmRpaRateLimitError,
|
|
188
|
+
IbmRpaTimeoutError,
|
|
189
|
+
)
|
|
190
|
+
|
|
191
|
+
try:
|
|
192
|
+
client.workspace.get_active_workspace()
|
|
193
|
+
except IbmRpaRateLimitError: # HTTP 429 after all retries (a subclass of IbmRpaHttpError)
|
|
194
|
+
print("rate limited, try again later")
|
|
195
|
+
except IbmRpaHttpError as e: # other 4xx and 5xx
|
|
196
|
+
print(e.status_code, e.body) # {"errorMessage": "...", "modelState": {...}}
|
|
197
|
+
except (IbmRpaTimeoutError, IbmRpaConnectionError) as e:
|
|
198
|
+
print("network error:", e)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
All exceptions derive from `ibmrpa_sdk.exceptions.IbmRpaError`. A 404 can mean missing permissions, not only a
|
|
202
|
+
missing resource.
|
|
203
|
+
|
|
204
|
+
### Advanced configuration
|
|
205
|
+
|
|
206
|
+
```python
|
|
207
|
+
import httpx
|
|
208
|
+
from ibmrpa_sdk import IbmRpaClient, Settings
|
|
209
|
+
|
|
210
|
+
settings = Settings(base_url=BASE_URL, token=token, timeout=60, retry_max_attempts=5)
|
|
211
|
+
with IbmRpaClient(settings=settings) as c:
|
|
212
|
+
print(c.workspace.get_active_workspace())
|
|
213
|
+
|
|
214
|
+
# Your own httpx client (proxies, certificates, limits) is used as is: set base_url and timeout on it too.
|
|
215
|
+
with IbmRpaClient(base_url=BASE_URL, token=token, http_client=httpx.Client(base_url=BASE_URL, timeout=60)) as c:
|
|
216
|
+
print(c.workspace.get_active_workspace())
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## API coverage
|
|
220
|
+
|
|
221
|
+
| API | Methods |
|
|
222
|
+
|---|---|
|
|
223
|
+
| Authentication | `list_regions()`, `list_tenants_by_user(username)`, `get_token(...)` |
|
|
224
|
+
| Bots | `run_bot(...)`, `run_bot_by_unique_id(...)` |
|
|
225
|
+
| Process Management | `create_process_instance(...)`, `get_process_instance(...)` |
|
|
226
|
+
| Projects | `get_openapi_by_id(...)`, `get_openapi_by_unique_id(...)` |
|
|
227
|
+
| Tenant Management | `get_tenant(...)`, `suspend_tenant(...)`, `resume_tenant(...)` |
|
|
228
|
+
| Workspace | `get_active_workspace()` |
|
|
229
|
+
|
|
230
|
+
Not covered yet (use `client.request`): list processes, get a process, list projects, create a tenant,
|
|
231
|
+
`DELETE /v2.0/tenant/{tenantId}`, and SSO login with the `oidc-access-token` header.
|
|
232
|
+
|
|
233
|
+
## Known limitations
|
|
234
|
+
|
|
235
|
+
- The endpoint methods of `AsyncIbmRpaClient` do not work yet and raise
|
|
236
|
+
`AttributeError: 'coroutine' object has no attribute 'json'`. `await client.request(...)` works:
|
|
237
|
+
|
|
238
|
+
```python
|
|
239
|
+
import asyncio
|
|
240
|
+
from ibmrpa_sdk import AsyncIbmRpaClient
|
|
241
|
+
|
|
242
|
+
async def main():
|
|
243
|
+
async with AsyncIbmRpaClient(base_url=BASE_URL, token=token) as aclient:
|
|
244
|
+
print((await aclient.request("GET", "/v2.0/workspace")).json())
|
|
245
|
+
|
|
246
|
+
asyncio.run(main())
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
- Retries also apply to `POST` requests that time out, so a timed-out `run_bot` call can start the bot twice. Set
|
|
250
|
+
`IBMRPA_RETRY_MAX_ATTEMPTS=1` for critical calls.
|
|
251
|
+
- `get_openapi_*` can return a `dict`, a `str` or `{"content": text}`, depending on the response.
|
|
252
|
+
|
|
253
|
+
## License
|
|
254
|
+
|
|
255
|
+
MIT
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# ibmrpa-sdk
|
|
2
|
+
|
|
3
|
+
Unofficial Python SDK for the **IBM RPA SaaS** REST APIs: authentication, bots, orchestration processes, projects,
|
|
4
|
+
tenants and workspace. Built on `httpx`, `pydantic` and `tenacity`. The client adds the bearer token, retries
|
|
5
|
+
transient failures and turns HTTP errors into exceptions.
|
|
6
|
+
|
|
7
|
+
> **Unofficial.** This project is not affiliated with, endorsed by or supported by IBM. IBM is a trademark of
|
|
8
|
+
> International Business Machines Corporation.
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
pip install ibmrpa-sdk
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Requires Python 3.9 or newer. The import name is `ibmrpa_sdk`.
|
|
17
|
+
|
|
18
|
+
## Configuration
|
|
19
|
+
|
|
20
|
+
The client reads these environment variables, or takes the same values in its constructor:
|
|
21
|
+
|
|
22
|
+
| Variable | Purpose | Default |
|
|
23
|
+
|---|---|---|
|
|
24
|
+
| `IBMRPA_BASE_URL` | API server of your region, scheme and host only | required |
|
|
25
|
+
| `IBMRPA_TOKEN` | `accessToken` returned by `get_token` | no token |
|
|
26
|
+
| `IBMRPA_TIMEOUT` | Timeout per request, in seconds | `30` |
|
|
27
|
+
| `IBMRPA_VERIFY_SSL` | Verify the server TLS certificate | `true` |
|
|
28
|
+
| `IBMRPA_RETRY_MAX_ATTEMPTS` | Attempts on timeouts, connection errors and HTTP 429 | `3` |
|
|
29
|
+
| `IBMRPA_RETRY_BACKOFF` | Exponential backoff multiplier (waits between 1 s and 60 s) | `0.5` |
|
|
30
|
+
|
|
31
|
+
Regions, as returned by `list_regions` in November 2026:
|
|
32
|
+
|
|
33
|
+
| Region | Location | `IBMRPA_BASE_URL` |
|
|
34
|
+
|---|---|---|
|
|
35
|
+
| BR1 | Brazil South | `https://br1api.rpa.ibm.com` |
|
|
36
|
+
| BR2 | Brazil South (BR2) | `https://br2api.rpa.ibm.com` |
|
|
37
|
+
| EU1 | Germany West Central | `https://eu1api.rpa.ibm.com` |
|
|
38
|
+
| US1 | South Central US | `https://us1api.rpa.ibm.com` |
|
|
39
|
+
| AP1 | Southeast Asia | `https://ap1api.rpa.ibm.com` |
|
|
40
|
+
| UK1 | UK South | `https://uk1api.rpa.ibm.com` |
|
|
41
|
+
|
|
42
|
+
`list_regions` returns `apiUrl` values that end in `/v1.0/`. Use only the scheme and host as `base_url`, because the
|
|
43
|
+
SDK adds `/v1.0/...` or `/v2.0/...` to each call.
|
|
44
|
+
|
|
45
|
+
The SDK does not read usernames or passwords. The examples below use `IBMRPA_USERNAME` and `IBMRPA_PASSWORD`, which
|
|
46
|
+
you set yourself. Never hardcode credentials.
|
|
47
|
+
|
|
48
|
+
## Quickstart
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
import os
|
|
52
|
+
from ibmrpa_sdk import IbmRpaClient
|
|
53
|
+
|
|
54
|
+
BASE_URL = os.environ["IBMRPA_BASE_URL"]
|
|
55
|
+
USERNAME = os.environ["IBMRPA_USERNAME"]
|
|
56
|
+
|
|
57
|
+
# The regions, account/tenant and token endpoints do not need a bearer token.
|
|
58
|
+
with IbmRpaClient(base_url=BASE_URL) as anon:
|
|
59
|
+
tenants = anon.authentication.list_tenants_by_user(USERNAME) # [{"id": ..., "name": ...}]
|
|
60
|
+
resp = anon.authentication.get_token(
|
|
61
|
+
username=USERNAME,
|
|
62
|
+
password=os.environ["IBMRPA_PASSWORD"],
|
|
63
|
+
tenant_id=tenants[0]["id"],
|
|
64
|
+
)
|
|
65
|
+
token = resp["accessToken"] # valid for resp["expiresIn"] seconds (about 24 h): reuse it
|
|
66
|
+
|
|
67
|
+
client = IbmRpaClient(base_url=BASE_URL, token=token)
|
|
68
|
+
workspace_id = client.workspace.get_active_workspace()[0]["id"] # workspace ID == tenant ID
|
|
69
|
+
|
|
70
|
+
job = client.bots.run_bot_by_unique_id(
|
|
71
|
+
workspace_id,
|
|
72
|
+
project_unique_id="my-project",
|
|
73
|
+
bot_unique_id="my-bot",
|
|
74
|
+
input_variables={"in_customer": "ACME", "in_order": 12345},
|
|
75
|
+
callback_url="https://example.com/rpa/callback", # optional
|
|
76
|
+
)
|
|
77
|
+
print(job["jobId"], job.get("statusName"))
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
`client.bots.run_bot(workspace_id, project_id, bot_id, input_variables)` does the same with GUIDs. The keys of
|
|
81
|
+
`input_variables` are the names of the input variables of the bot's script. The API has no job-status endpoint, so
|
|
82
|
+
get the result through `callback_url`, or run the work as an orchestration process (see below).
|
|
83
|
+
|
|
84
|
+
The examples below reuse `client` and `workspace_id`. Call `client.close()` when you are done, or use
|
|
85
|
+
`with IbmRpaClient(...) as client:` to close the connection automatically.
|
|
86
|
+
|
|
87
|
+
## More examples
|
|
88
|
+
|
|
89
|
+
### List regions
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
from urllib.parse import urlsplit
|
|
93
|
+
from ibmrpa_sdk import IbmRpaClient
|
|
94
|
+
|
|
95
|
+
with IbmRpaClient(base_url="https://br1api.rpa.ibm.com") as anon:
|
|
96
|
+
for region in anon.authentication.list_regions(): # [{"name", "description", "apiUrl"}]
|
|
97
|
+
api = urlsplit(region["apiUrl"])
|
|
98
|
+
print(region["name"], region["description"], f"{api.scheme}://{api.netloc}")
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Orchestration process: create an instance and poll it
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
import time
|
|
105
|
+
|
|
106
|
+
PROCESS_ID = "00000000-0000-0000-0000-000000000000"
|
|
107
|
+
|
|
108
|
+
instance_id = client.process.create_process_instance(
|
|
109
|
+
workspace_id, PROCESS_ID, payload={"customer": "ACME", "priority": "high"}
|
|
110
|
+
)["id"]
|
|
111
|
+
|
|
112
|
+
for _ in range(60): # about 10 minutes
|
|
113
|
+
info = client.process.get_process_instance(workspace_id, PROCESS_ID, instance_id)
|
|
114
|
+
if info.get("statusName") == "done":
|
|
115
|
+
print(info.get("outputs"))
|
|
116
|
+
break
|
|
117
|
+
time.sleep(10)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Project OpenAPI document
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
doc = client.projects.get_openapi_by_unique_id(workspace_id, "my-project")
|
|
124
|
+
print(doc["content"] if isinstance(doc, dict) and "content" in doc else doc) # YAML text, or parsed JSON
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
`client.projects.get_openapi_by_id(workspace_id, project_id)` does the same with the project GUID.
|
|
128
|
+
|
|
129
|
+
### Tenants (requires the Platform administrator role)
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
info = client.tenant.get_tenant(workspace_id)
|
|
133
|
+
print(info["name"], "suspended" if info.get("isDeactivated") else "active")
|
|
134
|
+
|
|
135
|
+
# Suspending blocks all use of the tenant. Run these only on purpose:
|
|
136
|
+
# client.tenant.suspend_tenant(workspace_id)
|
|
137
|
+
# client.tenant.resume_tenant(workspace_id)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Endpoints without a dedicated method
|
|
141
|
+
|
|
142
|
+
`client.request(method, path, **kwargs)` calls any endpoint with the token, retries and error handling applied.
|
|
143
|
+
Extra keyword arguments go to httpx (`params`, `json`, `data`, `headers`), and it returns an `httpx.Response`:
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
projects = client.request("GET", f"/v2.0/workspace/{workspace_id}/projects").json()
|
|
147
|
+
page = client.request(
|
|
148
|
+
"GET", f"/v2.0/workspace/{workspace_id}/process", params={"Search": "billing", "Limit": 50}
|
|
149
|
+
).json()
|
|
150
|
+
print([p["uniqueId"] for p in projects], page["total"])
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Errors
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from ibmrpa_sdk.exceptions import (
|
|
157
|
+
IbmRpaConnectionError,
|
|
158
|
+
IbmRpaHttpError,
|
|
159
|
+
IbmRpaRateLimitError,
|
|
160
|
+
IbmRpaTimeoutError,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
try:
|
|
164
|
+
client.workspace.get_active_workspace()
|
|
165
|
+
except IbmRpaRateLimitError: # HTTP 429 after all retries (a subclass of IbmRpaHttpError)
|
|
166
|
+
print("rate limited, try again later")
|
|
167
|
+
except IbmRpaHttpError as e: # other 4xx and 5xx
|
|
168
|
+
print(e.status_code, e.body) # {"errorMessage": "...", "modelState": {...}}
|
|
169
|
+
except (IbmRpaTimeoutError, IbmRpaConnectionError) as e:
|
|
170
|
+
print("network error:", e)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
All exceptions derive from `ibmrpa_sdk.exceptions.IbmRpaError`. A 404 can mean missing permissions, not only a
|
|
174
|
+
missing resource.
|
|
175
|
+
|
|
176
|
+
### Advanced configuration
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
import httpx
|
|
180
|
+
from ibmrpa_sdk import IbmRpaClient, Settings
|
|
181
|
+
|
|
182
|
+
settings = Settings(base_url=BASE_URL, token=token, timeout=60, retry_max_attempts=5)
|
|
183
|
+
with IbmRpaClient(settings=settings) as c:
|
|
184
|
+
print(c.workspace.get_active_workspace())
|
|
185
|
+
|
|
186
|
+
# Your own httpx client (proxies, certificates, limits) is used as is: set base_url and timeout on it too.
|
|
187
|
+
with IbmRpaClient(base_url=BASE_URL, token=token, http_client=httpx.Client(base_url=BASE_URL, timeout=60)) as c:
|
|
188
|
+
print(c.workspace.get_active_workspace())
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## API coverage
|
|
192
|
+
|
|
193
|
+
| API | Methods |
|
|
194
|
+
|---|---|
|
|
195
|
+
| Authentication | `list_regions()`, `list_tenants_by_user(username)`, `get_token(...)` |
|
|
196
|
+
| Bots | `run_bot(...)`, `run_bot_by_unique_id(...)` |
|
|
197
|
+
| Process Management | `create_process_instance(...)`, `get_process_instance(...)` |
|
|
198
|
+
| Projects | `get_openapi_by_id(...)`, `get_openapi_by_unique_id(...)` |
|
|
199
|
+
| Tenant Management | `get_tenant(...)`, `suspend_tenant(...)`, `resume_tenant(...)` |
|
|
200
|
+
| Workspace | `get_active_workspace()` |
|
|
201
|
+
|
|
202
|
+
Not covered yet (use `client.request`): list processes, get a process, list projects, create a tenant,
|
|
203
|
+
`DELETE /v2.0/tenant/{tenantId}`, and SSO login with the `oidc-access-token` header.
|
|
204
|
+
|
|
205
|
+
## Known limitations
|
|
206
|
+
|
|
207
|
+
- The endpoint methods of `AsyncIbmRpaClient` do not work yet and raise
|
|
208
|
+
`AttributeError: 'coroutine' object has no attribute 'json'`. `await client.request(...)` works:
|
|
209
|
+
|
|
210
|
+
```python
|
|
211
|
+
import asyncio
|
|
212
|
+
from ibmrpa_sdk import AsyncIbmRpaClient
|
|
213
|
+
|
|
214
|
+
async def main():
|
|
215
|
+
async with AsyncIbmRpaClient(base_url=BASE_URL, token=token) as aclient:
|
|
216
|
+
print((await aclient.request("GET", "/v2.0/workspace")).json())
|
|
217
|
+
|
|
218
|
+
asyncio.run(main())
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
- Retries also apply to `POST` requests that time out, so a timed-out `run_bot` call can start the bot twice. Set
|
|
222
|
+
`IBMRPA_RETRY_MAX_ATTEMPTS=1` for critical calls.
|
|
223
|
+
- `get_openapi_*` can return a `dict`, a `str` or `{"content": text}`, depending on the response.
|
|
224
|
+
|
|
225
|
+
## License
|
|
226
|
+
|
|
227
|
+
MIT
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ibmrpa_sdk"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Unofficial Python SDK for the IBM RPA SaaS REST APIs"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "Rodrigo Vitorino" }]
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
keywords = ["ibm", "rpa", "robotic process automation", "sdk", "api", "httpx"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Intended Audience :: Developers",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
20
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
21
|
+
]
|
|
22
|
+
requires-python = ">=3.9"
|
|
23
|
+
dependencies = [
|
|
24
|
+
"httpx>=0.24.0",
|
|
25
|
+
"pydantic>=2.0.0",
|
|
26
|
+
"tenacity>=8.2.0",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.optional-dependencies]
|
|
30
|
+
dev = [
|
|
31
|
+
"pytest>=7.0.0",
|
|
32
|
+
"pytest-asyncio>=0.21.0",
|
|
33
|
+
"respx>=0.20.0",
|
|
34
|
+
"ruff>=0.1.0",
|
|
35
|
+
"mypy>=1.0.0",
|
|
36
|
+
"types-requests",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.packages.find]
|
|
40
|
+
where = ["src"]
|
|
41
|
+
|
|
42
|
+
[tool.mypy]
|
|
43
|
+
strict = true
|
|
44
|
+
ignore_missing_imports = true
|
|
45
|
+
|
|
46
|
+
[tool.ruff]
|
|
47
|
+
line-length = 120
|
|
48
|
+
target-version = "py39"
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import httpx
|
|
2
|
+
from typing import Optional, Any, Dict, Union
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from .config import Settings
|
|
6
|
+
from .auth import AuthStrategy, BearerTokenAuth, ApiKeyAuth, NoAuth
|
|
7
|
+
from .exceptions import (
|
|
8
|
+
IbmRpaHttpError,
|
|
9
|
+
IbmRpaTimeoutError,
|
|
10
|
+
IbmRpaConnectionError,
|
|
11
|
+
IbmRpaRateLimitError,
|
|
12
|
+
IbmRpaError
|
|
13
|
+
)
|
|
14
|
+
from .utils.retry import create_retry_decorator
|
|
15
|
+
from .utils.logging import get_logger
|
|
16
|
+
|
|
17
|
+
logger = get_logger("ibmrpa_sdk.async_client")
|
|
18
|
+
|
|
19
|
+
class AsyncIbmRpaClient:
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
base_url: Optional[str] = None,
|
|
23
|
+
token: Optional[str] = None,
|
|
24
|
+
settings: Optional[Settings] = None,
|
|
25
|
+
http_client: Optional[httpx.AsyncClient] = None
|
|
26
|
+
):
|
|
27
|
+
self.settings = settings or Settings()
|
|
28
|
+
|
|
29
|
+
# Override settings if explicitly provided
|
|
30
|
+
if base_url:
|
|
31
|
+
self.settings.base_url = base_url
|
|
32
|
+
if token:
|
|
33
|
+
self.settings.token = token
|
|
34
|
+
|
|
35
|
+
self.settings.validate_setup()
|
|
36
|
+
|
|
37
|
+
# Determine Auth Strategy
|
|
38
|
+
self.auth: AuthStrategy = NoAuth()
|
|
39
|
+
if self.settings.token:
|
|
40
|
+
self.auth = BearerTokenAuth(self.settings.token)
|
|
41
|
+
elif self.settings.api_key:
|
|
42
|
+
self.auth = ApiKeyAuth(self.settings.api_key)
|
|
43
|
+
|
|
44
|
+
# HTTP Client Setup
|
|
45
|
+
self._http = http_client or httpx.AsyncClient(
|
|
46
|
+
base_url=self.settings.base_url,
|
|
47
|
+
timeout=self.settings.timeout,
|
|
48
|
+
verify=self.settings.verify_ssl,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# Retry Decorator
|
|
52
|
+
self._retry = create_retry_decorator(
|
|
53
|
+
self.settings.retry_max_attempts,
|
|
54
|
+
self.settings.retry_backoff
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Initialize Endpoint Categories
|
|
58
|
+
from .endpoints.authentication import Authentication
|
|
59
|
+
from .endpoints.bots import Bots
|
|
60
|
+
from .endpoints.projects import Projects
|
|
61
|
+
from .endpoints.tenant_management import TenantManagement
|
|
62
|
+
from .endpoints.workspace import Workspace
|
|
63
|
+
from .endpoints.process_management import ProcessManagement
|
|
64
|
+
|
|
65
|
+
self.authentication = Authentication(self)
|
|
66
|
+
self.bots = Bots(self)
|
|
67
|
+
self.projects = Projects(self)
|
|
68
|
+
self.tenant = TenantManagement(self)
|
|
69
|
+
self.workspace = Workspace(self)
|
|
70
|
+
self.process = ProcessManagement(self)
|
|
71
|
+
|
|
72
|
+
async def request(
|
|
73
|
+
self,
|
|
74
|
+
method: str,
|
|
75
|
+
path: str,
|
|
76
|
+
**kwargs
|
|
77
|
+
) -> httpx.Response:
|
|
78
|
+
|
|
79
|
+
@self._retry
|
|
80
|
+
async def _do_request():
|
|
81
|
+
# Inject Auth
|
|
82
|
+
request = self._http.build_request(method, path, **kwargs)
|
|
83
|
+
request = self.auth.configure(request)
|
|
84
|
+
|
|
85
|
+
try:
|
|
86
|
+
logger.debug(f"Request: {method} {request.url}")
|
|
87
|
+
response = await self._http.send(request)
|
|
88
|
+
|
|
89
|
+
# Handling Errors
|
|
90
|
+
if response.is_error:
|
|
91
|
+
self._handle_error(response)
|
|
92
|
+
|
|
93
|
+
return response
|
|
94
|
+
|
|
95
|
+
except httpx.TimeoutException as e:
|
|
96
|
+
logger.error(f"Timeout: {e}")
|
|
97
|
+
raise IbmRpaTimeoutError(f"Request timed out: {e}") from e
|
|
98
|
+
except httpx.NetworkError as e:
|
|
99
|
+
logger.error(f"Network error: {e}")
|
|
100
|
+
raise IbmRpaConnectionError(f"Network happened: {e}") from e
|
|
101
|
+
except IbmRpaError:
|
|
102
|
+
raise
|
|
103
|
+
except Exception as e:
|
|
104
|
+
logger.error(f"Unexpected error: {e}")
|
|
105
|
+
raise IbmRpaError(f"Unexpected error: {e}") from e
|
|
106
|
+
|
|
107
|
+
return await _do_request()
|
|
108
|
+
|
|
109
|
+
def _handle_error(self, response: httpx.Response):
|
|
110
|
+
status = response.status_code
|
|
111
|
+
try:
|
|
112
|
+
body = response.json()
|
|
113
|
+
except Exception:
|
|
114
|
+
body = response.text
|
|
115
|
+
|
|
116
|
+
logger.error(f"HTTP Error {status}: {body}")
|
|
117
|
+
|
|
118
|
+
if status == 429:
|
|
119
|
+
raise IbmRpaRateLimitError(status, "Too Many Requests", body)
|
|
120
|
+
elif 400 <= status < 500:
|
|
121
|
+
raise IbmRpaHttpError(status, "Client Error", body)
|
|
122
|
+
elif 500 <= status < 600:
|
|
123
|
+
raise IbmRpaHttpError(status, "Server Error", body)
|
|
124
|
+
|
|
125
|
+
async def close(self):
|
|
126
|
+
await self._http.aclose()
|
|
127
|
+
|
|
128
|
+
async def __aenter__(self):
|
|
129
|
+
return self
|
|
130
|
+
|
|
131
|
+
async def __aexit__(self, exc_type, exc_value, traceback):
|
|
132
|
+
await self.close()
|