Skip to content

Commit

Permalink
Merge pull request #1 from mmshivesh/post-json-bugfix
Browse files Browse the repository at this point in the history
Added new error type and fix empty JSON from POST
  • Loading branch information
mmshivesh authored Jul 19, 2020
2 parents d6cc348 + 93c6d26 commit c41be2b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 9 additions & 5 deletions pysnoonotes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests

from .auth import SnooNotesAuth
from .errors import LoginFailedError
from .errors import RequestFailedError

class SnooNotes(SnooNotesAuth):
def __init__(self, username, user_key):
Expand Down Expand Up @@ -39,14 +39,18 @@ def _authed_request(self, request_type, endpoint, data=None):
r = requests.post(url=self._endpoint_url(endpoint), headers={
"Authorization": f"{self.token_type} {self.access_token}"
}, json=data)
if r.ok:
return
else:
raise RequestFailedError(f"{request_type} request returned a non-ok code: {r.status_code}")
elif request_type == "GET":
r = requests.get(url=self._endpoint_url(endpoint), headers={
"Authorization": f"{self.token_type} {self.access_token}"
})
if r.ok:
return r.json()
else:
raise LoginFailedError(f"{request_type} request returned a non-ok code: {r.status_code}")
if r.ok:
return r.json()
else:
raise RequestFailedError(f"{request_type} request returned a non-ok code: {r.status_code}")

def add_note_for_user(self, username, note_type, subreddit, message, url):
"""Add a Snoonote to the `username` under the specific `subreddit`.
Expand Down
6 changes: 5 additions & 1 deletion pysnoonotes/errors.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class LoginFailedError(Exception):
"""Raised when login to Snoonotes fails."""
"""Raised when login to Snoonotes API fails."""
pass

class RequestFailedError(Exception):
"""Raised when a query to Snoonotes API fails."""
pass

0 comments on commit c41be2b

Please sign in to comment.