How to Write Python Scripts in DreamFactory

How to Write Python Scripts in DreamFactory

This tutorial demonstrates how to write Python scripts in DreamFactory.

hi everyone this has been with Dream Factory in this short video I'm going to talk about scripting server-side scripting with Python this video is similar to the other videos on PHP scripting nodejs and JavaScript so basically the same material but this time we're going to cover it with Python so a little bit of context first go ahead and get yourself a Dream Factory instance here I'm running on localhost wherever you've installed Dream Factory you will have an account and you can log in and prerequisite for this of course is to have some services so make sure that you have some API endpoints and once you have those endpoints you may want to implement some logic so actually today we're going to cover a couple things we're going to talk about process event scripts and then I'll briefly cover writing custom scripts as well with Python so a process event is basically any action that you want to take on one of the API endpoints and DreamFactory either on the request or the response so for example what we'll show today is a field validation we'll also show a kind of workflow scenario where you want to basically kick off a push notification if a certain event occurs on an API call so the first thing the field validation will go ahead and you know click on scripts and then find the API endpoint that you want you can think of this user interface is basically an API tree forever all of the API endpoints and you can traverse and look at all the endpoints and figure out which endpoint you want to put logic on so here I'm going to go down to the database API I'm going to look at some tables and I want to decide okay do I want to do this on post action or a put action or a get action or whatever the action is here it'll be a post so I'll go here when I click on that basically I'll see all of the tables and you'll notice a pre process and a post process so it's pretty simple you have the API name the type of API it is here we're going to be making API calls on tables the table name the verb and then the pre process or post process so a pre process example with python is that you want to do something simple such as a field validation you want to make sure that you know a specific field for example isn't empty when it gets submitted to the server so here you can pick your language here of course it's Python you could upload a file if you wanted and that will bring that file in the source code that you've written into this UI make sure that you make your script active and if the script is going to modify either the requests or the response make sure you check that this is related to how the system processes it processes the script at runtime and relates to performance and optimization to make sure that you check that if you're going to be actually altering the request or the response so here you just write your Python code one important thing make sure that you have the bunch package installed in order for this to work you can do pip install Bunch in order to get that this particular example you're just write a little bit of Python that that runs prior to the script prior to the API call rather inserting a database records so basically loop through the payload look and see if a field is empty if it is and braze a throw and error so that's about it and then on the response similar type of thing except this time we're going to go to the contact table but we're gonna post process on the post so this basically means okay records got inserted we get a response back in this case it'll be a 200 response successful and then we're going to do something so again similars before select our language make it active and then write your code so here we're gonna basically write a little bit of Python code that stores a payload in a variable and then creates a message and then uses the platform API dot post so if you read our documentation platform API dot post is really useful because you can call any other endpoint from a script any other endpoint of Dream Factory so that's about it for event scripts for a brief overview if you know Python this is really easy or also you can pick the other languages that you like so next what I'll do is show a quick example of creating a service so this is basically a service as you maybe you want to write your own custom API so DreamFactory auto generates api's for tons and tons of different data sources so you don't have to write an API however sometimes you may want to write an API and DreamFactory lets you do that it's very easy so when you go here I'm just going to show an example when you create a new one you basically go down to script and then you'll write a script and you would pick Python here I'll show a worked example just a little calculator API similar to the other videos this time we'll write it in Python so give your API a name label and a description make sure it's active and then you can save it before you even read any code in the config section you can either upload a Python routine a script basically or you can type it in here so this basically does some simple math operations using Python as the programming language that's how the API will work at runtime when it gets some input parameters from the API call and then you can optionally put in a service definition what this is is basically a swagger or llamó file swagger 2.0 we have a bunch of tutorials on this but basically you can document the API the use of that is you can then see your calculator API that you wrote in Python or PHP or whatever language it'll show up in the API Doc's with endpoints and then you can actually attach logic on any of those endpoints if you want so you can write custom api's and then use event scripting to actually customize what happens on the request of the response in addition to just the raw API that you write so all in all very flexible system pick your language of choice Python PHP you can also hook up a node.js server and run it with Dream Factory or use v8 j s which is the native PHP runtime for our sorry JavaScript runtime for writing server-side scripts so that's it in a nutshell a couple other things you'll want to see if you go to wiki dot dream factor comm we have some a bunch of examples on server-side scripting with the different languages the one that I showed today the field validation and the push notification rule are here if you want to see the code as well as the custom scripting service with math so that that should get you started also check out our YouTube channel and you can go to Dream Factory comm /resources for more information and links to the docs as well as our community forum and that's at community dream factory calm ok I hope this was helpful and thanks

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *