Skip to content

Commit

Permalink
remove deprecated HTTPError; remove requests’ exception wrappers
Browse files Browse the repository at this point in the history
to fix AttributeError: 'HTTPError' in burnash#430
  • Loading branch information
burnash committed Dec 20, 2016
1 parent e93d4dd commit addd494
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 43 deletions.
59 changes: 25 additions & 34 deletions gspread/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

from . import urlencode
from .ns import _ns
from .httpsession import HTTPSession, HTTPError
from .httpsession import HTTPSession
from .exceptions import RequestError
from .models import Spreadsheet
from .urls import (
construct_url,
DRIVE_FILES_API_V2_URL,
DRIVE_FILES_UPLOAD_API_V2_URL
)
from .utils import finditem, extract_id_from_url
from .exceptions import (SpreadsheetNotFound, UpdateCellError, RequestError)
from .exceptions import (SpreadsheetNotFound, UpdateCellError)


class Client(object):
Expand Down Expand Up @@ -189,10 +190,7 @@ def del_spreadsheet(self, file_id):
file_id
)

try:
self.session.delete(url)
except HTTPError as ex:
raise RequestError(ex.message)
self.session.delete(url)

def del_worksheet(self, worksheet):
url = construct_url(
Expand All @@ -219,9 +217,9 @@ def put_feed(self, url, data):

try:
r = self.session.put(url, data, headers=headers)
except HTTPError as ex:
if getattr(ex, 'code', None) == 403:
raise UpdateCellError(ex.message)
except RequestError as ex:
if ex[0] == 403:
raise UpdateCellError(ex[1])
else:
raise

Expand All @@ -231,10 +229,7 @@ def post_feed(self, url, data):
headers = {'Content-Type': 'application/atom+xml'}
data = self._ensure_xml_header(data)

try:
r = self.session.post(url, data, headers=headers)
except HTTPError as ex:
raise RequestError(ex.message)
r = self.session.post(url, data, headers=headers)

return ElementTree.fromstring(r.content)

Expand Down Expand Up @@ -292,18 +287,15 @@ def import_csv(self, file_id, data):
headers = {'Content-Type': 'text/csv'}
url = '{0}/{1}'.format(DRIVE_FILES_UPLOAD_API_V2_URL, file_id)

try:
self.session.put(
url,
data=data,
params={
'uploadType': 'media',
'convert': True
},
headers=headers
)
except HTTPError as ex:
raise RequestError(ex.message)
self.session.put(
url,
data=data,
params={
'uploadType': 'media',
'convert': True
},
headers=headers
)

def list_permissions(self, file_id):
"""Retrieve a list of permissions for a file.
Expand Down Expand Up @@ -374,13 +366,15 @@ def insert_permission(
'sendNotificationEmails': notify,
'emailMessage': email_message
}

headers = {'Content-Type': 'application/json'}

try:
self.session.post(url, json.dumps(data), params=params, headers=headers)
except HTTPError as ex:
raise RequestError(ex.message)
self.session.post(
url,
json.dumps(data),
params=params,
headers=headers
)

def remove_permission(self, file_id, permission_id):
"""Deletes a permission from a file.
Expand All @@ -395,10 +389,7 @@ def remove_permission(self, file_id, permission_id):
)
headers = {'Content-Type': 'application/json'}

try:
self.session.delete(url, headers=headers)
except HTTPError as ex:
raise RequestError(ex.message)
self.session.delete(url, headers=headers)


def authorize(credentials):
Expand Down
6 changes: 0 additions & 6 deletions gspread/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,3 @@ class UpdateCellError(GSpreadException):

class RequestError(GSpreadException):
"""Error while sending API request."""

class HTTPError(RequestError):
"""DEPRECATED. Error while sending API request."""
def __init__(self, code, msg):
super(HTTPError, self).__init__(msg)
self.code = code
7 changes: 4 additions & 3 deletions gspread/httpsession.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
basestring = unicode = str


from .exceptions import HTTPError
from .exceptions import RequestError


class HTTPSession(object):
Expand Down Expand Up @@ -60,11 +60,12 @@ def request(self, method, url, data=None, params=None, headers=None, files=None,
try:
func = getattr(self.requests_session, method.lower())
except AttributeError:
raise Exception("HTTP method '{}' is not supported".format(method))
raise RequestError("HTTP method '{}' is not supported".format(method))

response = func(url, data=data, params=params, headers=request_headers, files=files, json=json)

if response.status_code > 399:
raise HTTPError(response.status_code, "{0}: {1}".format(
raise RequestError(response.status_code, "{0}: {1}".format(
response.status_code, response.content))
return response

Expand Down

0 comments on commit addd494

Please sign in to comment.