Improve DNS lookup for PlatformIO API

This commit is contained in:
Ivan Kravets
2016-03-21 14:17:36 +02:00
parent 4256d6b6a3
commit c327cd5c3f
3 changed files with 19 additions and 7 deletions

View File

@ -9,7 +9,8 @@ PlatformIO 2.0
* Launched `PlatformIO Community Forums <http://community.platformio.org>`_
(`issue #530 <https://github.com/platformio/platformio/issues/530>`_)
* Disable progress bar for download operations when prompts are disabled
* Improved DNS lookup for PlatformIO API
* Disabled progress bar for download operations when prompts are disabled
* Fixed multiple definition errors for ST STM32 development platform and mbed
framework
(`issue #571 <https://github.com/platformio/platformio/issues/571>`_)

View File

@ -33,6 +33,7 @@ __license__ = "Apache Software License"
__copyright__ = "Copyright 2014-2016 Ivan Kravets"
__apiurl__ = "http://api.platformio.org"
__apiip__ = "178.62.159.183"
if sys.version_info >= (3, 0, 0):

View File

@ -24,7 +24,7 @@ from os.path import abspath, basename, dirname, expanduser, isdir, isfile, join
from platform import system, uname
from threading import Thread
from platformio import __apiurl__, __version__, exception
from platformio import __apiip__, __apiurl__, __version__, exception
# pylint: disable=wrong-import-order
try:
@ -322,18 +322,26 @@ def get_request_defheaders():
)}
def get_api_result(path, params=None, data=None):
def get_api_result(path, params=None, data=None, skipdns=False):
import requests
result = None
r = None
headers = get_request_defheaders()
url = __apiurl__
if skipdns:
url = "http://%s" % __apiip__
headers['host'] = __apiurl__[__apiurl__.index("://")+3:]
try:
if data:
r = requests.post(__apiurl__ + path, params=params, data=data,
headers=get_request_defheaders())
r = requests.post(
url + path, params=params, data=data, headers=headers,
timeout=(3, 13)
)
else:
r = requests.get(__apiurl__ + path, params=params,
headers=get_request_defheaders())
r = requests.get(
url + path, params=params, headers=headers, timeout=(3, 13))
result = r.json()
r.raise_for_status()
except requests.exceptions.HTTPError as e:
@ -342,6 +350,8 @@ def get_api_result(path, params=None, data=None):
else:
raise exception.APIRequestError(e)
except requests.exceptions.ConnectionError:
if not skipdns:
return get_api_result(path, params, data, skipdns=True)
raise exception.APIRequestError(
"Could not connect to PlatformIO Registry Service. "
"Please try later.")