|
1 | | -# taskbadger-python |
2 | | -A client library for accessing Task Badger |
| 1 | +# Task Badger Python Client |
| 2 | +This is the official Python SDK for [Task Badger](https://taskbadger.net/) |
3 | 3 |
|
4 | | -## Usage |
5 | | -First, create a client: |
| 4 | +--- |
6 | 5 |
|
7 | | -```python |
8 | | -from taskbadger import Client |
9 | | - |
10 | | -client = Client(base_url="https://api.example.com") |
11 | | -``` |
12 | | - |
13 | | -If the endpoints you're going to hit require authentication, use `AuthenticatedClient` instead: |
14 | | - |
15 | | -```python |
16 | | -from taskbadger import AuthenticatedClient |
17 | | - |
18 | | -client = AuthenticatedClient(base_url="https://api.example.com", token="SuperSecretToken") |
19 | | -``` |
| 6 | +## Getting Started |
20 | 7 |
|
21 | | -Now call your endpoint and use your models: |
22 | | - |
23 | | -```python |
24 | | -from taskbadger.models import MyDataModel |
25 | | -from taskbadger.api.my_tag import get_my_data_model |
26 | | -from taskbadger.types import Response |
| 8 | +### Install |
27 | 9 |
|
28 | | -my_data: MyDataModel = get_my_data_model.sync(client=client) |
29 | | -# or if you need more info (e.g. status_code) |
30 | | -response: Response[MyDataModel] = get_my_data_model.sync_detailed(client=client) |
| 10 | +```bash |
| 11 | +pip install --upgrade taskbadger |
31 | 12 | ``` |
32 | 13 |
|
33 | | -Or do the same thing with an async version: |
| 14 | +### Configuration |
34 | 15 |
|
35 | 16 | ```python |
36 | | -from taskbadger.models import MyDataModel |
37 | | -from taskbadger.api.my_tag import get_my_data_model |
38 | | -from taskbadger.types import Response |
| 17 | +import taskbadger |
39 | 18 |
|
40 | | -my_data: MyDataModel = await get_my_data_model.asyncio(client=client) |
41 | | -response: Response[MyDataModel] = await get_my_data_model.asyncio_detailed(client=client) |
42 | | -``` |
43 | | - |
44 | | -By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle. |
45 | | - |
46 | | -```python |
47 | | -client = AuthenticatedClient( |
48 | | - base_url="https://internal_api.example.com", |
49 | | - token="SuperSecretToken", |
50 | | - verify_ssl="/path/to/certificate_bundle.pem", |
| 19 | +taskbadger.init( |
| 20 | + organization_slug="my-org", |
| 21 | + project_slug="my-project", |
| 22 | + token="***" |
51 | 23 | ) |
52 | 24 | ``` |
53 | 25 |
|
54 | | -You can also disable certificate validation altogether, but beware that **this is a security risk**. |
| 26 | +### Usage |
55 | 27 |
|
56 | 28 | ```python |
57 | | -client = AuthenticatedClient( |
58 | | - base_url="https://internal_api.example.com", |
59 | | - token="SuperSecretToken", |
60 | | - verify_ssl=False |
| 29 | +from taskbadger import Task, Action, EmailIntegration |
| 30 | + |
| 31 | +# create a new task with custom data and an action definition |
| 32 | +task = Task.create( |
| 33 | + "task name", |
| 34 | + data={ |
| 35 | + "custom": "data" |
| 36 | + }, |
| 37 | + actions=[ |
| 38 | + Action( |
| 39 | + "*/10%,success,error", |
| 40 | + integration=EmailIntegration(to="me@example.com") |
| 41 | + ) |
| 42 | + ] |
61 | 43 | ) |
62 | | -``` |
63 | | - |
64 | | -Things to know: |
65 | | -1. Every path/method combo becomes a Python module with four functions: |
66 | | - 1. `sync`: Blocking request that returns parsed data (if successful) or `None` |
67 | | - 1. `sync_detailed`: Blocking request that always returns a `Request`, optionally with `parsed` set if the request was successful. |
68 | | - 1. `asyncio`: Like `sync` but async instead of blocking |
69 | | - 1. `asyncio_detailed`: Like `sync_detailed` but async instead of blocking |
70 | | - |
71 | | -1. All path/query params, and bodies become method arguments. |
72 | | -1. If your endpoint had any tags on it, the first tag will be used as a module name for the function (my_tag above) |
73 | | -1. Any endpoint which did not have a tag will be in `taskbadger.api.default` |
74 | | - |
75 | | -## Building / publishing this Client |
76 | | -This project uses [Poetry](https://python-poetry.org/) to manage dependencies and packaging. Here are the basics: |
77 | | -1. Update the metadata in pyproject.toml (e.g. authors, version) |
78 | | -1. If you're using a private repository, configure it with Poetry |
79 | | - 1. `poetry config repositories.<your-repository-name> <url-to-your-repository>` |
80 | | - 1. `poetry config http-basic.<your-repository-name> <username> <password>` |
81 | | -1. Publish the client with `poetry publish --build -r <your-repository-name>` or, if for public PyPI, just `poetry publish --build` |
82 | | - |
83 | | -If you want to install this client into another project without publishing it (e.g. for development) then: |
84 | | -1. If that project **is using Poetry**, you can simply do `poetry add <path-to-this-client>` from that project |
85 | | -1. If that project is not using Poetry: |
86 | | - 1. Build a wheel with `poetry build -f wheel` |
87 | | - 1. Install that wheel from the other project `pip install <path-to-wheel>` |
88 | | - |
89 | 44 |
|
90 | | -## Updating the autogenerated code |
91 | | -```shell |
92 | | -poetry shell |
93 | | -cd ../ |
94 | | -openapi-python-client update --path taskbadger-python/taskbadger.yaml --config taskbadger-python/generator_config.yml |
| 45 | +# update the task status to 'processing' |
| 46 | +task.start() |
| 47 | +try: |
| 48 | + for i in range(100): |
| 49 | + do_something(i) |
| 50 | + if i % 10 == 0: |
| 51 | + # update the progress of the task |
| 52 | + task.update_progress(i) |
| 53 | +except Exception as e: |
| 54 | + # record task errors |
| 55 | + task.error(data={ |
| 56 | + "error": str(e) |
| 57 | + }) |
| 58 | + raise |
| 59 | + |
| 60 | +# record task success |
| 61 | +task.success() |
95 | 62 | ``` |
0 commit comments