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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
# Odoo REST API
This is a module which expose Odoo as a REST API 


## Installing
* Download this module and put it to your Odoo addons directory
* Install requirements with `pip install -r requirements.txt`

## Getting Started

### Authenticating users
Before making any request make sure you are authenticated. The route which is used to authenticate users is `/auth/`. Below is an example showing how to authenticate users.
```py
import json
import requests
import sys

AUTH_URL = 'http://localhost:8069/auth/'

headers = {'Content-type': 'application/json'}


# Remember to configure default db on odoo configuration file(dbfilter = ^db_name$)
# Authentication credentials
data = {
    'params': {
         'login': 'your@email.com',
         'password': 'yor_password',
         'db': 'your_db_name'
    }
}

# Authenticate user
res = requests.post(
    AUTH_URL, 
    data=json.dumps(data), 
    headers=headers
)

# Get response cookies
# This hold information for authenticated user
cookies = res.cookies


# Example 1
# Get users
USERS_URL = 'http://localhost:8069/api/res.users/'

# This will take time since it retrives all res.users fields
# You can use query param to fetch specific fields

res = requests.get(
    USERS_URL, 
    cookies=cookies  # Here we are sending cookies which holds info for authenticated user
)

# This will be a very long response since it has many data
print(res.text)


# Example 2
# Get products(assuming you have products in you db)
# Here am using query param to fetch only product id and name(This will be faster)
USERS_URL = 'http://localhost:8069/api/product.product/'

# Use query param to fetch only id and name
params = {'query': '{id, name}'}

res = requests.get(
    USERS_URL, 
    params=params,
    cookies=cookies  # Here we are sending cookies which holds info for authenticated user
)

# This will be small since we've retrieved only id and name
print(res.text)
```


## Allowed HTTP methods 

## 1. GET

### Model records: 

`GET /api/{model}/`
#### Parameters 
* **query (optional):**

   This parameter is used to dynamically select fields to include on a response. For example if we want to select `id` and `name` fields from `res.users` model here is how we would do it.

   `GET /api/res.users/?query={id, name}`

   ```js
   {
       "count": 2, 
       "prev": null, 
       "current": 1, 
       "next": null, 
       "total_pages": 1, 
       "result": [
           {
               "id": 2, 
               "name": "Administrator"
            }, 
           {
               "id": 6, 
               "name": "Sailors Co Ltd"
            }
        ]
    }
   ```
   
   For nested records, for example if we want to select `id`, `name` and `company_id` fields from `res.users` model, but under `company_id` we want to select `name` field only. here is how we would do it.

   `GET /api/res.users/?query={id, name, company_id{name}}`

   ```js
   {
       "count": 2, 
       "prev": null, 
       "current": 1, 
       "next": null, 
       "total_pages": 1, 
       "result": [
           {
               "id": 2, 
               "name": "Administrator",
               "company_id": {
                   "name": "Singo Africa"
               }
            }, 
           {
               "id": 6, 
               "name": "Sailors Co Ltd",
               "company_id": {
                   "name": "Singo Africa"
               }
            }
        ]
    }
   ```

   For nested iterable records, for example if we want to select `id`, `name` and `related_products` fields from `product.template` model, but under `related_products` we want to select `name` field only. here is how we would do it.

   `GET /api/product.template/?query={id, name, related_products{name}}`

   ```js
   {
       "count": 2, 
       "prev": null, 
       "current": 1, 
       "next": null, 
       "total_pages": 1, 
       "result": [
           {
               "id": 16, 
               "name": "Alaf Resincot Steel Roof-16", 
               "related_products": [
                   {"name": "Alloy Steel AISI 4140 Bright Bars - All 5.8 meter longs"}, 
                   {"name": "Test product"}
                ]
            }, 
            {
                "id": 18,
                 "name": "Alaf Resincot Steel Roof-43", 
                 "related_products": [
                     {"name": "Alloy Steel AISI 4140 Bright Bars - All 5.8 meter longs"}, 
                     {"name": "Aluminium Sheets & Plates"}, 
                     {"name": "Test product"}
                 ]
            }
        ]
   }
   ```

   If you want to fetch all fields except few you can use exclude(-) operator. For example in the case above if we want to fetch all fields except `name` field, here is how we could do it   
   `GET /api/product.template/?query={-name}`
   
   ```js
   {
        "count": 3, 
        "prev": null, 
        "current": 1, 
        "next": null, 
        "total_pages": 1, 
        "result": [
            {   
                "id": 1,
                ... // All fields except name
            }, 
            {
                "id": 2
                ... // All fields except name
            }
            ...
        ]
   }
   ```

   There is also a wildcard(\*) operator which can be used to fetch all fields, Below is an example which shows how you can fetch all product's fields but under `related_products` field get all fields except `id`.

   `GET /api/product.template/?query={*, related_products{-id}}`

   ```js
   {
        "count": 3, 
        "prev": null, 
        "current": 1, 
        "next": null, 
        "total_pages": 1, 
        "result": [
            {   
                "id": 1,
                "name": "Pen",
                "related_products"{
                    "name": "Pencil",
                    ... // All fields except id
                }
                ... // All fields
            }, 
            ...
        ]
   }
   ```

   **If you don't specify query parameter all fields will be returned.**


