Skip to content

Commit

Permalink
Closes #4, Python 2 support
Browse files Browse the repository at this point in the history
Dani Carrion committed Sep 11, 2016
1 parent c1460e8 commit b16c185
Showing 4 changed files with 28 additions and 14 deletions.
18 changes: 13 additions & 5 deletions pyrestcli/auth.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import warnings
import requests
from gettext import gettext as _
from urllib.parse import urljoin

try:
from urllib.parse import urljoin
except ImportError:
from urlparse import urljoin

from .exceptions import BadRequestException, NotFoundException, ServerErrorException, AuthErrorException


class BaseAuthClient:
class BaseAuthClient(object):
""" Basic client to access (non)authorized REST APIs """
def __init__(self, base_url, proxies=None):
"""
@@ -80,12 +82,18 @@ class TokenAuthClient(BaseAuthClient):
This class provides you with authenticated access to APIs using a token-based HTTP Authentication scheme
The token will be included in the Authorization HTTP header, prefixed by a keyword (default: "Token"), with whitespace separating the two strings
"""
def __init__(self, token, *args, header_keyword="Token", **kwargs):
def __init__(self, token, *args, **kwargs):
"""
:param Token: Authentication token
:param header_keywork: Authorization HTTP header prefix
:param header_keyword: Authorization HTTP header prefix
:return:
"""
if 'header_keyword' in kwargs:
header_keyword = kwargs['header_keyword']
del kwargs['header_keyword']
else:
header_keyword = "Token"

if not self.base_url.startswith('https'):
warnings.warn(_("You are using unencrypted token authentication!!!"))

2 changes: 1 addition & 1 deletion pyrestcli/paginators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Paginator:
class Paginator(object):
def __init__(self, base_url):
self.base_url = base_url

20 changes: 13 additions & 7 deletions pyrestcli/resources.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import requests
from six import with_metaclass, iteritems
from future.utils import python_2_unicode_compatible
from datetime import datetime
from urllib.parse import urljoin
try:
from urllib.parse import urljoin
except ImportError:
from urlparse import urljoin

from .fields import Field
from .paginators import DummyPaginator


class APIConnected:
class APIConnected(object):
"""
This class handle API endpoints and interfaces with the authorization client for actually sending requests
"""
@@ -76,18 +81,19 @@ def __init__(cls, name, bases, nmspc):

for klass in bases:
if hasattr(klass, "Meta"):
for attribute_name, attribute in klass.Meta.__dict__.items():
for attribute_name, attribute in iteritems(klass.Meta.__dict__):
if not (attribute_name.startswith("__") or hasattr(cls.Meta, attribute_name)):
setattr(cls.Meta, attribute_name, attribute)

cls.fields = []
for attribute_name, attribute in cls.__dict__.items():
for attribute_name, attribute in iteritems(cls.__dict__):
if isinstance(attribute, Field):
attribute.name = attribute_name
cls.fields.append(attribute_name)


class Resource(APIConnected, metaclass=ResourceMetaclass):
@python_2_unicode_compatible
class Resource(with_metaclass(ResourceMetaclass, APIConnected)):
"""
Resource on the REST API
@@ -109,7 +115,7 @@ def __str__(self):
Give a nice representation for the resource
:param return: Resource friendly representation based on the self.Meta.name_field attribute
"""
return getattr(self, self.Meta.name_field, super(Resource, self).__str__()).encode("utf-8")
return getattr(self, self.Meta.name_field, super(Resource, self).__str__())

def get_resource_endpoint(self):
"""
@@ -124,7 +130,7 @@ def update_from_dict(self, attribute_dict):
:param attribute_dict: Dictionary to be mapped into object attributes
:return:
"""
for field_name, field_value in attribute_dict.items():
for field_name, field_value in iteritems(attribute_dict):
if self.fields is None or field_name in self.fields:
setattr(self, field_name, field_value)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -8,5 +8,5 @@
version="0.6.0",
license="MIT",
url="https://github.com/danicarrion/pyrestcli",
install_requires=['requests>=2.10.0', 'python-dateutil==2.5.3'],
install_requires=['requests>=2.10.0', 'python-dateutil==2.5.3', 'future==0.15.2'],
packages=["pyrestcli"])

0 comments on commit b16c185

Please sign in to comment.