Lewis Lebentz
Lewis Lebentz

Reputation: 875

How to make a Flask Endpoint?

As you will be able to see from my previous questions, I have been working on a project, and really want to know how I can get this last part finished.

Quick summary of project: I have a Raspberry Pi that is running a Web Server (Lighttpd) and Flask. It has an RF USB Transmitter connected, which controls the power of a plug via a Python script. (Power.pyon GitHub). This works.

I now need to create an Endpoint in Flask so that Salesforce can send it some JSON, and it will understand it.

I want to keep this as simple as I can, so I understand what it's actually doing. In my last question, someone did provide me with something, but I thought it'd be better have a specific question relating to it, rather than trying to cover too much in one.

All I need to be able to send is 'power=on/off', 'device=0,1,2', 'time=(secondsasinteger)' and 'pass=thepassword' I can send this as URL variables, or a POST to my existing power.py linked above, and it does it.

I would like a simple, clear way of sending this from Salesforce in JSON, to Flask and make it understand the request.

Literally all I need to do now is go to: ip/cgi-bin/power.py?device=0&power=on&time=10&pass=password

That would load a Python script, and turn device 0 on for 10 seconds. (0 is unlimited).

How can I convert that to JSON? What code do I need to put into Flask for it to be able to comprehend that? Can I forward the variables onto the power.py so the Flask file only has to find the variables and values?

I have downloaded Postman in Chrome, and this allows me to send POST's to the Pi to test things.

Where can I find out more info about this, as a beginner?

Can I send something like this?

'requestNumber = JSONRequest.post(
"ip/api.py",
{
    deviceid: 0,
    pass: "password",
    time: 60,
    power: "on"
},'

Upvotes: 1

Views: 2855

Answers (1)

user1981924
user1981924

Reputation: 846

I don't know how you can get saleforce to send a POST request with an associated JSON, but capturing it with Flask is fairly easy. Consider the following example:

from flask import request
from yourmodule import whatever_function_you_want_to_launch
from your_app import app

@app.route('/power/', methods=['POST'])
def power():
    if request.headers['Content-Type'] == 'application/json':
        return whatever_function_you_want_to_launch(request.json)
    else:
        return response("json record not found in request", 415)

when saleforce visits the url http://example.com/power/ your applications executes the power() function passing it, as a parameter, a dictionary containing the JSON contents. The whatever_function_you_want_to_launch function can use the dictionary to trigger whatever action you want to take, and return a response back to the power() function. The power() function would return this respose back to salesforce.

For example:

def whatever_function_you_want_to_launch(data):
    device = data['deviceid']
    power = data['power']
    message = ""
    if power == "on":
       turn_power_on(device)
       message = "power turned on for device " + device
    else:
       turn_power_off(device)
       message = "power turned off for device " + device
    return make_response(message, 200)

this is just a short example, of course. You'd need to add some additional stuff (e.g. handle the case that the JSON is malformed, or does not contain one of the requested keys).

in order to test the whole stuff you can also use curl command (available on Linux, don't know on other OSs) with this type of syntax:

curl -H "Content-type: application/json" -X POST http://localhost:5000/power/ -d '{"deviceid": "0", "pass": "password", "time": "60", "power": "on"}'

Upvotes: 3

Related Questions