Commit 56eeac5e authored by Romain Courteaud's avatar Romain Courteaud

erp5_json_rpc_api: WIP 2 actions

parent 81215fb0
...@@ -26,11 +26,11 @@ ...@@ -26,11 +26,11 @@
############################################################################## ##############################################################################
import base64 # import base64
import binascii # import binascii
import json import json
import typing import typing
import six # import six
from six.moves.urllib.parse import unquote from six.moves.urllib.parse import unquote
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
...@@ -40,7 +40,7 @@ if typing.TYPE_CHECKING: ...@@ -40,7 +40,7 @@ if typing.TYPE_CHECKING:
_ = ( _ = (
OpenAPIOperation, OpenAPIParameter, HTTPRequest, Any, Callable, Optional) OpenAPIOperation, OpenAPIParameter, HTTPRequest, Any, Callable, Optional)
import jsonschema # import jsonschema
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from zExceptions import NotFound from zExceptions import NotFound
from zope.publisher.browser import BrowserView from zope.publisher.browser import BrowserView
...@@ -51,9 +51,6 @@ import zope.interface ...@@ -51,9 +51,6 @@ import zope.interface
from Products.ERP5Type import Permissions, PropertySheet from Products.ERP5Type import Permissions, PropertySheet
from erp5.component.document.JsonRpcAPITypeInformation import ( from erp5.component.document.JsonRpcAPITypeInformation import (
NoMethodForOperationError,
ParameterValidationError,
SchemaDefinitionError,
byteify byteify
) )
from erp5.component.document.OpenAPIService import OpenAPIService from erp5.component.document.OpenAPIService import OpenAPIService
...@@ -119,6 +116,15 @@ class JsonRpcAPIService(OpenAPIService): ...@@ -119,6 +116,15 @@ class JsonRpcAPIService(OpenAPIService):
request_method = request.method.lower() request_method = request.method.lower()
matched_operation = None matched_operation = None
if (request_method == 'post') and (len(request_path_parts) == 1):
matched_operation = None
if request_path_parts[0] == 'WIP.create.installation':
matched_operation = 'jIOWebSection_createSoftwareInstallationFromJSON'
elif request_path_parts[0] == 'WIP.request':
matched_operation = 'jIOWebSection_requestSoftwareInstanceFromJSON'
self.log('ROMAIN %s' % str(request_path_parts))
"""
for operation in self.getTypeInfo().getOpenAPIOperationIterator(): for operation in self.getTypeInfo().getOpenAPIOperationIterator():
if operation.request_method != request_method: if operation.request_method != request_method:
continue continue
...@@ -149,9 +155,11 @@ class JsonRpcAPIService(OpenAPIService): ...@@ -149,9 +155,11 @@ class JsonRpcAPIService(OpenAPIService):
# > their templated counterparts # > their templated counterparts
matched_operation = operation matched_operation = operation
continue continue
"""
request.other['traverse_subpath'] = request_path_parts request.other['traverse_subpath'] = request_path_parts
return matched_operation return matched_operation
'''
def getMethodForOperation(self, operation): def getMethodForOperation(self, operation):
# type: (OpenAPIOperation) -> Optional[Callable] # type: (OpenAPIOperation) -> Optional[Callable]
operation_id = operation.get('operationId') operation_id = operation.get('operationId')
...@@ -242,26 +250,15 @@ class JsonRpcAPIService(OpenAPIService): ...@@ -242,26 +250,15 @@ class JsonRpcAPIService(OpenAPIService):
{}, {},
schema, schema,
) )
'''
def executeMethod(self, request): def executeMethod(self, request):
self.log('XXX ROMAIN execute method')
if 1:
# jIOWebSection_requestSoftwareInstanceFromJSON
json_data = byteify(request.get('BODY'))
self.log(json_data)
return self.jIOWebSection_requestSoftwareInstanceFromJSON(json_data=byteify(json.loads(request.get('BODY'))))
#return 'nope'
#return self._asjIOStyle(mode="post", text_content=self.REQUEST.get('BODY'))
#raise NotImplementedError('plop')
# type: (HTTPRequest) -> Any # type: (HTTPRequest) -> Any
operation = self.getMatchingOperation(request) operation = self.getMatchingOperation(request)
if operation is None: if operation is None:
raise NotFound() raise NotFound()
method = self.getMethodForOperation(operation) method = getattr(self, operation)#self.getMethodForOperation(operation)
parameters = self.extractParametersFromRequest(operation, request) # parameters = self.extractParametersFromRequest(operation, request)
result = method(**parameters) result = method(json_data=byteify(json.loads(request.get('BODY'))))#**parameters)
response = request.RESPONSE response = request.RESPONSE
if response.getHeader('Content-Type'): if response.getHeader('Content-Type'):
return result return result
......
...@@ -165,9 +165,26 @@ class TestJsonRpcAPIConnectorView(JsonRpcAPITestCase): ...@@ -165,9 +165,26 @@ class TestJsonRpcAPIConnectorView(JsonRpcAPITestCase):
class TestJsonRpcAPIDemoDemoDemoXXXTOMoveTODO(JsonRpcAPITestCase): class TestJsonRpcAPIDemoDemoDemoXXXTOMoveTODO(JsonRpcAPITestCase):
def test_21_createSoftwareInstallation(self):
response = self.publish(
self.connector.getPath() + '/WIP.create.installation',
user='ERP5TypeTestCase',
request_method='POST',
stdin=io.BytesIO(
json.dumps(
{
"software_release_uri": "https://example.org/foobar",
"reference": "XXX",
"portal_type": "Software Instance",
"title": "new_name"
}).encode()),
env={'CONTENT_TYPE': 'application/json'})
self.assertEqual(response.getBody(), b'"ok"')
self.assertEqual(response.getStatus(), 200)
def test_21_request(self): def test_21_request(self):
response = self.publish( response = self.publish(
self.connector.getPath() + '/XXXcallAPIEndpointXXX', self.connector.getPath() + '/WIP.request',
user='ERP5TypeTestCase', user='ERP5TypeTestCase',
request_method='POST', request_method='POST',
stdin=io.BytesIO( stdin=io.BytesIO(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment