Skip to content

add user-agent to http header string, remove header byte encoding #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions adafruit_io/adafruit_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Adafruit_IO.git"

CLIENT_HEADERS = {
'User-Agent': 'AIO-CircuitPython/{0}'.format(__version__)
}

class RESTClient():
"""
REST Client for interacting with the Adafruit IO API.
Expand All @@ -62,9 +66,17 @@ def __init__(self, adafruit_io_username, adafruit_io_key, wifi_manager):
self.wifi = wifi_manager
else:
raise TypeError("This library requires a WiFiManager object.")
self.http_headers = [{bytes("X-AIO-KEY", "utf-8"):bytes(self.key, "utf-8"),
bytes("Content-Type", "utf-8"):bytes('application/json', "utf-8")},
{bytes("X-AIO-KEY", "utf-8"):bytes(self.key, "utf-8")}]
self._aio_headers = [{"X-AIO-KEY":self.key,
"Content-Type":'application/json'},
{"X-AIO-KEY":self.key,}]

@staticmethod
def _create_headers(io_headers):
"""Creates http request headers.
"""
headers = CLIENT_HEADERS.copy()
headers.update(io_headers)
return headers

@staticmethod
def _create_data(data, metadata):
Expand Down Expand Up @@ -103,7 +115,7 @@ def _post(self, path, payload):
response = self.wifi.post(
path,
json=payload,
headers=self.http_headers[0])
headers=self._create_headers(self._aio_headers[0]))
self._handle_error(response)
return response.json()

Expand All @@ -115,7 +127,7 @@ def _get(self, path, return_text=False):
"""
response = self.wifi.get(
path,
headers=self.http_headers[1])
headers=self._create_headers(self._aio_headers[1]))
self._handle_error(response)
if return_text:
return response.text
Expand All @@ -128,7 +140,7 @@ def _delete(self, path):
"""
response = self.wifi.delete(
path,
headers=self.http_headers[0])
headers=self._create_headers(self._aio_headers[0]))
self._handle_error(response)
return response.json()

Expand Down