forked from platformio/platformio-core
Put UserAgent for all http requests
This commit is contained in:
@ -4,9 +4,8 @@
|
||||
import click
|
||||
import requests
|
||||
|
||||
from platformio import __version__
|
||||
from platformio import __version__, util
|
||||
from platformio.exception import GetLatestVersionError
|
||||
from platformio.util import exec_command
|
||||
|
||||
|
||||
@click.command("upgrade",
|
||||
@ -22,9 +21,9 @@ def cli():
|
||||
click.secho("Please wait while upgrading PlatformIO ...",
|
||||
fg="yellow")
|
||||
|
||||
pip_result = exec_command(["pip", "install", "--upgrade",
|
||||
"platformio"])
|
||||
pio_result = exec_command(["platformio", "--version"])
|
||||
pip_result = util.exec_command(["pip", "install", "--upgrade",
|
||||
"platformio"])
|
||||
pio_result = util.exec_command(["platformio", "--version"])
|
||||
|
||||
if last in pio_result['out'].strip():
|
||||
click.secho("PlatformIO has been successfully upgraded to %s" %
|
||||
@ -37,7 +36,9 @@ def cli():
|
||||
def get_latest_version():
|
||||
try:
|
||||
pkgdata = requests.get(
|
||||
"https://pypi.python.org/pypi/platformio/json").json()
|
||||
"https://pypi.python.org/pypi/platformio/json",
|
||||
headers=util.get_request_defheaders()
|
||||
).json()
|
||||
return pkgdata['info']['version']
|
||||
except:
|
||||
raise GetLatestVersionError()
|
||||
|
@ -9,9 +9,9 @@ from time import mktime
|
||||
from click import progressbar
|
||||
from requests import get
|
||||
|
||||
from platformio import util
|
||||
from platformio.exception import (FDSHASumMismatch, FDSizeMismatch,
|
||||
FDUnrecognizedStatusCode)
|
||||
from platformio.util import change_filemtime, exec_command
|
||||
|
||||
|
||||
class FileDownloader(object):
|
||||
@ -27,7 +27,8 @@ class FileDownloader(object):
|
||||
self.set_destination(join(dest_dir, self._fname))
|
||||
self._progressbar = None
|
||||
|
||||
self._request = get(url, stream=True)
|
||||
self._request = get(url, stream=True,
|
||||
headers=util.get_request_defheaders())
|
||||
if self._request.status_code != 200:
|
||||
raise FDUnrecognizedStatusCode(self._request.status_code, url)
|
||||
|
||||
@ -66,11 +67,12 @@ class FileDownloader(object):
|
||||
|
||||
dlsha1 = None
|
||||
try:
|
||||
result = exec_command(["sha1sum", self._destination])
|
||||
result = util.exec_command(["sha1sum", self._destination])
|
||||
dlsha1 = result['out']
|
||||
except OSError:
|
||||
try:
|
||||
result = exec_command(["shasum", "-a", "1", self._destination])
|
||||
result = util.exec_command(
|
||||
["shasum", "-a", "1", self._destination])
|
||||
dlsha1 = result['out']
|
||||
except OSError:
|
||||
pass
|
||||
@ -83,7 +85,7 @@ class FileDownloader(object):
|
||||
def _preserve_filemtime(self, lmdate):
|
||||
timedata = parsedate_tz(lmdate)
|
||||
lmtime = mktime(timedata[:9])
|
||||
change_filemtime(self._destination, lmtime)
|
||||
util.change_filemtime(self._destination, lmtime)
|
||||
|
||||
def __del__(self):
|
||||
self._request.close()
|
||||
|
@ -12,8 +12,7 @@ from time import time
|
||||
import click
|
||||
import requests
|
||||
|
||||
from platformio import __version__, app
|
||||
from platformio.util import exec_command, get_systype
|
||||
from platformio import __version__, app, util
|
||||
|
||||
|
||||
class TelemetryBase(object):
|
||||
@ -77,7 +76,7 @@ class MeasurementProtocol(TelemetryBase):
|
||||
dpdata.append("Click/%s" % click.__version__)
|
||||
# dpdata.append("Requests/%s" % requests.__version__)
|
||||
try:
|
||||
result = exec_command(["scons", "--version"])
|
||||
result = util.exec_command(["scons", "--version"])
|
||||
match = re.search(r"engine: v([\d\.]+)", result['out'])
|
||||
if match:
|
||||
dpdata.append("SCons/%s" % match.group(1))
|
||||
@ -86,7 +85,7 @@ class MeasurementProtocol(TelemetryBase):
|
||||
self['an'] = " ".join(dpdata)
|
||||
|
||||
def _prefill_custom_data(self):
|
||||
self['cd1'] = get_systype()
|
||||
self['cd1'] = util.get_systype()
|
||||
self['cd2'] = "Python/%s %s" % (platform.python_version(),
|
||||
platform.platform())
|
||||
self['cd4'] = 1 if app.get_setting("enable_prompts") else 0
|
||||
@ -155,6 +154,7 @@ class MPDataPusher(threading.Thread):
|
||||
r = self.http_session().post(
|
||||
"https://ssl.google-analytics.com/collect",
|
||||
data=data,
|
||||
headers=util.get_request_defheaders(),
|
||||
timeout=3
|
||||
)
|
||||
r.raise_for_status()
|
||||
|
@ -242,19 +242,22 @@ def get_logicaldisks():
|
||||
return disks
|
||||
|
||||
|
||||
def get_request_defheaders():
|
||||
return {"User-Agent": "PlatformIO/%s %s" % (
|
||||
__version__, requests.utils.default_user_agent())}
|
||||
|
||||
|
||||
def get_api_result(path, params=None, data=None):
|
||||
result = None
|
||||
r = None
|
||||
|
||||
try:
|
||||
headers = {"User-Agent": "PlatformIO/%s %s" % (
|
||||
__version__, requests.utils.default_user_agent())}
|
||||
|
||||
if data:
|
||||
r = requests.post(__apiurl__ + path, params=params, data=data,
|
||||
headers=headers)
|
||||
headers=get_request_defheaders())
|
||||
else:
|
||||
r = requests.get(__apiurl__ + path, params=params, headers=headers)
|
||||
r = requests.get(__apiurl__ + path, params=params,
|
||||
headers=get_request_defheaders())
|
||||
result = r.json()
|
||||
r.raise_for_status()
|
||||
except requests.exceptions.HTTPError as e:
|
||||
|
Reference in New Issue
Block a user