* **filter (optional):**

    This is used to filter out data returned. For example if we want to get all products with id ranging from 60 to 70, here's how we would do it.

    `GET /api/product.template/?query={id, name}&filter=[["id", ">", 60], ["id", "<", 70]]`

    ```js
    {
        "count": 3, 
        "prev": null, 
        "current": 1, 
        "next": null, 
        "total_pages": 1, 
        "result": [
            {
                "id": 67, 
                "name": "Crown Paints Economy Superplus Emulsion"
            }, 
            {
                "id": 69,
                "name": "Crown Paints Permacote"
            }
        ]
    }
    ```

* **page_size (optional) & page (optional):**

    These two allows us to do pagination. Hre page_size is used to specify number of records on a single page and page is used to specify the current page. For example if we want our page_size to be 5 records and we want to fetch data on page 3 here is how we would do it.

    `GET /api/product.template/?query={id, name}&page_size=5&page=3`

    ```js
    {
        "count": 5, 
        "prev": 2, 
        "current": 3, 
        "next": 4, 
        "total_pages": 15, 
        "result": [
            {"id": 141, "name": "Borewell Slotting Pipes"}, 
            {"id": 114, "name": "Bright Bars"}, 
            {"id": 128, "name": "Chain Link Fence"}, 
            {"id": 111, "name": "Cold Rolled Sheets - CRCA & GI Sheets"}, 
            {"id": 62, "name": "Crown Paints Acrylic Primer/Sealer Undercoat"}
        ]
    }
    ```

    Note: `prev`, `current`, `next` and `total_pages` shows the previous page, current page, next page and the total number of pages respectively.

* **limit (optional):**

    This is used to limit the number of results returned on a request regardless of pagination. For example
    
    `GET /api/product.template/?query={id, name}&limit=3`

    ```js
    {
        "count": 3, 
        "prev": null, 
        "current": 1, 
        "next": null, 
        "total_pages": 1, 
        "result": [
            {"id": 16, "name": "Alaf Resincot Steel Roof-16"}, 
            {"id": 18, "name": "Alaf Resincot Steel Roof-43"}, 
            {"id": 95, "name": "Alaf versatile steel roof"}
        ]
    }
    ```

### Model record:  

`GET /api/{model}/{id}`
#### Parameters
* **query (optional):**

    Here query parameter works exactly the same as explained before except it selects fields on a single record. For example

    `GET /api/product.template/95/?query={id, name}`

    ```js
    {
        "id": 95, 
        "name": "Alaf versatile steel roof"
    }
    ```


## 2. POST

`POST /api/{model}/`
#### Headers
* Content-Type: application/json
#### Parameters 
* **data (mandatory):**

    This is used to pass data to be posted. For example
    
    `POST /api/product.public.category/`

    Request Body

    ```js
    {
        "params": {
            "data": {
                "name": "Test category_2"
            }
        }
    }
    ```

    Response

    ```js
    {
        "jsonrpc": "2.0",
        "id": null,
        "result": 398
    }
    ```

    The number on `result` is the `id` of the newly created record.

* **context (optional):**

    This is used to pass any context if it's needed when creating new record. The format of passing it is

    Request Body

    ```js
    {
        "params": {
            "context": {
                "context_1": "context_1_value",
                "context_2": "context_2_value",
                ....
            },
            "data": {
                "field_1": "field_1_value",
                "field_2": "field_2_value",
                ....
            }
        }
    }
    ```

## 3. PUT

### Model records: 

`PUT /api/{model}/`
#### Headers
* Content-Type: application/json
#### Parameters
* **data (mandatory):**

    This is used to pass data to update, it works with filter parameter, See example below

