import datetime import urllib import inspect import json from datetime import timedelta from urllib.request import Request class PRequest: Account="user@plaspy.com" ApiKey = "MyAPIKey" IdDevice = "IdDevice" LastLocation = True # Only for query GetLocation LastAlert = True # Only for query GetAlert StartUTC = str(datetime.datetime.utcnow() - timedelta(hours=12)) EndUTC = str(datetime.datetime.utcnow()) def props(self): pr = {} for name in dir(self): value = getattr(self, name) if not name.startswith('__') and not inspect.ismethod(value): pr[name] = value return pr def GetParamsRequest(self): return urllib.parse.urlencode(self.props()) def GetXMLRequest(self): request="" for name in dir(self): value = getattr(self, name) if not name.startswith('__') and not inspect.ismethod(value): request+="<"+name +">" + str(value) + "" request+= "" return request def MakeRequest(operation): APIURL = "https://api.plaspy.com/api/" request= PRequest() print("Testing " + operation + "\n") print("Testing POST") print("Request: " + request.GetParamsRequest()) f = urllib.request.urlopen(APIURL + operation, request.GetParamsRequest().encode()) response= f.read() print("Response: " + str(response)) print() f.close() print("Testing POST XML") print("Request: " + request.GetXMLRequest()) q= Request(APIURL + operation, request.GetXMLRequest().encode(), headers= {'Content-type': 'text/xml'}) f= urllib.request.urlopen(q) response= f.read() print("Response: " + str(response)) print() f.close() print("Testing POST JSON") print("Request: " + str(request.props())) q= Request(APIURL + operation, json.dumps(request.props()).encode(), headers= {'Content-type': 'application/json'}) f= urllib.request.urlopen(q) response= f.read() print("Response: " + str(response)) print() f.close() print("Testing Get JSON") print("Request: " + request.GetParamsRequest()) f = urllib.request.urlopen(APIURL + operation + "?" + request.GetParamsRequest()) response= f.read() print("Response: " + str(response)) print() f.close() MakeRequest("GetLocation"); MakeRequest("GetAlert");