Skip to content

Commit c12b608

Browse files
committed
Cleaner Http response decoding
1 parent 1a46070 commit c12b608

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

lnetatmo.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ def getCameraPicture(self, image_id, key):
425425
"image_id" : image_id,
426426
"key" : key
427427
}
428-
resp = postRequest(_GETCAMERAPICTURE_REQ, postParams, json_resp=False)
428+
resp = postRequest(_GETCAMERAPICTURE_REQ, postParams)
429429
image_type = imghdr.what('NONE.FILE',resp)
430430
return resp, image_type
431431

@@ -544,8 +544,7 @@ class WelcomeData(HomeData):
544544

545545
# Utilities routines
546546

547-
def postRequest(url, params, json_resp=True):
548-
# Netatmo response body size limited to 64k (should be under 16k)
547+
def postRequest(url, params):
549548
if version_info.major == 3:
550549
req = urllib.request.Request(url)
551550
req.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
@@ -556,13 +555,11 @@ def postRequest(url, params, json_resp=True):
556555
headers = {"Content-Type" : "application/x-www-form-urlencoded;charset=utf-8"}
557556
req = urllib2.Request(url=url, data=params, headers=headers)
558557
resp = urllib2.urlopen(req)
559-
data = ""
560-
for buff in iter(lambda: resp.read(65535), b''):
561-
data += buff.decode("utf-8")
562-
if json_resp:
563-
return json.loads(data)
564-
else:
565-
return data
558+
data = b""
559+
for buff in iter(lambda: resp.read(65535), b''): data += buff
560+
# Return values in bytes if not json data to handle properly camera images
561+
returnedContentType = resp.getheader("Content-Type") if version_info.major == 3 else resp.info()["Content-Type"]
562+
return json.loads(data.decode("utf-8")) if "application/json" in returnedContentType else data
566563

567564
def toTimeString(value):
568565
return time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime(int(value)))

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
setup(
66
name='lnetatmo',
7-
version='1.3.4',
7+
version='1.3.5',
88
classifiers=[
99
'Development Status :: 5 - Production/Stable',
1010
'Intended Audience :: Developers',
@@ -17,7 +17,7 @@
1717
scripts=[],
1818
data_files=[],
1919
url='https://github.com/philippelt/netatmo-api-python',
20-
download_url='https://github.com/philippelt/netatmo-api-python/tarball/v1.3.4.tar.gz',
20+
download_url='https://github.com/philippelt/netatmo-api-python/tarball/v1.3.5.tar.gz',
2121
license='GPL V3',
2222
description='Simple API to access Netatmo weather station data from any python script.'
2323
)

0 commit comments

Comments
 (0)