I am trying to do a GET request with a Python script on a button script.
I have previously got it to work with a simpler API, but the new API i am needing to use required HMAC-SHA356 encoding.
I finally managed to get it to work, but have noticed a strange behaviour when running the script from different systems.
[ul]1) When i run the script from a Windows 7 Ignition 7.9 Server, it works fine.
2) When i run it through a designer on my laptop pointing to the Win7 server, it works fine.
3) When i run the script on a Debian Ignition 7.8 Server, it works fine.
4) When i run it through a designer on my local laptop (Windows 8.1) pointing to the Debian Ignition server, i get the following error:
[/ul]
[quote]urllib2.URLError: <urlopen error (10054, ‘Software caused connection abort’)>
[/quote]
I don’t quite understand why the same code can work in most instances except this one case. Is there some sort of communication issue without request timeouts when sending a request from a remote designer to the Gateway? My guess is that the Gateway is cancelling the request, but i really have no idea, its over my head.
The code i’m executing is:
[code]def updateProductsAPI():
“”"
This function reads all products from Unleashed via API call.
The API returns JSON data. The GUID is compared with those in the SQL table.
If a record exists in SQL, it is updated. Otherwise the new record is inserted.
“”"
import system
import urllib2
import hashlib
import base64
import hmac
# Generate URL for API call to read products
url = "https://api.unleashedsoftware.com/" + "Products?"
urlArgs = ""
# Encrypt signature using key
apiID = "XXXX"
apiKEY = "XXXXXX-XXXXX"
message = urlArgs.encode('utf-8')
secret = apiKEY.encode('utf-8')
signature = base64.b64encode(hmac.new(secret,message, digestmod=hashlib.sha256).digest())
# Create header with information required by API
urlHeader = {"Accept": "application/json", "api-auth-id": apiID, "api-auth-signature": signature, "Content-Type": "application/json"}
# Read in data from Unleashed API
request = urllib2.Request(url, headers=urlHeader)
contents = urllib2.urlopen(request).read()
data = system.util.jsonDecode(contents)
print "Success!!"
event.source.Updating = 0
end def
“”" Start of main function “”"
event.source.Updating = 1
system.util.invokeAsynchronous(updateProductsAPI)[/code]