|
4 | 4 | [](https://github.com/aeecleclair/HelloAssoAPIWrapper/blob/main/LICENSE) |
5 | 5 | [](https://pydantic.dev) |
6 | 6 |
|
7 | | -A Python wrapper for the HelloAsso API, using [Pydantic](https://docs.pydantic.dev/latest/) for models validation. |
| 7 | +A fully typed Python wrapper for _HelloAsso_ API. You will find information about _HelloAsso_ on their [website](https://www.helloasso.com/) and their [API swagger documentation](https://api.helloasso.com/v5/swagger/ui/index). |
| 8 | + |
| 9 | +The module is based on [Pydantic](https://docs.pydantic.dev/latest/) for models validation and on HelloAsso's [HaApiV5](https://github.com/HelloAsso/HaApiV5) for API calls. |
| 10 | + |
| 11 | +# Installation |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install HelloAssoAPIWrapper |
| 15 | +``` |
8 | 16 |
|
9 | 17 | # Usage |
10 | 18 |
|
11 | 19 | ```python |
12 | | -from HelloAssoAPIWrapper import HelloAssoAPIWrapper |
| 20 | +# Import the wrapper |
| 21 | +from helloasso_api_wrapper import HelloAssoAPIWrapper |
| 22 | + |
| 23 | +# Create an instance of `HelloAssoAPIWrapper` |
| 24 | +hello_asso = HelloAssoAPIWrapper( |
| 25 | + api_base=settings.HELLOASSO_API_BASE, |
| 26 | + client_id=settings.HELLOASSO_CLIENT_ID, |
| 27 | + client_secret=settings.HELLOASSO_CLIENT_SECRET, |
| 28 | + timeout=60, |
| 29 | +) |
13 | 30 | ``` |
14 | 31 |
|
15 | | -### Notification result webhooks |
| 32 | +## Usage example to init a checkout |
16 | 33 |
|
17 | | -You should configure a webhook to receive the notification results. |
18 | | -HelloAsso will make a POST request to the URL you provided with a JSON payload corresponding to a `NotificationResultContent` object. |
| 34 | +```python |
| 35 | +from helloasso_api_wrapper import HelloAssoAPIWrapper |
| 36 | +from helloasso_api_wrapper.exceptions import ApiV5BadRequest |
| 37 | + |
| 38 | +from helloasso_api_wrapper.models.carts import ( |
| 39 | + CheckoutPayer, |
| 40 | + InitCheckoutBody, |
| 41 | + InitCheckoutResponse, |
| 42 | +) |
| 43 | + |
| 44 | +# First create an instance of `HelloAssoAPIWrapper` with the previous snippet |
| 45 | +hello_asso = HelloAssoAPIWrapper( |
| 46 | + api_base=settings.HELLOASSO_API_BASE, |
| 47 | + client_id=settings.HELLOASSO_CLIENT_ID, |
| 48 | + client_secret=settings.HELLOASSO_CLIENT_SECRET, |
| 49 | + timeout=60, |
| 50 | +) |
| 51 | + |
| 52 | +# Then use the wrapper to init a checkout |
| 53 | +init_checkout_body = InitCheckoutBody( |
| 54 | + totalAmount=100, # The total amount of the checkout in cents |
| 55 | + initialAmount=100, |
| 56 | + itemName="Our first checkout", |
| 57 | + backUrl="https://yourwebsite.com/callback", # The url must use https |
| 58 | + errorUrl="https://yourwebsite.com/callback",, |
| 59 | + returnUrl="https://yourwebsite.com/callback",, |
| 60 | + containsDonation=False, |
| 61 | + payer=CheckoutPayer( |
| 62 | + firstName="Fabristpp", |
| 63 | + lastName="John", |
| 64 | + email="fabristpp@email.fr", |
| 65 | + ), |
| 66 | +) |
| 67 | + |
| 68 | +response: InitCheckoutResponse |
| 69 | +try: |
| 70 | + response = self.hello_asso.checkout_intents_management.init_a_checkout( |
| 71 | + helloasso_slug, |
| 72 | + init_checkout_body, |
| 73 | + ) |
| 74 | +except ApiV5BadRequest as error: |
| 75 | + print("Failed to init a checkout") |
| 76 | + raise error |
| 77 | +``` |
| 78 | + |
| 79 | +# Structure of the module |
| 80 | + |
| 81 | +Authentication is done on the background using HelloAsso's `HaApiV5` module. |
| 82 | + |
| 83 | +The module is organized around a class [`HelloAssoAPIWrapper`](./helloasso_api_wrapper/__init__.py) containing all the methods to interact with the HelloAsso API. |
| 84 | + |
| 85 | +This class expose multiple clients, allowing to interact with different parts of the API. All clients are located in the [`clients`](./helloasso_api_wrapper/clients) folder. |
| 86 | + |
| 87 | +Pydantic models are located in the [`models`](./helloasso_api_wrapper/models) folder. Models are usually really permissive, typing all fields as Nullable. This is because the API is not always clear about the fields that are required or not. _Some assumptions are made to make the models more strict._ |
| 88 | + |
| 89 | +We expose exceptions from `HaApiV5`. You can import them directly: `from helloasso_api_wrapper.exceptions import *` |
| 90 | + |
| 91 | +# Documentation about HelloAsso API |
| 92 | + |
| 93 | +You will find documentation about HelloAsso API endpoints on: |
| 94 | + |
| 95 | +- [HelloAsso API swagger documentation](https://api.helloasso.com/v5/swagger/ui/index) |
| 96 | +- [HelloAsso API documentation](https://www.helloasso.com/public-documents/documents_api/guide_utilisation_api.pdf) |
| 97 | +- [HelloAsso Checkout documentation](https://www.helloasso.com/public-documents/documents_api/documentation_checkout.pdf) |
19 | 98 |
|
20 | 99 | ## HelloAsso sandbox |
21 | 100 |
|
22 | | -HelloAsso provide a sandbox: api.helloasso-sandbox.com |
| 101 | +HelloAsso provide a sandbox: api.helloasso-sandbox.com to test your integration. |
| 102 | + |
| 103 | +# Notification result webhooks |
| 104 | + |
| 105 | +You should configure a webhook to receive the notification results. |
| 106 | +HelloAsso will make a POST request to the URL you provided with a JSON payload corresponding to a `NotificationResultContent` object. |
23 | 107 |
|
24 | 108 | # Development |
25 | 109 |
|
26 | | -Models when first generated using HelloAsso swagger documentation, and then adapted to include additional models, use stricter types and add documentation |
| 110 | +Models where first generated using HelloAsso swagger documentation, and then adapted to include additional models, use stricter types and add documentation. _Some assumptions are made to make the models more strict._ |
27 | 111 |
|
28 | 112 | ## Add new methods |
29 | 113 |
|
30 | | -todo |
| 114 | +Currently, most endpoints are not implemented. To add a new method, you simply need to create a new method in the corresponding client. The method should use the `self.api.callAndSerialize` method to make the API call. |
| 115 | + |
| 116 | +Example for [checkout_intents_management `init_a_checkout` method](./helloasso_api_wrapper/clients/checkout_intents_management.py): |
| 117 | + |
| 118 | +```python |
| 119 | +def init_a_checkout( |
| 120 | + self, |
| 121 | + organization_slug: str, |
| 122 | + init_checkout_body: InitCheckoutBody, |
| 123 | +) -> InitCheckoutResponse: |
| 124 | + return self.api.callAndSerialize( |
| 125 | + f"/organizations/{organization_slug}/checkout-intents", # API endpoint |
| 126 | + InitCheckoutResponse, # Model to serialize the response |
| 127 | + body=init_checkout_body, # Body of the request |
| 128 | + method="POST", # Request method |
| 129 | + ) |
| 130 | +``` |
31 | 131 |
|
32 | 132 | ## Models auto-generation |
33 | 133 |
|
34 | | -Download the swagger file from the HelloAsso API documentation. It use an old swagger 2 version. You need to convert it to OpenAPI 3.0.0 version. You can use the online tool [Swagger Editor](https://editor.swagger.io/). |
| 134 | +> This was used to generate the first version of the models. It should not be necessary to do it again. |
| 135 | +
|
| 136 | +Download the swagger file from the HelloAsso API documentation. It uses an old swagger 2 version. You need to convert it to OpenAPI 3.0.0 version. You can use the online tool [Swagger Editor](https://editor.swagger.io/). |
35 | 137 |
|
36 | 138 | Then you can use the [datamodel-codegen](https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/) tool to generate the models: |
37 | 139 |
|
38 | 140 | ```bash |
39 | 141 | datamodel-codegen --input HelloAssoV5OpenAPI.json --output HelloAssoAPIWrapper |
40 | 142 | ``` |
41 | 143 |
|
42 | | -Make a release on Pypi |
| 144 | +# Make a release on Pypi |
| 145 | + |
| 146 | +You need to edit HelloAssoAPIWrapper version in [helloasso_api_wrapper/\_\_about\_\_.py](./helloasso_api_wrapper/__about__.py). Then make a release on GitHub and add a tag. The tag should match v\*.\*.\*. |
43 | 147 |
|
44 | | -You need to edit HelloAssoAPIWrapper version in [helloasso_api_wrapper/\_\_about\_\_.py](./helloasso_api_wrapper/__about__.py). Then make a release on GitHub and add a tag. The tag should match v*.*.\*. |
45 | 148 |
|
0 commit comments