4444__version__ = "0.0.0-auto.0"
4545__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Adafruit_IO.git"
4646
47+ CLIENT_HEADERS = {
48+ 'User-Agent' : 'AIO-CircuitPython/{0}' .format (__version__ )
49+ }
50+
4751class RESTClient ():
4852 """
4953 REST Client for interacting with the Adafruit IO API.
@@ -62,9 +66,17 @@ def __init__(self, adafruit_io_username, adafruit_io_key, wifi_manager):
6266 self .wifi = wifi_manager
6367 else :
6468 raise TypeError ("This library requires a WiFiManager object." )
65- self .http_headers = [{bytes ("X-AIO-KEY" , "utf-8" ):bytes (self .key , "utf-8" ),
66- bytes ("Content-Type" , "utf-8" ):bytes ('application/json' , "utf-8" )},
67- {bytes ("X-AIO-KEY" , "utf-8" ):bytes (self .key , "utf-8" )}]
69+ self ._aio_headers = [{"X-AIO-KEY" :self .key ,
70+ "Content-Type" :'application/json' },
71+ {"X-AIO-KEY" :self .key ,}]
72+
73+ @staticmethod
74+ def _create_headers (io_headers ):
75+ """Creates http request headers.
76+ """
77+ headers = CLIENT_HEADERS .copy ()
78+ headers .update (io_headers )
79+ return headers
6880
6981 @staticmethod
7082 def _create_data (data , metadata ):
@@ -103,7 +115,7 @@ def _post(self, path, payload):
103115 response = self .wifi .post (
104116 path ,
105117 json = payload ,
106- headers = self .http_headers [0 ])
118+ headers = self ._create_headers ( self . _aio_headers [0 ]) )
107119 self ._handle_error (response )
108120 return response .json ()
109121
@@ -115,7 +127,7 @@ def _get(self, path, return_text=False):
115127 """
116128 response = self .wifi .get (
117129 path ,
118- headers = self .http_headers [1 ])
130+ headers = self ._create_headers ( self . _aio_headers [1 ]) )
119131 self ._handle_error (response )
120132 if return_text :
121133 return response .text
@@ -128,7 +140,7 @@ def _delete(self, path):
128140 """
129141 response = self .wifi .delete (
130142 path ,
131- headers = self .http_headers [0 ])
143+ headers = self ._create_headers ( self . _aio_headers [0 ]) )
132144 self ._handle_error (response )
133145 return response .json ()
134146
0 commit comments