Frappe API gives data inside
message’ in its json response ,is it possible to change it somehow like make it
{
‘data’:{
}
status_code:CODE,
message:Message
Status:True
}
?and throw proper errors in permission error case?
ankush
September 27, 2023, 7:30am
2
API v2 is WIP, it will give data in data
key
frappe:develop
← ankush:api_v2
opened 12:22PM - 03 Sep 23 UTC
### Why?
- Versioning lets us improve the way APIs work while still being backw… ard compatible.
- We have known few problems with APIs for long time but they can't be easily addressed because they will likely require backward incompatible changes.
### Changes
- All API routing is now handled by Werkzeug routing.
- New alias for API versions `/api/*` == `/api/v1/`
- `/api/v2` prefix for new APIs.
- API versions will ONLY specify URL and response structure.
- REST APIs will not break between versions. RPC calls and schema are out of the scope of versioning, they CAN change between versions. Keeping them backward compatible is job of developers doing the changes.
### V2 changes and features
##### 1. Shorthand for all whitelisted functions in controller files.
```diff
- GET /api/v1/method/erpnext.selling.doctype.sales_order.sales_order.make_sales_invoice
+ GET /api/v2/method/Sales Order/make_sales_invoice
```
##### 2. Call whitelisted functions directly on ~~resources~~ => documents.
```diff
- POST /api/v1/resource/Sales Order/SO-0001?run_method=submit
+ POST /api/v2/document/Sales Order/SO-0001/method/submit
```
closes https://github.com/frappe/frappe/issues/14869
##### 3. Response Structure change
- [x] `message` => `data` key for response data **everywhere**.
- [x] `errors` key for nested and rich error information, replacing `exc`/`exc_type` etc. closes https://github.com/frappe/frappe/issues/14905
##### 4. URL/verb differences
- PATCH == PUT for updating documents. PATCH is the correct convention for partial updates.
- APIs are now divided in 3 types:
- `document` - previously known as `resource`
- `method` - RPC calls, same as before
- `doctype` - new class of APIs that operate on doctype instead of documents. e.g. meta, count, list etc.
### TODO
- [x] This PR currently copies a lot of code to demonstrate changes. Rewrite all code that touches v2 APIs before merge.
- [x] Merge `handle.py` and `api.py`, remove `globals()` lookups in RPC handler. That's unnecessary special handling.
- [x] URL encoding of names
- [x] Finalize response structure
- [x] Impl Errors and message response structure
- [x] Tests for both versions. Aim 100% coverage for this.
- [x] Collections should have methods, e.g. `GET /api/resource/Sales Order/method/count`, meta (?)
- [ ] File creation and attachment
- [ ] Update client side code, frappeclient.
Optional / separate PRs.
- [ ] Access to actual response object from whitelisted functions.
- [ ] frappe.response.header for sending custom headers
- [ ] get_value alternate
- [ ] Documents should have methods like add attachment, get attachment, add comment, etc
- [ ] Bulk update
- [ ] permissions ??
- [ ] bulk insert with array
- [x] delete child doc
- [ ] update js `frappe.db`
- [ ] `?expand=["link_field_name"]` to link fields to their linked documents.
You can still return raw response from your function and it will sent as is. Checkout werkzeug response object.
2 Likes
Can you please elaborate more on this…like what it is and how to get it working?
rahib
March 4, 2024, 12:02pm
4
For anyone looking for the raw response using werkzeug, the answer is mentioned here.
For custom functions/endpoints you can use werkzeug Response
object
import json
from werkzeug.wrappers import Response
@frappe.whitelist()
def my_custom_endpoint():
data = {"foo": "bar"}
response = Response(json.dumps(data), content_type='application/json')
response.status_code = 200
return response
2 Likes