* **filter (mandatory):**

    This is used to filter data to update. For example

    `PUT /api/product.template/`

    Request Body

    ```js
    {
        "params": {
            "filter": [["id", "=", 95]],
            "data": {
                "name": "Test product"
            }
        }
    }
    ```

    Response

    ```js
    {
        "jsonrpc": "2.0",
        "id": null,
        "result": true
    }
    ```

    Note: If the result is true it means success and if false or otherwise it means there was an error during update.

* **context (optional):**
    Just like in GET context is used to pass any context associated with record update. The format of passing it is

    Request Body

    ```js
    {
        "params": {
            "context": {
                "context_1": "context_1_value",
                "context_2": "context_2_value",
                ....
            },
            "filter": [["id", "=", 95]],
            "data": {
                "field_1": "field_1_value",
                "field_2": "field_2_value",
                ....
            }
        }
    }
    ```

* **operation (optional)**:

    This is only applied to `one2many` and `many2many` fields. The concept is sometimes you might not want to replace all records on either `one2many` or `many2many` fields, instead you might want to add other records or remove some records, this is where put operations comes in place. Thre are basically three PUT operations which are push, pop and delete. 
    * push is used to add/append other records to existing linked records
    * pop is used to remove/unlink some records from the record being updated but it doesn't delete them on the system
    * delete is used to remove/unlink and delete records permanently on the system

    For example here is how you would update `related_product_ids` which is `many2many` field with PUT operations

    `PUT /api/product.template/`

    Request Body

    ```js
    {
        "params": {
            "filter": [["id", "=", 95]],
            "data": {
                "related_product_ids": {
                    "push": [102, 30],
                    "pop": [45],
                    "delete": [55]
                }
            }
        }
    }
    ```

    This will append product with ids 102 and 30 as related products to product with id 95 and from there unlink product with id 45 and again unlink product with id 55 and delete it from the system. So if befor this request product with id 95 had [20, 37, 45, 55] as related product ids, after this request it will be [20, 37, 102, 30].

    Note: You can use one operation or two or all three at a time depending on what you want to update on your field. If you dont use these operations on `one2many` and `many2many` fields, existing values will be replaced by new values passed, so you need to be very carefull on this part.

    Response:

    ```js
    {
        "jsonrpc": "2.0",
        "id": null,
        "result": true
    }
    ```

### Model record: 

`PUT /api/{model}/{id}`
#### Headers
* Content-Type: application/json
#### Parameters
* data (mandatory)
* context (optional)
* PUT operation(push, pop, delete) (optional)

All parameters works the same as explained on previous section, what changes is that here they apply to a single record being updated and we don't have filter parameter because `id` of record to be updated is passed on URL as `{id}`. Example to give us an idea of how this works.

`PUT /api/product.template/95/`

Request Body

```js
{
    "params": {
        "data": {
            "related_product_ids": {
                "push": [102, 30],
                "pop": [45],
                "delete": [55]
            }
        }
    }
}
```

## 4. DELETE

### Model records: 

`DELETE /api/{model}/`
#### Parameters
* **filter (mandatory):**

    This is used to filter data to delete. For example

    `DELETE /api/product.template/?filter=[["id", "=", 95]]`

    Response

    ```js
    {
        "result": true
    }
    ```
    
    Note: If the result is true it means success and if false or otherwise it means there was an error during deletion.


### Model records: 

`DELETE /api/{model}/{id}`
#### Parameters
This takes no parameter and we don't have filter parameter because `id` of record to be deleted is passed on URL as `{id}`. Example to give us an idea of how this works.

`DELETE /api/product.template/95/`

Response

```js
{
    "result": true
}
```

## Calling Model's Function

Sometimes you might need to call model's function or a function bound to a record, inorder to do so, send a `POST` request with a body containing arguments(args) and keyword arguments(kwargs) required by the function you want to call.

Below is how you can call model's function

`POST /object/{model}/{function name}`

Request Body

```js
{
    "params": {
	"args": [arg1, arg2, ..],
	"kwargs ": {
	    "key1": "value1",
	    "key2": "value2",
	    ...
	}
    }
}
```

And below is how you can call a function bound to a record

`POST /object/{model}/{record_id}/{function name}`

Request Body

```js
{
    "params": {
	"args": [arg1, arg2, ..],
	"kwargs ": {
	    "key1": "value1",
	    "key2": "value2",
	    ...
	}
    }
}
```

In both cases the response will be the result returned by the function called