7ewis
Posts: 130
Joined: Wed Dec 26, 2012 11:30 am

RESTful Web Service

Thu Feb 27, 2014 11:03 am

Hello,

I'm not entirely sure what I am looking for, but I need to be able to run the REST API on my Pi. I'll explain my current setup first.

I have a Raspberry Pi, that eventually is going to be able to communicate with Salesforce using REST, and when a High Priority Support Case comes in, the Pi will turn on a flashing light.

At the moment, I have the Pi connected into my network running Raspbian. It hosts a website using Lighttpd, with Python CGI enabled. So I have set it up so that I can use POST from the index page, specifiying a password to allow control, power on/off, a time period to stay on, and the device I want to control. So for example, I could go to:

cgi-bin/power.py?device=1&power=on&time=0&password=password

And that would turn on Device 1 indefinitely. (I don't think it's relevant, but the way that all works is via a Python wrapper, which allows me to send commands to a USB RF Transmitter, which is paired to 3 RF Sockets plugged into the wall).

So I have most of it working, it's just the connectivity with Salesforce I need to work out. But I have never used REST before, and don't really have much experience in programming anyway. My friend knows how to setup the Salesforce side, which I don't have direct access to, so I just need to get the REST API setup.

Does anyone here have any experience with it? Where should I start?
Raspberry Pi - Model B (512MB)

User avatar
topguy
Posts: 6284
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: RESTful Web Service

Thu Feb 27, 2014 12:35 pm

I'm a little unsure what is client and what is server in your setup.

Are you going to "poll" Salesforce via REST to detect a state change ( High Priority Support Caes present or not) ?
(Then your friend has to specify the REST API so you can use it.)

Or is the Salesforce going to use REST to trigger the light automatically when a HPSC arrives ?
(In which case you have to specidy a REST interface for your friend to use.)

Both CherryPy and web.py are Python modules you can use to create simple RESTful interfaces. (you can probably skip the lighttp server)

7ewis
Posts: 130
Joined: Wed Dec 26, 2012 11:30 am

Re: RESTful Web Service

Thu Feb 27, 2014 3:08 pm

topguy wrote:I'm a little unsure what is client and what is server in your setup.

Are you going to "poll" Salesforce via REST to detect a state change ( High Priority Support Caes present or not) ?
(Then your friend has to specify the REST API so you can use it.)

Or is the Salesforce going to use REST to trigger the light automatically when a HPSC arrives ?
(In which case you have to specidy a REST interface for your friend to use.)

Both CherryPy and web.py are Python modules you can use to create simple RESTful interfaces. (you can probably skip the lighttp server)
Sorry, Salesforce is going to trigger the light when a case comes in. So I believe I have to create some sort of REST service on the Pi?

What do they actually do? Because I do already have Lighttpd setup. Would it be easy to transition over?

Say I do use CherryPy, what actaully needs to be setup to allow Salesforce to connect to it using REST? From what i have reasearched, it looks like I need a small bit of code stating what it needs to return or something, which would just be a POST to power.py?power=on&device=1 or something?
Raspberry Pi - Model B (512MB)

User avatar
topguy
Posts: 6284
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: RESTful Web Service

Fri Feb 28, 2014 10:02 am

A simple tutorial http://rest.elkstein.org/2008/02/what-is-rest.html can probably explain it better than me.

If your friend has integrated Salesforce with other REST interfaces before he should probably give you a guideline on how the API could look.

7ewis
Posts: 130
Joined: Wed Dec 26, 2012 11:30 am

Re: RESTful Web Service

Wed Mar 26, 2014 10:49 am

I have now installed Flask onto the Pi. And it accepts JSON, as I can see the requests it receives in the console.

Does anyone have any experience with Flask? I'm not too sure how I would parse requests, and forward them to my Python script to turn on/off the devices.
Raspberry Pi - Model B (512MB)

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: RESTful Web Service

Wed Mar 26, 2014 2:17 pm

REST is not a technology. It is not a language or a service. It is a philosophy. POST and GET are methods that fits within this framework philosophy.

Responding to post query requires nothing more than either a service listening specifically for it, or something basic like a web server than takes action. In apache, for example, you can set up a script alias in your site config that will process a URL from, say, cgi or wsgi. If you can use GET, you can display your passed parameters using javascript if you like. Anyhow, the point is that you don't need a fancy framework to do what you are trying to do. I'll link you an apache example when I get a second here. Your configuration will be different for your server obviously.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: RESTful Web Service

Wed Mar 26, 2014 3:15 pm

Here is a sample wsgi script that retrieves POST parameters and processes in python. I call it using AJAX in a web page, but you can use it for direct POST calls if you like. It take a WSGIScriptAlias directive in the apache site configuration, with mod_wsgi enabled.

https://github.com/iinnovations/iicontr ... tions.wsgi
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

Return to “Advanced users”