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