Skip to content

Commit

Permalink
fix TypeError when offline
Browse files Browse the repository at this point in the history
Simplenote.authenticate() and get_token() usually return a str.  When
offline, these methods return None.  The get_note(), update_note(), etc.
do not consider for this behavior.  Therefore, None will be set to http
header, and TypeError will occur when sending a request.

Related: cpbotha/nvpy#191

BROKEN CHANGE:

authenticate() and get_token() no longer catch the IOError,
BadStatusLine and part of HTTPError errors.
  • Loading branch information
yuuki0xff committed Oct 3, 2019
1 parent 74da92f commit 8237677
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions simplenote/simplenote.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
from urllib.error import HTTPError
import urllib.parse as urllib
import html
from http.client import BadStatusLine
from http.client import HTTPException, BadStatusLine
else:
import urllib2
from urllib2 import HTTPError
import urllib
from HTMLParser import HTMLParser
from httplib import BadStatusLine
from httplib import HTTPException, BadStatusLine

import base64
import time
Expand Down Expand Up @@ -71,7 +71,12 @@ def authenticate(self, user, password):
Returns:
Simplenote API token as string
Raises:
- SimplenoteLoginFailed
- HTTPError
- HTTPException
- IOError
"""

request = Request(AUTH_URL)
Expand All @@ -83,10 +88,10 @@ def authenticate(self, user, password):
try:
res = urllib2.urlopen(request).read()
token = json.loads(res.decode('utf-8'))["access_token"]
except (HTTPError, BadStatusLine):
raise SimplenoteLoginFailed('Login to Simplenote API failed!')
except IOError: # no connection exception
token = None
except HTTPError as e:
if e.code == 401:
raise SimplenoteLoginFailed('Login to Simplenote API failed! Check email address and password.')
raise
return token

def get_token(self):
Expand All @@ -98,6 +103,10 @@ def get_token(self):
Returns:
Simplenote API token as string
Raises:
- HTTPError
- HTTPException
- IOError
"""
if self.token == None:
self.token = self.authenticate(self.username, self.password)
Expand Down Expand Up @@ -125,9 +134,13 @@ def get_note(self, noteid, version=None):
if version is not None:
params_version = '/v/' + str(version)

try:
token = self.get_token()
except Exception as e:
return e, -1
params = '/i/%s%s' % (str(noteid), params_version)
request = Request(DATA_URL+params)
request.add_header(self.header, self.get_token())
request.add_header(self.header, token)
try:
response = urllib2.urlopen(request)
except HTTPError as e:
Expand Down Expand Up @@ -179,10 +192,14 @@ def update_note(self, note):
else:
url = '%s/i/%s?response=1' % (DATA_URL, noteid)

try:
token = self.get_token()
except Exception as e:
return e, -1
# TODO: Could do with being consistent here. Everywhere else is Request(DATA_URL+params)
note_to_update = self.__remove_simplenote_api_fields(note_to_update)
request = Request(url, data=json.dumps(note_to_update).encode('utf-8'))
request.add_header(self.header, self.get_token())
request.add_header(self.header, token)
request.add_header('Content-Type', 'application/json')

response = ""
Expand Down Expand Up @@ -267,8 +284,12 @@ def get_note_list(self, data=True, since=None, tags=[]):
params += '&data=true'

# perform initial HTTP request
try:
token = self.get_token()
except Exception as e:
return e, -1
request = Request(DATA_URL+params)
request.add_header(self.header, self.get_token())
request.add_header(self.header, token)
try:
response = urllib2.urlopen(request)
response_notes = json.loads(response.read().decode('utf-8'))
Expand All @@ -295,7 +316,7 @@ def get_note_list(self, data=True, since=None, tags=[]):

# perform the actual HTTP request
request = Request(DATA_URL+params)
request.add_header(self.header, self.get_token())
request.add_header(self.header, token)
try:
response = urllib2.urlopen(request)
response_notes = json.loads(response.read().decode('utf-8'))
Expand Down Expand Up @@ -368,9 +389,13 @@ def delete_note(self, note_id):
if (status == -1):
return note, status

try:
token = self.get_token()
except Exception as e:
return e, -1
params = '/i/%s' % (str(note_id))
request = Request(url=DATA_URL+params, method='DELETE')
request.add_header(self.header, self.get_token())
request.add_header(self.header, token)
try:
response = urllib2.urlopen(request)
except (IOError, BadStatusLine) as e:
Expand Down

0 comments on commit 8237677

Please sign in to comment.