refactor: resource => document

This lets us create three types of APIs:

- Document APIs that operate on documents
- DocType APIs that operate on collections - list, count, search
- Method APIs that are RPC calls
This commit is contained in:
Ankush Menat 2023-09-05 15:45:39 +05:30
parent 10ace97ebb
commit e63f0c895d
2 changed files with 25 additions and 8 deletions

View file

@ -1,3 +1,12 @@
"""REST API v2
This file defines routes and implementation for REST API.
Note:
- All functions in this file should be treated as "whitelisted" as they are exposed via routes
- None of the functions present here should be called from python code, their location and
internal implementation can change without treating it as "breaking change".
"""
import json
from werkzeug.routing import Rule
@ -12,11 +21,12 @@ def handle_rpc_call(method: str, doctype: str | None = None):
from frappe.modules.utils import load_doctype_module
if doctype:
# Expand to run actual method
# Expand to run actual method from doctype controller
module = load_doctype_module(doctype)
method = module.__name__ + "." + method
if method == "login":
# Login works implicitly right now.
return
return execute_cmd(method)
@ -96,13 +106,13 @@ def execute_doc_method(doctype: str, name: str, method: str | None = None):
url_rules = [
Rule("/method/<method>", endpoint=handle_rpc_call),
Rule("/method/<doctype>/<method>", endpoint=handle_rpc_call),
Rule("/resource/<doctype>", methods=["GET"], endpoint=document_list),
Rule("/resource/<doctype>", methods=["POST"], endpoint=create_doc),
Rule("/resource/<doctype>/<path:name>/", methods=["GET"], endpoint=read_doc),
Rule("/resource/<doctype>/<path:name>/", methods=["PUT"], endpoint=update_doc),
Rule("/resource/<doctype>/<path:name>/", methods=["DELETE"], endpoint=delete_doc),
Rule("/document/<doctype>", methods=["GET"], endpoint=document_list),
Rule("/document/<doctype>", methods=["POST"], endpoint=create_doc),
Rule("/document/<doctype>/<path:name>/", methods=["GET"], endpoint=read_doc),
Rule("/document/<doctype>/<path:name>/", methods=["PUT"], endpoint=update_doc),
Rule("/document/<doctype>/<path:name>/", methods=["DELETE"], endpoint=delete_doc),
Rule(
"/resource/<doctype>/<path:name>/method/<method>/",
"/document/<doctype>/<path:name>/method/<method>/",
methods=["GET", "POST"],
endpoint=execute_doc_method,
),

View file

@ -91,6 +91,13 @@ def parameterize(*api_versions):
return decorator
resource_key = {
"": "resource",
"v1": "resource",
"v2": "document",
}
class FrappeAPITestCase(FrappeTestCase):
version = "" # Empty implies v1
TEST_CLIENT = get_test_client()
@ -100,7 +107,7 @@ class FrappeAPITestCase(FrappeTestCase):
return get_url()
def resource_path(self, *parts):
return self.get_path("resource", *parts)
return self.get_path(resource_key[self.version], *parts)
def method_path(self, *method):
return self.get_path("method", *method)