openbadgefactory 0.1.0a1__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.
- openbadgefactory-0.1.0a1/LICENSE +21 -0
- openbadgefactory-0.1.0a1/PKG-INFO +229 -0
- openbadgefactory-0.1.0a1/README.md +194 -0
- openbadgefactory-0.1.0a1/examples/README.md +30 -0
- openbadgefactory-0.1.0a1/examples/async_list_badges.py +42 -0
- openbadgefactory-0.1.0a1/examples/download_badge_images.py +37 -0
- openbadgefactory-0.1.0a1/examples/issue_badge.py +50 -0
- openbadgefactory-0.1.0a1/examples/list_badges.py +37 -0
- openbadgefactory-0.1.0a1/examples/ping.py +31 -0
- openbadgefactory-0.1.0a1/pyproject.toml +191 -0
- openbadgefactory-0.1.0a1/pyproject.toml.orig +152 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/__init__.py +43 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/_auth.py +218 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/_base.py +310 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/_base_model.py +39 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/_client.py +458 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/_generated.py +956 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/_response.py +156 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/_spec.py +9 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/_version.py +6 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/exceptions.py +151 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/models.py +321 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/py.typed +0 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/resources/__init__.py +6 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/resources/_common.py +162 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/resources/badges.py +267 -0
- openbadgefactory-0.1.0a1/src/openbadgefactory/resources/events.py +668 -0
- openbadgefactory-0.1.0a1/tests/conftest.py +60 -0
- openbadgefactory-0.1.0a1/tests/test_examples.py +160 -0
- openbadgefactory-0.1.0a1/tests/unit/test_auth_flow.py +333 -0
- openbadgefactory-0.1.0a1/tests/unit/test_badges.py +334 -0
- openbadgefactory-0.1.0a1/tests/unit/test_base.py +325 -0
- openbadgefactory-0.1.0a1/tests/unit/test_client.py +459 -0
- openbadgefactory-0.1.0a1/tests/unit/test_events.py +408 -0
- openbadgefactory-0.1.0a1/tests/unit/test_exceptions.py +90 -0
- openbadgefactory-0.1.0a1/tests/unit/test_models.py +192 -0
- openbadgefactory-0.1.0a1/tests/unit/test_response.py +184 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Stefano Borsatto
|
|
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,229 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: openbadgefactory
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: Unofficial Python client for the Open Badge Factory REST API
|
|
5
|
+
Keywords: open badges,open badge factory,obf,digital credentials,micro-credentials,api client
|
|
6
|
+
Author: borsattoz
|
|
7
|
+
Author-email: borsattoz <borsattoz@scroc.co>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Education
|
|
21
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Dist: anyio>=4.16.0
|
|
25
|
+
Requires-Dist: httpx>=0.28.1
|
|
26
|
+
Requires-Dist: pydantic>=2.14.4
|
|
27
|
+
Requires-Dist: typing-extensions>=4.16.0
|
|
28
|
+
Requires-Python: >=3.10
|
|
29
|
+
Project-URL: Homepage, https://github.com/borsattoz/openbadgefactory-python
|
|
30
|
+
Project-URL: Documentation, https://github.com/borsattoz/openbadgefactory-python#readme
|
|
31
|
+
Project-URL: Source, https://github.com/borsattoz/openbadgefactory-python
|
|
32
|
+
Project-URL: Issues, https://github.com/borsattoz/openbadgefactory-python/issues
|
|
33
|
+
Project-URL: Changelog, https://github.com/borsattoz/openbadgefactory-python/blob/main/CHANGELOG.md
|
|
34
|
+
Description-Content-Type: text/markdown
|
|
35
|
+
|
|
36
|
+
# openbadgefactory
|
|
37
|
+
|
|
38
|
+
[](https://github.com/borsattoz/openbadgefactory-python/actions/workflows/ci.yml)
|
|
39
|
+
[](https://pypi.org/project/openbadgefactory/)
|
|
40
|
+
[](https://pypi.org/project/openbadgefactory/)
|
|
41
|
+
[](https://github.com/borsattoz/openbadgefactory-python/blob/main/LICENSE)
|
|
42
|
+
|
|
43
|
+
An unofficial, typed Python client for the
|
|
44
|
+
[Open Badge Factory](https://openbadgefactory.com) REST API (v2).
|
|
45
|
+
|
|
46
|
+
> [!NOTE]
|
|
47
|
+
> This project is **not affiliated with or endorsed by** Open Badge Factory Ltd.
|
|
48
|
+
> It is an independent client built on the public
|
|
49
|
+
> [OpenAPI specification](https://openbadgefactory.com/obf-api-v2/openapi-public.yaml).
|
|
50
|
+
> It is in **alpha**: the API of this library may still change between `0.x` releases.
|
|
51
|
+
|
|
52
|
+
## Features
|
|
53
|
+
|
|
54
|
+
- Sync (`Client`) and async (`AsyncClient`) clients with the same API.
|
|
55
|
+
- Fully typed: Pydantic v2 models generated from the official OpenAPI spec, checked with mypy `--strict` and pyright.
|
|
56
|
+
- OAuth2 client credentials handled for you: tokens are fetched, cached, shared between threads or tasks, and renewed before they expire.
|
|
57
|
+
- Lazy pagination: `iter()` fetches further pages only while you iterate.
|
|
58
|
+
- Safe retries: connection errors, `429` and `5xx` responses are retried with exponential backoff and `Retry-After`. Requests that would not be safe to repeat, such as issuing a badge, are never retried once they may have reached the API.
|
|
59
|
+
- Input checked before sending: ids, e-mail lists, timezone-aware dates, page sizes.
|
|
60
|
+
- A clear exception hierarchy rooted in `OBFError`.
|
|
61
|
+
|
|
62
|
+
## Installation
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install --pre openbadgefactory
|
|
66
|
+
# or
|
|
67
|
+
uv add --prerelease=allow openbadgefactory
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Python 3.10 or later is required. `--pre` is needed while only alpha releases exist.
|
|
71
|
+
|
|
72
|
+
## Credentials
|
|
73
|
+
|
|
74
|
+
You need an API client id and secret, created in Open Badge Factory under
|
|
75
|
+
**Admin tools > API** (a PRO subscription is required).
|
|
76
|
+
|
|
77
|
+
Pass them explicitly:
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from openbadgefactory import Client
|
|
81
|
+
|
|
82
|
+
obf = Client("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
or read them from the environment with `Client.from_env()`:
|
|
86
|
+
|
|
87
|
+
| Variable | Value |
|
|
88
|
+
|---|---|
|
|
89
|
+
| `OBF_CLIENT_ID` | your API client id |
|
|
90
|
+
| `OBF_CLIENT_SECRET` | your API client secret |
|
|
91
|
+
| `OBF_BASE_URL` | optional, e.g. `https://factory.cancred.ca` for CanCred Factory |
|
|
92
|
+
|
|
93
|
+
The constructor never reads the environment: only `from_env()` does.
|
|
94
|
+
Keep the secret out of version control, for example in a `.env` file listed in `.gitignore`.
|
|
95
|
+
|
|
96
|
+
## Quick start
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
from openbadgefactory import Client
|
|
100
|
+
|
|
101
|
+
with Client.from_env() as obf:
|
|
102
|
+
print(obf.ping()) # True if the API is reachable and the credentials work
|
|
103
|
+
|
|
104
|
+
for summary in obf.badges.iter():
|
|
105
|
+
print(summary.id, summary.name)
|
|
106
|
+
|
|
107
|
+
badge = obf.badges.get("BADGE_ID") # the full badge, with every language
|
|
108
|
+
print(badge.languages, badge.content_for("en"))
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Use the client as a context manager, or call `close()` when you are done, so its
|
|
112
|
+
connections are released.
|
|
113
|
+
|
|
114
|
+
### Issuing a badge
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
from openbadgefactory import Client
|
|
118
|
+
from openbadgefactory.models import IssueRecipient
|
|
119
|
+
|
|
120
|
+
with Client.from_env(api_consumer_id="my-app") as obf:
|
|
121
|
+
result = obf.events.issue(
|
|
122
|
+
"BADGE_ID",
|
|
123
|
+
[
|
|
124
|
+
"pippo@topolinia.city",
|
|
125
|
+
IssueRecipient(
|
|
126
|
+
email="paperino@paperopoli.city", name="Paolino Paperino", show_name=True
|
|
127
|
+
),
|
|
128
|
+
],
|
|
129
|
+
event_name="Python course, December",
|
|
130
|
+
log_entry={"course_id": 123},
|
|
131
|
+
send_email=False,
|
|
132
|
+
)
|
|
133
|
+
event_id = result.resource_id
|
|
134
|
+
|
|
135
|
+
for recipient in obf.events.iter_recipients(event_id=event_id):
|
|
136
|
+
print(recipient.email, recipient.assertion_id)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
`api_consumer_id` labels the events your application creates, so you can find
|
|
140
|
+
them later with `obf.events.iter(api_consumer_id="my-app")`. Set it once on the
|
|
141
|
+
client, or per call.
|
|
142
|
+
|
|
143
|
+
### Async
|
|
144
|
+
|
|
145
|
+
```python
|
|
146
|
+
import asyncio
|
|
147
|
+
|
|
148
|
+
from openbadgefactory import AsyncClient
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
async def main() -> None:
|
|
152
|
+
async with AsyncClient.from_env() as obf:
|
|
153
|
+
async for badge in obf.badges.iter():
|
|
154
|
+
print(badge.id, badge.name)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
asyncio.run(main())
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Errors
|
|
161
|
+
|
|
162
|
+
Every error raised by the library is an `OBFError`:
|
|
163
|
+
|
|
164
|
+
```text
|
|
165
|
+
OBFError
|
|
166
|
+
├── APIConnectionError the API could not be reached
|
|
167
|
+
│ └── APITimeoutError
|
|
168
|
+
├── ResponseValidationError the API answered with something unexpected
|
|
169
|
+
└── APIError the API answered with an error status
|
|
170
|
+
├── BadRequestError 400, 409, 422
|
|
171
|
+
├── AuthenticationError 401, or invalid credentials
|
|
172
|
+
├── PermissionDeniedError 403
|
|
173
|
+
├── NotFoundError 404
|
|
174
|
+
├── RateLimitError 429 (has .retry_after)
|
|
175
|
+
└── ServerError 5xx
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
from openbadgefactory import Client, NotFoundError, OBFError
|
|
180
|
+
|
|
181
|
+
with Client.from_env() as obf:
|
|
182
|
+
try:
|
|
183
|
+
badge = obf.badges.get("BADGE_ID")
|
|
184
|
+
except NotFoundError:
|
|
185
|
+
print("No such badge")
|
|
186
|
+
except OBFError as error:
|
|
187
|
+
print(f"Open Badge Factory error: {error}")
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Timeouts and retries
|
|
191
|
+
|
|
192
|
+
```python
|
|
193
|
+
from openbadgefactory import Client
|
|
194
|
+
|
|
195
|
+
obf = Client.from_env(
|
|
196
|
+
timeout=10.0, # seconds, per request (default 30)
|
|
197
|
+
max_retries=3, # default 2; 0 disables retries
|
|
198
|
+
max_retry_after=30.0, # wait at most this long for Retry-After (default 60)
|
|
199
|
+
)
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Supported API
|
|
203
|
+
|
|
204
|
+
The client targets the OBF public API **v2**, spec version `202608`
|
|
205
|
+
(available at runtime as `openbadgefactory.API_SPEC_VERSION`).
|
|
206
|
+
|
|
207
|
+
| Resource | Methods |
|
|
208
|
+
|---|---|
|
|
209
|
+
| Connection | `ping` |
|
|
210
|
+
| `badges` | `list`, `iter`, `get`, `create`, `update`, `delete`, `images`, `iter_images`, `download_image` |
|
|
211
|
+
| `events` | `issue`, `list`, `iter`, `get`, `recipients`, `iter_recipients`, `revoke`, `evidence`, `assertion_evidence`, `add_evidence`, `delete_evidence` |
|
|
212
|
+
|
|
213
|
+
Earnable badges and applications, client aliases and organisation details are
|
|
214
|
+
not wrapped yet: their models are available in `openbadgefactory.models`.
|
|
215
|
+
|
|
216
|
+
## Examples
|
|
217
|
+
|
|
218
|
+
Complete, runnable scripts are in [`examples/`](https://github.com/borsattoz/openbadgefactory-python/tree/main/examples).
|
|
219
|
+
|
|
220
|
+
## Contributing
|
|
221
|
+
|
|
222
|
+
Bug reports and pull requests are welcome: see
|
|
223
|
+
[CONTRIBUTING.md](https://github.com/borsattoz/openbadgefactory-python/blob/main/CONTRIBUTING.md).
|
|
224
|
+
To report a security issue, see
|
|
225
|
+
[SECURITY.md](https://github.com/borsattoz/openbadgefactory-python/blob/main/SECURITY.md).
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
[MIT](https://github.com/borsattoz/openbadgefactory-python/blob/main/LICENSE)
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# openbadgefactory
|
|
2
|
+
|
|
3
|
+
[](https://github.com/borsattoz/openbadgefactory-python/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/openbadgefactory/)
|
|
5
|
+
[](https://pypi.org/project/openbadgefactory/)
|
|
6
|
+
[](https://github.com/borsattoz/openbadgefactory-python/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
An unofficial, typed Python client for the
|
|
9
|
+
[Open Badge Factory](https://openbadgefactory.com) REST API (v2).
|
|
10
|
+
|
|
11
|
+
> [!NOTE]
|
|
12
|
+
> This project is **not affiliated with or endorsed by** Open Badge Factory Ltd.
|
|
13
|
+
> It is an independent client built on the public
|
|
14
|
+
> [OpenAPI specification](https://openbadgefactory.com/obf-api-v2/openapi-public.yaml).
|
|
15
|
+
> It is in **alpha**: the API of this library may still change between `0.x` releases.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- Sync (`Client`) and async (`AsyncClient`) clients with the same API.
|
|
20
|
+
- Fully typed: Pydantic v2 models generated from the official OpenAPI spec, checked with mypy `--strict` and pyright.
|
|
21
|
+
- OAuth2 client credentials handled for you: tokens are fetched, cached, shared between threads or tasks, and renewed before they expire.
|
|
22
|
+
- Lazy pagination: `iter()` fetches further pages only while you iterate.
|
|
23
|
+
- Safe retries: connection errors, `429` and `5xx` responses are retried with exponential backoff and `Retry-After`. Requests that would not be safe to repeat, such as issuing a badge, are never retried once they may have reached the API.
|
|
24
|
+
- Input checked before sending: ids, e-mail lists, timezone-aware dates, page sizes.
|
|
25
|
+
- A clear exception hierarchy rooted in `OBFError`.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install --pre openbadgefactory
|
|
31
|
+
# or
|
|
32
|
+
uv add --prerelease=allow openbadgefactory
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Python 3.10 or later is required. `--pre` is needed while only alpha releases exist.
|
|
36
|
+
|
|
37
|
+
## Credentials
|
|
38
|
+
|
|
39
|
+
You need an API client id and secret, created in Open Badge Factory under
|
|
40
|
+
**Admin tools > API** (a PRO subscription is required).
|
|
41
|
+
|
|
42
|
+
Pass them explicitly:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from openbadgefactory import Client
|
|
46
|
+
|
|
47
|
+
obf = Client("YOUR_CLIENT_ID", "YOUR_CLIENT_SECRET")
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
or read them from the environment with `Client.from_env()`:
|
|
51
|
+
|
|
52
|
+
| Variable | Value |
|
|
53
|
+
|---|---|
|
|
54
|
+
| `OBF_CLIENT_ID` | your API client id |
|
|
55
|
+
| `OBF_CLIENT_SECRET` | your API client secret |
|
|
56
|
+
| `OBF_BASE_URL` | optional, e.g. `https://factory.cancred.ca` for CanCred Factory |
|
|
57
|
+
|
|
58
|
+
The constructor never reads the environment: only `from_env()` does.
|
|
59
|
+
Keep the secret out of version control, for example in a `.env` file listed in `.gitignore`.
|
|
60
|
+
|
|
61
|
+
## Quick start
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
from openbadgefactory import Client
|
|
65
|
+
|
|
66
|
+
with Client.from_env() as obf:
|
|
67
|
+
print(obf.ping()) # True if the API is reachable and the credentials work
|
|
68
|
+
|
|
69
|
+
for summary in obf.badges.iter():
|
|
70
|
+
print(summary.id, summary.name)
|
|
71
|
+
|
|
72
|
+
badge = obf.badges.get("BADGE_ID") # the full badge, with every language
|
|
73
|
+
print(badge.languages, badge.content_for("en"))
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Use the client as a context manager, or call `close()` when you are done, so its
|
|
77
|
+
connections are released.
|
|
78
|
+
|
|
79
|
+
### Issuing a badge
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from openbadgefactory import Client
|
|
83
|
+
from openbadgefactory.models import IssueRecipient
|
|
84
|
+
|
|
85
|
+
with Client.from_env(api_consumer_id="my-app") as obf:
|
|
86
|
+
result = obf.events.issue(
|
|
87
|
+
"BADGE_ID",
|
|
88
|
+
[
|
|
89
|
+
"pippo@topolinia.city",
|
|
90
|
+
IssueRecipient(
|
|
91
|
+
email="paperino@paperopoli.city", name="Paolino Paperino", show_name=True
|
|
92
|
+
),
|
|
93
|
+
],
|
|
94
|
+
event_name="Python course, December",
|
|
95
|
+
log_entry={"course_id": 123},
|
|
96
|
+
send_email=False,
|
|
97
|
+
)
|
|
98
|
+
event_id = result.resource_id
|
|
99
|
+
|
|
100
|
+
for recipient in obf.events.iter_recipients(event_id=event_id):
|
|
101
|
+
print(recipient.email, recipient.assertion_id)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`api_consumer_id` labels the events your application creates, so you can find
|
|
105
|
+
them later with `obf.events.iter(api_consumer_id="my-app")`. Set it once on the
|
|
106
|
+
client, or per call.
|
|
107
|
+
|
|
108
|
+
### Async
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
import asyncio
|
|
112
|
+
|
|
113
|
+
from openbadgefactory import AsyncClient
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
async def main() -> None:
|
|
117
|
+
async with AsyncClient.from_env() as obf:
|
|
118
|
+
async for badge in obf.badges.iter():
|
|
119
|
+
print(badge.id, badge.name)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
asyncio.run(main())
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Errors
|
|
126
|
+
|
|
127
|
+
Every error raised by the library is an `OBFError`:
|
|
128
|
+
|
|
129
|
+
```text
|
|
130
|
+
OBFError
|
|
131
|
+
├── APIConnectionError the API could not be reached
|
|
132
|
+
│ └── APITimeoutError
|
|
133
|
+
├── ResponseValidationError the API answered with something unexpected
|
|
134
|
+
└── APIError the API answered with an error status
|
|
135
|
+
├── BadRequestError 400, 409, 422
|
|
136
|
+
├── AuthenticationError 401, or invalid credentials
|
|
137
|
+
├── PermissionDeniedError 403
|
|
138
|
+
├── NotFoundError 404
|
|
139
|
+
├── RateLimitError 429 (has .retry_after)
|
|
140
|
+
└── ServerError 5xx
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
from openbadgefactory import Client, NotFoundError, OBFError
|
|
145
|
+
|
|
146
|
+
with Client.from_env() as obf:
|
|
147
|
+
try:
|
|
148
|
+
badge = obf.badges.get("BADGE_ID")
|
|
149
|
+
except NotFoundError:
|
|
150
|
+
print("No such badge")
|
|
151
|
+
except OBFError as error:
|
|
152
|
+
print(f"Open Badge Factory error: {error}")
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Timeouts and retries
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
from openbadgefactory import Client
|
|
159
|
+
|
|
160
|
+
obf = Client.from_env(
|
|
161
|
+
timeout=10.0, # seconds, per request (default 30)
|
|
162
|
+
max_retries=3, # default 2; 0 disables retries
|
|
163
|
+
max_retry_after=30.0, # wait at most this long for Retry-After (default 60)
|
|
164
|
+
)
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Supported API
|
|
168
|
+
|
|
169
|
+
The client targets the OBF public API **v2**, spec version `202608`
|
|
170
|
+
(available at runtime as `openbadgefactory.API_SPEC_VERSION`).
|
|
171
|
+
|
|
172
|
+
| Resource | Methods |
|
|
173
|
+
|---|---|
|
|
174
|
+
| Connection | `ping` |
|
|
175
|
+
| `badges` | `list`, `iter`, `get`, `create`, `update`, `delete`, `images`, `iter_images`, `download_image` |
|
|
176
|
+
| `events` | `issue`, `list`, `iter`, `get`, `recipients`, `iter_recipients`, `revoke`, `evidence`, `assertion_evidence`, `add_evidence`, `delete_evidence` |
|
|
177
|
+
|
|
178
|
+
Earnable badges and applications, client aliases and organisation details are
|
|
179
|
+
not wrapped yet: their models are available in `openbadgefactory.models`.
|
|
180
|
+
|
|
181
|
+
## Examples
|
|
182
|
+
|
|
183
|
+
Complete, runnable scripts are in [`examples/`](https://github.com/borsattoz/openbadgefactory-python/tree/main/examples).
|
|
184
|
+
|
|
185
|
+
## Contributing
|
|
186
|
+
|
|
187
|
+
Bug reports and pull requests are welcome: see
|
|
188
|
+
[CONTRIBUTING.md](https://github.com/borsattoz/openbadgefactory-python/blob/main/CONTRIBUTING.md).
|
|
189
|
+
To report a security issue, see
|
|
190
|
+
[SECURITY.md](https://github.com/borsattoz/openbadgefactory-python/blob/main/SECURITY.md).
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
[MIT](https://github.com/borsattoz/openbadgefactory-python/blob/main/LICENSE)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Examples
|
|
2
|
+
|
|
3
|
+
Small, complete scripts showing how to use `openbadgefactory`. They read your
|
|
4
|
+
credentials from the environment with `Client.from_env()`:
|
|
5
|
+
|
|
6
|
+
| Variable | Value |
|
|
7
|
+
|---|---|
|
|
8
|
+
| `OBF_CLIENT_ID` | your organisation id |
|
|
9
|
+
| `OBF_CLIENT_SECRET` | an API secret from *Admin tools > API* (PRO subscription) |
|
|
10
|
+
| `OBF_BASE_URL` | optional, e.g. `https://factory.cancred.ca` for CanCred Factory |
|
|
11
|
+
|
|
12
|
+
Run them from the repository root:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
export OBF_CLIENT_ID=... OBF_CLIENT_SECRET=...
|
|
16
|
+
uv run examples/ping.py
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
| Script | What it shows |
|
|
20
|
+
|---|---|
|
|
21
|
+
| [`ping.py`](ping.py) | Check connection and credentials; handle errors with `OBFError` |
|
|
22
|
+
| [`list_badges.py`](list_badges.py) | Iterate over every badge with `badges.iter()` |
|
|
23
|
+
| [`download_badge_images.py`](download_badge_images.py) | Download badge images with `badges.download_image()` |
|
|
24
|
+
| [`async_list_badges.py`](async_list_badges.py) | `AsyncClient`: fetch many badges concurrently |
|
|
25
|
+
| [`issue_badge.py`](issue_badge.py) | Issue a badge with `events.issue()` and an `api_consumer_id` (dry run by default) |
|
|
26
|
+
|
|
27
|
+
The examples only read data, except `issue_badge.py`, which issues a real badge
|
|
28
|
+
(and e-mails the recipients) only when run with `--yes`.
|
|
29
|
+
They are run by the test suite (`tests/test_examples.py`) against a simulated
|
|
30
|
+
API, so they stay in sync with the library.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""List badges with the async client, fetching their details concurrently.
|
|
2
|
+
|
|
3
|
+
::
|
|
4
|
+
|
|
5
|
+
OBF_CLIENT_ID=... OBF_CLIENT_SECRET=... uv run examples/async_list_badges.py
|
|
6
|
+
|
|
7
|
+
The list only has each badge's name; the full badge (every language) needs one
|
|
8
|
+
request per badge. With :class:`~openbadgefactory.AsyncClient` those requests run
|
|
9
|
+
concurrently, a few at a time.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import asyncio
|
|
15
|
+
|
|
16
|
+
from openbadgefactory import AsyncClient, Badge
|
|
17
|
+
|
|
18
|
+
#: How many badge requests may run at the same time.
|
|
19
|
+
CONCURRENCY = 5
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
async def fetch_all() -> list[Badge]:
|
|
23
|
+
async with AsyncClient.from_env() as obf:
|
|
24
|
+
ids = [badge.id async for badge in obf.badges.iter()]
|
|
25
|
+
limit = asyncio.Semaphore(CONCURRENCY)
|
|
26
|
+
|
|
27
|
+
async def fetch(badge_id: str) -> Badge:
|
|
28
|
+
async with limit:
|
|
29
|
+
return await obf.badges.get(badge_id)
|
|
30
|
+
|
|
31
|
+
return await asyncio.gather(*(fetch(badge_id) for badge_id in ids))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def main() -> int:
|
|
35
|
+
for badge in asyncio.run(fetch_all()):
|
|
36
|
+
languages = ", ".join(badge.languages) or "-"
|
|
37
|
+
print(f"{badge.id} [{languages}] {badge.name}")
|
|
38
|
+
return 0
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""Save the image of every published badge into a folder.
|
|
2
|
+
|
|
3
|
+
::
|
|
4
|
+
|
|
5
|
+
OBF_CLIENT_ID=... OBF_CLIENT_SECRET=... uv run examples/download_badge_images.py images/
|
|
6
|
+
|
|
7
|
+
Each file is named after the badge id, with the extension of the image type
|
|
8
|
+
(``.png`` or ``.svg``).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import argparse
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
from openbadgefactory import Client
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def main(argv: list[str] | None = None) -> int:
|
|
20
|
+
parser = argparse.ArgumentParser(description="Download your badge images.")
|
|
21
|
+
parser.add_argument("folder", type=Path, help="where to save the images")
|
|
22
|
+
args = parser.parse_args(argv)
|
|
23
|
+
|
|
24
|
+
folder: Path = args.folder
|
|
25
|
+
folder.mkdir(parents=True, exist_ok=True)
|
|
26
|
+
|
|
27
|
+
with Client.from_env() as obf:
|
|
28
|
+
for badge in obf.badges.iter():
|
|
29
|
+
image = obf.badges.download_image(badge.id)
|
|
30
|
+
path = folder / f"{badge.id}{image.extension}"
|
|
31
|
+
path.write_bytes(image.content)
|
|
32
|
+
print(f"{path} ({len(image.content)} bytes, {image.media_type})")
|
|
33
|
+
return 0
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
if __name__ == "__main__":
|
|
37
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""Issue a badge to one or more people: a dry run unless you pass ``--yes``.
|
|
2
|
+
|
|
3
|
+
::
|
|
4
|
+
|
|
5
|
+
uv run examples/issue_badge.py BADGE_ID ada@example.com alan@example.com
|
|
6
|
+
uv run examples/issue_badge.py BADGE_ID ada@example.com --yes --no-email
|
|
7
|
+
|
|
8
|
+
Without ``--yes`` it only shows what would be issued: issuing is real and sends
|
|
9
|
+
e-mails to the recipients. Every badge issued here is labelled with the
|
|
10
|
+
``api_consumer_id`` "openbadgefactory-examples", so you can find them later with
|
|
11
|
+
``client.events.list(api_consumer_id="openbadgefactory-examples")``.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import argparse
|
|
17
|
+
|
|
18
|
+
from openbadgefactory import Client, OBFError
|
|
19
|
+
|
|
20
|
+
API_CONSUMER_ID = "openbadgefactory-examples"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def main(argv: list[str] | None = None) -> int:
|
|
24
|
+
parser = argparse.ArgumentParser(description="Issue an Open Badge Factory badge.")
|
|
25
|
+
parser.add_argument("badge_id", help="the badge to issue")
|
|
26
|
+
parser.add_argument("emails", nargs="+", help="recipients' e-mail addresses")
|
|
27
|
+
parser.add_argument("--yes", action="store_true", help="really issue the badge")
|
|
28
|
+
parser.add_argument("--no-email", action="store_true", help="do not notify recipients")
|
|
29
|
+
args = parser.parse_args(argv)
|
|
30
|
+
|
|
31
|
+
with Client.from_env(api_consumer_id=API_CONSUMER_ID) as obf:
|
|
32
|
+
try:
|
|
33
|
+
badge = obf.badges.get(args.badge_id)
|
|
34
|
+
print(f"Badge: {badge.name} ({badge.id})")
|
|
35
|
+
print(f"Recipients: {', '.join(args.emails)}")
|
|
36
|
+
if not args.yes:
|
|
37
|
+
print("Dry run: nothing issued. Add --yes to issue the badge.")
|
|
38
|
+
return 0
|
|
39
|
+
|
|
40
|
+
result = obf.events.issue(badge.id, args.emails, send_email=not args.no_email)
|
|
41
|
+
except OBFError as error:
|
|
42
|
+
print(f"Failed: {error}")
|
|
43
|
+
return 1
|
|
44
|
+
|
|
45
|
+
print(f"Issued: event {result.resource_id}")
|
|
46
|
+
return 0
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
if __name__ == "__main__":
|
|
50
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""List the badges of your organisation, page after page.
|
|
2
|
+
|
|
3
|
+
::
|
|
4
|
+
|
|
5
|
+
OBF_CLIENT_ID=... OBF_CLIENT_SECRET=... uv run examples/list_badges.py
|
|
6
|
+
OBF_CLIENT_ID=... OBF_CLIENT_SECRET=... uv run examples/list_badges.py --drafts
|
|
7
|
+
|
|
8
|
+
``badges.iter()`` fetches further pages only while you iterate, so it works the
|
|
9
|
+
same with ten badges or ten thousand.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import argparse
|
|
15
|
+
|
|
16
|
+
from openbadgefactory import Client
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def main(argv: list[str] | None = None) -> int:
|
|
20
|
+
parser = argparse.ArgumentParser(description="List your Open Badge Factory badges.")
|
|
21
|
+
parser.add_argument("--drafts", action="store_true", help="list draft badges instead")
|
|
22
|
+
args = parser.parse_args(argv)
|
|
23
|
+
|
|
24
|
+
count = 0
|
|
25
|
+
with Client.from_env() as obf:
|
|
26
|
+
for badge in obf.badges.iter(draft=args.drafts):
|
|
27
|
+
created = badge.created_at.date().isoformat() if badge.created_at else "-"
|
|
28
|
+
print(f"{badge.id} {created} {badge.name}")
|
|
29
|
+
count += 1
|
|
30
|
+
|
|
31
|
+
kind = "draft" if args.drafts else "published"
|
|
32
|
+
print(f"{count} {kind} badge(s)")
|
|
33
|
+
return 0
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
if __name__ == "__main__":
|
|
37
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""Check that the API is reachable and your credentials work.
|
|
2
|
+
|
|
3
|
+
Run it with your organisation id and API secret (Admin tools > API)::
|
|
4
|
+
|
|
5
|
+
OBF_CLIENT_ID=... OBF_CLIENT_SECRET=... uv run examples/ping.py
|
|
6
|
+
|
|
7
|
+
Set ``OBF_BASE_URL=https://factory.cancred.ca`` to use CanCred Factory.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from openbadgefactory import Client, OBFError
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def main() -> int:
|
|
16
|
+
try:
|
|
17
|
+
with Client.from_env() as obf:
|
|
18
|
+
ok = obf.ping()
|
|
19
|
+
except OBFError as error:
|
|
20
|
+
print(f"Connection failed: {error}")
|
|
21
|
+
return 1
|
|
22
|
+
|
|
23
|
+
if not ok:
|
|
24
|
+
print("The API answered, but reported a failure")
|
|
25
|
+
return 1
|
|
26
|
+
print(f"Connected to {obf.base_url} as {obf.client_id}")
|
|
27
|
+
return 0
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
if __name__ == "__main__":
|
|
31
|
+
raise SystemExit(main())
|