Skip to content

Fix typerror when offline #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions simplenote/simplenote.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@
import sys
if sys.version_info > (3, 0):
import urllib.request as urllib2
import urllib.error
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
import datetime
import uuid

try:
Expand Down Expand Up @@ -71,7 +65,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 +82,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 +97,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 +128,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 +186,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 +278,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 +310,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 +383,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
12 changes: 10 additions & 2 deletions tests/test_simplenote.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,18 @@ def test_simplenote_auth(self):
token = self.simplenote_instance.get_token()
self.assertNotEqual(None, token)

def test_simplenote_failed_auth(self):
s = simplenote.Simplenote(self.user, "")
def test_simplenote_failed_auth_with_incorrect_password(self):
s = simplenote.Simplenote(self.user, "incorrect-password")
self.assertRaises(simplenote.SimplenoteLoginFailed, s.get_token)

def test_simplenote_failed_auth_with_empty_password(self):
s = simplenote.Simplenote(self.user, "")
self.assertRaises(Exception, s.get_token)

def test_simplenote_failed_auth_with_empty_email_address(self):
s = simplenote.Simplenote("", "foo")
self.assertRaises(Exception, s.get_token)

def test_simplenote_get_list_length(self):
res, status = self.simplenote_instance.get_note_list()
if status == 0:
Expand Down