Skip to content

Commit 6ebdaad

Browse files
Document module usage (#3)
* Add notes about forbidden payer information when initializing a checkout * Document the module usage
1 parent ff85db3 commit 6ebdaad

2 files changed

Lines changed: 118 additions & 11 deletions

File tree

README.md

Lines changed: 114 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,145 @@
44
[![license](https://img.shields.io/github/license/aeecleclair/HelloAssoAPIWrapper)](https://github.com/aeecleclair/HelloAssoAPIWrapper/blob/main/LICENSE)
55
[![Pydantic v2](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/pydantic/pydantic/main/docs/badge/v2.json)](https://pydantic.dev)
66

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+
```
816

917
# Usage
1018

1119
```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+
)
1330
```
1431

15-
### Notification result webhooks
32+
## Usage example to init a checkout
1633

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)
1998

2099
## HelloAsso sandbox
21100

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.
23107

24108
# Development
25109

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._
27111

28112
## Add new methods
29113

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+
```
31131

32132
## Models auto-generation
33133

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/).
35137

36138
Then you can use the [datamodel-codegen](https://docs.pydantic.dev/latest/integrations/datamodel_code_generator/) tool to generate the models:
37139

38140
```bash
39141
datamodel-codegen --input HelloAssoV5OpenAPI.json --output HelloAssoAPIWrapper
40142
```
41143

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\*.\*.\*.
43147

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*.*.\*.
45148

helloasso_api_wrapper/clients/checkout_intents_management.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def init_a_checkout(
2828
) -> InitCheckoutResponse:
2929
"""
3030
https://www.helloasso.com/public-documents/documents_api/documentation_checkout.pdf
31+
32+
Note:
33+
- the first name must not be the same as the last name
34+
- some first names and names are not accepted, like "test"
3135
"""
3236
# Tested, works
3337
return self.api.callAndSerialize(

0 commit comments

Comments
 (0)