Python Custom scripting service math

From DreamFactory
Jump to: navigation, search
DreamFactoryTutorialsPython Custom scripting service math
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
=== Tutorial ===
+
You can create any number of scripting services and invoke them from the REST API. In this example we'll create a simple service named 'math' that accepts two numbers as query parameters and returns the result as JSON. Since the script is a service, you can control access to it using roles. The script has whatever access to the API that is allowed by the role of the user calling the script. If allowed, the script can make additional REST API calls or cURL requests.
You can create any number of custom scripting services and invoke them from the REST API. In this example we'll create a simple service named 'math' that accepts two numbers as query parameters and returns the result as JSON. Since the script is a service, you can control access to it using roles. The script has whatever access to the API that is allowed by the role of the user calling the script. If allowed, the script can make additional REST API calls or cURL requests.
+
  
=== Create a Python custom scripting service ===
+
=== Create a Python Scripting Service ===
  
 
* Go to the services tab in the admin console and click Create.   
 
* Go to the services tab in the admin console and click Create.   
* Set the Service Type to Custom Scripting Service.   
+
* Set the Service Type to Script-->Python.   
 
* Set the service name to 'math'.   
 
* Set the service name to 'math'.   
 
* Set the service label to 'Math'.   
 
* Set the service label to 'Math'.   
* Go to the Config tab for the new service and set the Script Engine Type to Python. 
 
 
* Set the Content to contain the following code then click Create Service.   
 
* Set the Content to contain the following code then click Create Service.   
  
 +
'''''NOTE: DreamFactory Python scripting requires 'bunch' package to be installed on server. ''''' <code>sudo pip install bunch</code>
  
<pre>
+
<source lang=python>
verb = event['request']['method'];
+
verb = event.request.method;
 
+
 
if verb != 'GET':
 
if verb != 'GET':
 
     raise Exception('Only HTTP GET is allowed on this endpoint.');
 
     raise Exception('Only HTTP GET is allowed on this endpoint.');
   
+
 
# get resource, /math —> "", /math/add —> "add"
 
# get resource, /math —> "", /math/add —> "add"
resource = event['resource'];
+
resource = event.resource;
 
+
 
# get query params from request
 
# get query params from request
params = event['request']['parameters'];
+
params = event.request.parameters;
 
+
 
required = ['n1', 'n2'];
 
required = ['n1', 'n2'];
 
+
 
if resource != "":
 
if resource != "":
 
     for element in required:
 
     for element in required:
 
         if params.get(element, "") == "":
 
         if params.get(element, "") == "":
 
             raise Exception('Missing ' + element + ' in params.');
 
             raise Exception('Missing ' + element + ' in params.');
   
+
     n1 = float(params['n1']);
+
     n1 = float(params.n1);
     n2 = float(params['n2']);
+
     n2 = float(params.n2);
   
+
 
if resource == "":
 
if resource == "":
 
     # /math means return all supported resources
 
     # /math means return all supported resources
Line 49: Line 48:
 
else:
 
else:
 
     raise Exception('Invalid or missing resource name.');
 
     raise Exception('Invalid or missing resource name.');
 
+
 
return result;
 
return result;
</pre>
+
</source>
  
  
=== Call the service ===
+
=== Call the Service ===
  
 
From any REST client, make the request '''''GET /api/v2/math/add?n1=4&n2=5''''' and you should get back the result of 9.
 
From any REST client, make the request '''''GET /api/v2/math/add?n1=4&n2=5''''' and you should get back the result of 9.
 
A simple REST client can be found at '''''<your_instance_url>/test_rest.html'''''.   
 
A simple REST client can be found at '''''<your_instance_url>/test_rest.html'''''.   
Remember if you are not an admin user your user role must allow access to the custom scripting service.
+
Remember, if you are not an admin user, your user role must allow access to the scripting service.

Latest revision as of 22:22, 15 July 2016

You can create any number of scripting services and invoke them from the REST API. In this example we'll create a simple service named 'math' that accepts two numbers as query parameters and returns the result as JSON. Since the script is a service, you can control access to it using roles. The script has whatever access to the API that is allowed by the role of the user calling the script. If allowed, the script can make additional REST API calls or cURL requests.

Create a Python Scripting Service

  • Go to the services tab in the admin console and click Create.
  • Set the Service Type to Script-->Python.
  • Set the service name to 'math'.
  • Set the service label to 'Math'.
  • Set the Content to contain the following code then click Create Service.

NOTE: DreamFactory Python scripting requires 'bunch' package to be installed on server. sudo pip install bunch

verb = event.request.method;
 
if verb != 'GET':
    raise Exception('Only HTTP GET is allowed on this endpoint.');
 
# get resource, /math —> "", /math/add —> "add"
resource = event.resource;
 
# get query params from request
params = event.request.parameters;
 
required = ['n1', 'n2'];
 
if resource != "":
    for element in required:
        if params.get(element, "") == "":
            raise Exception('Missing ' + element + ' in params.');
 
    n1 = float(params.n1);
    n2 = float(params.n2);
 
if resource == "":
    # /math means return all supported resources
    result = {'resource':['add', 'subtract', 'multiply', 'divide']};
elif resource == "add":
    result = {'result':(n1+n2)};
elif resource == "subtract":
    result = {'result':(n1-n2)};
elif resource == "multiply":
    result = {'result':(n1*n2)};
elif resource == "divide":
    if n2 == 0:
        raise Exception('Divide by zero error.');
    result = {'result':(n1/n2)};
else:
    raise Exception('Invalid or missing resource name.');
 
return result;


Call the Service

From any REST client, make the request GET /api/v2/math/add?n1=4&n2=5 and you should get back the result of 9. A simple REST client can be found at <your_instance_url>/test_rest.html. Remember, if you are not an admin user, your user role must allow access to the scripting service.