[ADD] script restful: documentation and example
This commit is contained in:
parent
80934c8983
commit
0a800a0506
2 changed files with 69 additions and 0 deletions
25
script/restful/README.md
Normal file
25
script/restful/README.md
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
# Restful
|
||||
|
||||
REST (representational state transfer) service in ERPLibre, create a token and get data.
|
||||
|
||||
## Installation
|
||||
|
||||
In your instance, install module `restful`.
|
||||
|
||||
## Example
|
||||
|
||||
The example works with application Helpdesk, install module `helpdesk_mgmt`.
|
||||
|
||||
You need to run with specified database, and you can run the example script.
|
||||
|
||||
```bash
|
||||
./script/db_restore.py --database test
|
||||
./script/addons/install_addons.sh test restful,helpdesk_mgmt
|
||||
./run.sh -d test
|
||||
```
|
||||
|
||||
Test with the example while the server is running. You can add data manually.
|
||||
|
||||
```bash
|
||||
./script/restful/restful_example.py
|
||||
```
|
||||
44
script/restful/restful_example.py
Executable file
44
script/restful/restful_example.py
Executable file
|
|
@ -0,0 +1,44 @@
|
|||
#!./.venv/bin/python
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
headers = {
|
||||
"content-type": "application/x-www-form-urlencoded",
|
||||
"charset": "utf-8",
|
||||
}
|
||||
|
||||
data = {"login": "admin", "password": "admin", "db": "test"}
|
||||
|
||||
base_url = "http://127.0.0.1:8069"
|
||||
|
||||
req = requests.get(f"{base_url}/api/auth/token", data=data, headers=headers)
|
||||
|
||||
response = req.content.decode("utf-8")
|
||||
|
||||
print(response)
|
||||
|
||||
content = json.loads(response)
|
||||
|
||||
headers["access-token"] = content.get("access_token")
|
||||
# add the access token to the header
|
||||
|
||||
print(headers)
|
||||
|
||||
model_name = "helpdesk.ticket"
|
||||
req = requests.get(
|
||||
f"{base_url}/api/{model_name}/",
|
||||
headers=headers,
|
||||
data={"limit": 10, "domain": []},
|
||||
)
|
||||
|
||||
# ***Pass optional parameter like this, with data = ***
|
||||
# {
|
||||
# "limit": 10,
|
||||
# "domain": "[('supplier','=',True),('parent_id','=', False)]",
|
||||
# "order": "name asc",
|
||||
# "offset": 10,
|
||||
# }
|
||||
|
||||
print(req.content)
|
||||
Loading…
Reference in a new issue