HTTP request to API

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
skribb
Posts: 28
Joined: 22 Jun 2016, 20:52

HTTP request to API

13 Oct 2016, 06:24

i want to try and make HTTP requests to the LIFX api. Currently I use a python script in Eventghost.

Code: Select all

import requests

token = "xxxxxxxxxxxxxxxxxxxxxxxxxx"

headers = {
    "Authorization": "Bearer %s" % token,
}

payload = {
    "power": "on", "duration": 0.1, "color": "white brightness:1.0 kelvin:4500"
}

response = requests.put('https://api.lifx.com/v1/lights/all/state', data=payload, headers=headers)

##### kelvin is a little miffy. sometimes it works to change it, sometimes it doesn't
How would I implement the same in AHK?


Thanks a lot guys :superhappy:
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: HTTP request to API

13 Oct 2016, 08:25

I kinda like the library Requests, so I would suggest you keep using Python, if you want to control it using AHK, you can parse it parameters when you run it.

The AHK Script:

Code: Select all

#Include StdOutToVar.ahk

MsgBox, % LIFX_SetState("all", "on", "white kelvin:4500", 1.0, 0.1)

LIFX_SetState(selector, power, color, brightness, duration){
	return StdOutStream("python LIFX.py " """" selector """ """ power """ """ color """ """ brightness """ """ duration """")
}
The Python Script:

Code: Select all

import requests, sys

def LIFX_SetState(selector, power, color, brightness, duration):
	try:
		s = requests.Session()
		headers = {"Authorization": "Bearer " + "xxxxxxxxxxxxxxxxxxxxxxxxxx"}
		payload = {"power": power, "duration": duration, "color": color, "brightness": brightness}
		
		response = s.put("https://api.lifx.com/v1/lights/" + selector + "/state", data = payload, headers = headers)
		return JsonToStr(response)
	except:
		return "Error"

def JsonToStr(response):
	res = ""
	for key in response:
		res += key + ": ["
		for light in response[key]:
			res += "{"
			for key in light:
				res += key + ": " + light[key] + ", "
			res = res[0:-2] + "},"
		res = res[0:-1] + "],\n"
	return res[0:-2]

print(LIFX_SetState(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]), end = "")
You will also need this Script by Sean, HotKeyIt and SKAN.

Code: Select all

StdOutStream(sCmd, Callback = "") {		; Modified  :  SKAN 31-Aug-2013 http://goo.gl/j8XJXY                             
	Static StrGet := "StrGet"           ; Thanks to :  HotKeyIt         http://goo.gl/IsH1zs                                   
									    ; Original  :  Sean 20-Feb-2007 http://goo.gl/mxCdn
									  
	DllCall("CreatePipe", "UIntP", hPipeRead, "UIntP", hPipeWrite, "UInt", 0, "UInt", 0)
	DllCall("SetHandleInformation", "UInt", hPipeWrite, "UInt", 1, "UInt", 1)
	
	VarSetCapacity(STARTUPINFO, A_Is64bitOS ? 104 : 68,  0)     ; STARTUPINFO          ;  http://goo.gl/fZf24
	NumPut(68,         STARTUPINFO,  0)      					; cbSize
	NumPut(0x100,      STARTUPINFO, A_Is64bitOS ? 60 : 44)      ; dwFlags    =>  STARTF_USESTDHANDLES = 0x100
	NumPut(hPipeWrite, STARTUPINFO, A_Is64bitOS ? 88 : 60)      ; hStdOutput
	NumPut(hPipeWrite, STARTUPINFO, A_Is64bitOS ? 96 : 64)      ; hStdError
	
	VarSetCapacity(PROCESS_INFORMATION, A_Is64bitOS ? 32 : 16)  ; PROCESS_INFORMATION  ;  http://goo.gl/b9BaI
	
	If(!DllCall("CreateProcess", "UInt", 0, "UInt", &sCmd, "UInt", 0, "UInt", 0 ;  http://goo.gl/USC5a
							   , "UInt", 1, "UInt", 0x08000000, "UInt", 0, "UInt", 0
							   , "UInt", &STARTUPINFO, "UInt", &PROCESS_INFORMATION)){
		Return ""
			, DllCall("CloseHandle", "UInt", hPipeWrite)
			, DllCall("CloseHandle", "UInt", hPipeRead)
			, DllCall("SetLastError", "Int", -1)
	}
	
	hProcess := NumGet(PROCESS_INFORMATION, 0)
	hThread  := NumGet(PROCESS_INFORMATION, A_Is64bitOS ? 8 : 4)
	
	DllCall("CloseHandle", "UInt", hPipeWrite)
	
	AIC := (SubStr(A_AhkVersion, 1, 3) = "1.0") ;  A_IsClassic
	VarSetCapacity(Buffer, 4096, 0), nSz := 0
	
	While DllCall("ReadFile", "UInt", hPipeRead, "UInt", &Buffer, "UInt", 4094, "UIntP", nSz, "Int", 0) {
		tOutput := (AIC && NumPut(0, Buffer, nSz, "Char") && VarSetCapacity(Buffer, -1)) ? Buffer : %StrGet%(&Buffer, nSz, "CP850")
		
		Isfunc(Callback) ? %Callback%(tOutput, A_Index) : sOutput .= tOutput
	}
	DllCall("GetExitCodeProcess", "UInt", hProcess, "UIntP", ExitCode)
	DllCall("CloseHandle",  "UInt", hProcess)
	DllCall("CloseHandle",  "UInt", hThread)
	DllCall("CloseHandle",  "UInt", hPipeRead)
	DllCall("SetLastError", "UInt", ExitCode)
	
	Return Isfunc(Callback) ? %Callback%("", 0) : sOutput
}
A little disclaimer, since I neither have a LIFX bulb nor an access token I haven't been able to test this, but I have been using the same setup https://autohotkey.com/boards/viewtopic ... 609#p93609.
Please excuse my spelling I am dyslexic.
skribb
Posts: 28
Joined: 22 Jun 2016, 20:52

Re: HTTP request to API

14 Oct 2016, 10:48

Capn Odin wrote:I kinda like the library Requests, so I would suggest you keep using Py[/url].

When running the script all I get is a blank msgbox. I have installed Python 3.5.2 which I figured would be needed
User avatar
Capn Odin
Posts: 1352
Joined: 23 Feb 2016, 19:45
Location: Denmark
Contact:

Re: HTTP request to API

14 Oct 2016, 11:28

I think Python 3.3 would suffice, but try this.

Code: Select all

import requests, sys

def LIFX_SetState(selector, power, color, brightness, duration):
	try:
		s = requests.Session()
		headers = {"Authorization": "Bearer " + "xxxxxxxxxxxxxxxxxxxxxxxxxx"}
		payload = {"power": power, "duration": duration, "color": color, "brightness": brightness}
		
		response = s.put("https://api.lifx.com/v1/lights/" + selector + "/state", data = payload, headers = headers)
		print(response.text)
		return response.json()
	except:
		return "Error"

print(LIFX_SetState(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]), end = "")
You should be guaranteed to get some response if it work now, and I hate saying this but I assume you have used your access token.
This should help debugging the problem.

Edit: Sorry, I don't know why I wrote this so incoherently.
Please excuse my spelling I am dyslexic.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee and 250 guests