Simple example to convert CURL/JSON to Python?

Posted on
Sat Oct 21, 2017 11:03 pm
spiv offline
Posts: 190
Joined: Jul 15, 2014

Simple example to convert CURL/JSON to Python?

Haven't done a lot with Python yet, but I have an external device that I would like to control.

I have sample integration commands from the manufacturer that are shown as CURL scripts with JSON data.

Is there a cookbook reference or sample files that show how I can convert the samples from CURL/JSON to Python scripts or is there a direct way to run the existing script with Indigo?

I can easily cut/paste the parameters in the CURL command line, but was hoping not to have to learn Python (yet) and find a simple "connect-the-dots" kind of solution where I can cut/paste some Python or scripting code and not have to delve into the full workings of it (yet).

At the basic level, I just want to issue commands to the device and I don't need to process any returned data - not even status. At least for the proof-of-concept tests.

Posted on
Sun Oct 22, 2017 4:12 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Simple example to convert CURL/JSON to Python?

Should be very straightforward. Post a copy of a command you want to execute and we'll cobble together an example script

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sun Oct 22, 2017 12:22 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Simple example to convert CURL/JSON to Python?

This goes in an "Execute Python Script" action.
Code: Select all
import requests
requests.get(<<put url that use use with curl here>>)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Oct 22, 2017 1:28 pm
spiv offline
Posts: 190
Joined: Jul 15, 2014

Re: Simple example to convert CURL/JSON to Python?

Here's an example of a curl command I would like to send:

Code: Select all
$ curl -X POST ‘http://localhost:47147/api/v1/sendcommands' -X POST -d ‘{ "delay"
: "1000", "commands" : [ { "type" : "command", "params" : { "command" : "VOLUME
UP", "device" : “2DCCA135-4264-41B1-842B-8118337C71DE”, “repeat” : “constant” } }
] }’
{
"status" : "success",
"data" : {
"session" : "96F4C281-B34D-47D4-9097-EA80874FAFE4"
},
"code" : 200
}

Posted on
Sun Oct 22, 2017 1:43 pm
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Simple example to convert CURL/JSON to Python?

Here's the reference for what you want to do: http://docs.python-requests.org/en/mast ... uickstart/

That's an interesting one because the data you're posting is actually a JSON string.

So something like this, I think:

Code: Select all
payload =‘{ "delay" : "1000", "commands" : [ { "type" : "command", "params" : { "command" : "VOLUME UP", "device" : “2DCCA135-4264-41B1-842B-8118337C71DE”, “repeat” : “constant” } } ] }’

requests.post("http://localhost:47147/api/v1/sendcommands", json=payload)

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sun Oct 22, 2017 1:54 pm
spiv offline
Posts: 190
Joined: Jul 15, 2014

Re: Simple example to convert CURL/JSON to Python?

FlyingDiver wrote:
Here's the reference for what you want to do: http://docs.python-requests.org/en/mast ... uickstart/

That's an interesting one because the data you're posting is actually a JSON string.



Thanks!

I'll take a look.

(yeah, the JSON stuff is why I asked. Looked more complex then just a "one liner" like the Indigo HTTP commands.)

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest