obskyr / pushjet-py

A Python API for Pushjet. Send notifications to your phone from Python scripts!
MIT License
0 stars 4 forks source link

Empty response #1

Open ozroc opened 7 years ago

ozroc commented 7 years ago

Hello. I've found an issue with this simple test:

import pushjet
my_service = pushjet.Service.create(
    "My Laptop free space", 
    "http://icongal.com/gallery/image/338109/linux_hdd_hd_os_hardware_disk.png")

It just raises an exception:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-4-af0028397e30> in <module>()
      1 my_service = pushjet.Service.create(
      2     "My Laptop free space",
----> 3     "http://icongal.com/gallery/image/338109/linux_hdd_hd_os_hardware_disk.png")

/home/bluetc/miniconda3/lib/python3.6/site-packages/pushjet/pushjet.py in create(cls, name, icon_url)
    159         })
    160         _, response = cls._api._request('service', 'POST', data=data)
--> 161         return cls._from_data(response['service'])
    162 
    163 class Device(PushjetModel):

KeyError: 'service'

Digging a little bit, the response arrives empty.

Maybe this is caused by a server issue, but I think an additional check for a empty response and a more accurate exception would help.

Thank you!

Mechazawa commented 7 years ago

Service was having some issues, try again. It's most likely fixed now.

obskyr commented 7 years ago

Whoops, deleted my comment! For posterity, this is what I wrote:

I mean, ideally Pushjet shouldn't do this kind of thing... Methinks @Mechazawa's got some splainin' to do.

Perhaps I should add a few more checks for malformed responses! I'll see when I have time. I tried this out just now, but the Pushjet server just gives me a 502 Bad Gateway for every request I send, whatever the endpoint. Hrm HRM @Mechazawa cough hack sorry, frog in my throat. 😅

obskyr commented 7 years ago

@Mechazawa Yup, seems to be up again! Seems to be an old version, though...? HTTP status codes aren't being returned as they should be - we're back in 200 land.

ozroc commented 7 years ago

It works perfectly now.

Anyway, I think it could be easy to check responses and raise better exceptions. I'll take a look at it and try to make a PR.

Thanks friends!

obskyr commented 7 years ago

Looks like the reason it seemed like an empty object was being returned was because of these lines in Api._request:

        try:
            response = r.json()
        except ValueError:
            response = {}

That's probably not the way to handle that. I should probably turn malformed responses and 502s (perhaps other status codes, too?) into ServerErrors.