How to Validate Connection to URL

Posted on
Sat Jul 08, 2017 8:44 am
zurich offline
Posts: 103
Joined: Aug 11, 2014

How to Validate Connection to URL

Greetings,

Would you be able to point me towards some example for to check if a connection to a URL is working?

I wish to do this check in "validateDeviceConfigUi"

kind regards,

Z

Posted on
Sat Jul 08, 2017 9:26 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: How to Validate Connection to URL

In the MyQ plugin, I just call my login routine in that method:

Code: Select all
        if not self.myqLogin(username=valuesDict['myqLogin'], password=valuesDict['myqPassword'], brand=valuesDict['openerBrand']):
            errorDict['myqLogin'] = u"Login to MyQ server failed, check login, password, and brand"
            errorDict['myqPassword'] = u"Login to MyQ server failed, check login, password, and brand"
            return (False, valuesDict, errorDict)


I use the requests module to actually fetch the URL:

Code: Select all
        url = self.apiData[brand]["service"] + '/api/v4/user/validate'
        headers = {
                'User-Agent':       userAgent,
                "BrandId":          "2",
                "ApiVersion":       "4.1",
                "Culture":          "en",
                'MyQApplicationId': self.apiData[brand]["appID"]
        }
        payload = {
                'username': username,
                'password': password
        }

        try:
            response = requests.post(url, json=payload, headers=headers)
        except requests.exceptions.RequestException as err:
            self.logger.debug(u"myqLogin failure, request url = %s" % (url))
            self.logger.error(u"myqLogin failure, RequestException: %s" % (str(err)))
            self.securityToken = ""
            return False

        if (response.status_code != requests.codes.ok):
            self.logger.debug(u"myqLogin failure, Enum err code %s" % (response.status_coderl))
            self.securityToken = ""
            return False       


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

Posted on
Wed Jul 12, 2017 9:30 pm
mclass offline
Posts: 312
Joined: May 13, 2015
Location: Melbourne, Australia

Re: How to Validate Connection to URL

Hi,

I use the following script to determine if my Fronius solar inverters are on line before attempting to access data. Not particularly pretty, but it works :shock:

Code: Select all
## Script to test connection to URL

# Assumes Indigo variable statURL is to be updated with status of URL
# Insert subject URL as TestURL.

from urllib2 import Request, urlopen, URLError, HTTPError

# Enter test URL here
TestUrl = 'http://XXX.XXX.XXX.XXX'
## DEBUGGING
debug = 0 ## Set to 1 to print URL's full response to Indigo log, to 0 for no debug

# Test connection
req = Request(TestUrl)
try:
    response = urlopen(req)
except HTTPError, e:
    indigo.server.log(str(e.code),"Couldn't fulfill request to server. Error code: ")
    indigo.variable.updateValue('statURL', "OFFLINE")
except URLError, e:
    indigo.server.log(str(e.reason),"We failed to reach server. Reason: ")
    indigo.variable.updateValue('statURL', "OFFLINE")   
else:
   indigo.server.log("URL connection OK.")
   indigo.variable.updateValue('statURL', "ONLINE")
   if debug:
      indigo.server.log(response.read(), "Response from testURL is:")


Hope this helps.

mclass

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests