Skip to content

Commit

Permalink
Merge pull request #82 from rbw0/logging
Browse files Browse the repository at this point in the history
Adds logging
  • Loading branch information
rbw authored Feb 15, 2018
2 parents 7a9a96b + 4cbc74a commit 4ce9d56
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 29 deletions.
13 changes: 12 additions & 1 deletion pysnow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@
from .resource import Resource

__author__ = "Robert Wikman <[email protected]>"
__version__ = "0.6.7"
__version__ = "0.6.8"

# Set default logging handler to avoid "No handler found" warnings.
import logging
try: # Python 2.7+
from logging import NullHandler
except ImportError:
class NullHandler(logging.Handler):
def emit(self, record):
pass

logging.getLogger(__name__).addHandler(NullHandler())
24 changes: 17 additions & 7 deletions pysnow/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import logging
import inspect
import warnings

Expand All @@ -16,6 +17,8 @@

warnings.simplefilter("always", DeprecationWarning)

logger = logging.getLogger('pysnow')


class Client(object):
"""User-created Client object.
Expand Down Expand Up @@ -61,10 +64,11 @@ def __init__(self,
if not (host or instance):
raise InvalidUsage("You must supply either 'instance' or 'host'")

if not (user and password) and not session:
raise InvalidUsage("You must supply either username and password or a session object")
elif (user and session) is not None:
raise InvalidUsage("Provide either username and password or a session, not both.")
if not isinstance(self, pysnow.OAuthClient):
if not (user and password) and not session:
raise InvalidUsage("You must supply either username and password or a session object")
elif (user and session) is not None:
raise InvalidUsage("Provide either username and password or a session, not both.")

self.parameters = ParamsBuilder()

Expand All @@ -80,9 +84,12 @@ def __init__(self,
self._user = user
self._password = password
self.use_ssl = use_ssl

self.base_url = URLBuilder.get_base_url(use_ssl, instance, host)
self.session = self._get_session(session)

if not isinstance(self, pysnow.OAuthClient):
self.session = self._get_session(session)
else:
self.session = None

def _get_session(self, session):
"""Creates a new session with basic auth, unless one was provided, and sets headers.
Expand All @@ -91,10 +98,13 @@ def _get_session(self, session):
:return:
- :class:`requests.Session` object
"""

if not session:
logger.debug('(SESSION_CREATE) User: %s' % self._user)
s = requests.Session()
s.auth = HTTPBasicAuth(self._user, self._password)
else:
logger.debug('(SESSION_CREATE) Object: %s' % session)
s = session

s.headers.update(
Expand Down Expand Up @@ -146,7 +156,7 @@ def resource(self, api_path=None, base_path='/api/now', chunk_size=None):
return Resource(api_path=api_path,
base_path=base_path,
parameters=self.parameters,
chunk_size=chunk_size,
chunk_size=chunk_size or 8192,
session=self.session,
base_url=self.base_url)

Expand Down
21 changes: 13 additions & 8 deletions pysnow/oauth_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

import warnings
import six
import logging

from oauthlib.oauth2 import LegacyApplicationClient
from oauthlib.oauth2.rfc6749.errors import OAuth2Error
Expand All @@ -12,6 +12,8 @@

warnings.simplefilter("always", DeprecationWarning)

logger = logging.getLogger('pysnow')


class OAuthClient(Client):
"""Pysnow `Client` with extras for oauth session and token handling.
Expand All @@ -34,7 +36,6 @@ def __init__(self, client_id=None, client_secret=None, token_updater=None, **kwa
'provided user / password credentials or sessions will be ignored.')

# Forcibly set session, user and password
kwargs['session'] = OAuth2Session(client=LegacyApplicationClient(client_id=client_id))
kwargs['user'] = None
kwargs['password'] = None

Expand Down Expand Up @@ -113,7 +114,7 @@ def resource(self, api_path=None, base_path='/api/now', chunk_size=None):

if isinstance(self.token, dict):
self.session = self._get_oauth_session()
return super(OAuthClient, self).resource(api_path, base_path)
return super(OAuthClient, self).resource(api_path, base_path, chunk_size)

raise MissingToken("You must set_token() before creating a resource with OAuthClient")

Expand All @@ -128,12 +129,16 @@ def generate_token(self, user, password):
- TokenCreateError: If there was an error generating the new token
"""

logger.debug('(TOKEN_CREATE) :: User: %s' % user)

session = OAuth2Session(client=LegacyApplicationClient(client_id=self.client_id))

try:
return dict(self.session.fetch_token(token_url=self.token_url,
username=user,
password=password,
client_id=self.client_id,
client_secret=self.client_secret))
return dict(session.fetch_token(token_url=self.token_url,
username=user,
password=password,
client_id=self.client_id,
client_secret=self.client_secret))
except OAuth2Error as exception:
raise TokenCreateError('Error creating user token', exception.description, exception.status_code)

Expand Down
16 changes: 13 additions & 3 deletions pysnow/request.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# -*- coding: utf-8 -*-

import logging
import json

from .response import Response
from .exceptions import InvalidUsage

logger = logging.getLogger('pysnow')


class SnowRequest(object):
"""Creates a new :class:`SnowRequest` object.
Expand All @@ -14,11 +17,12 @@ class SnowRequest(object):
:param url_builder: :class:`url_builder.URLBuilder` object
"""

def __init__(self, parameters=None, session=None, url_builder=None, chunk_size=None):
def __init__(self, parameters=None, session=None, url_builder=None, chunk_size=None, parent=None):
self._parameters = parameters
self._url_builder = url_builder
self._session = session
self._chunk_size = chunk_size or 8192
self._chunk_size = chunk_size
self._parent = parent

self._url = url_builder.get_url()

Expand All @@ -32,9 +36,15 @@ def _get_response(self, method, **kwargs):
- :class:`pysnow.Response` object
"""

response = self._session.request(method, self._url, stream=True, params=self._parameters.as_dict(), **kwargs)
params = self._parameters.as_dict()

logger.debug('(REQUEST_SEND) Method: %s, Resource: %s' % (method, self._parent))

response = self._session.request(method, self._url, stream=True, params=params, **kwargs)
response.raw.decode_content = True

logger.debug('(RESPONSE_RECEIVE) Code: %d, Resource: %s' % (response.status_code, self._parent))

return Response(response, self._chunk_size)

def get(self, query, limit=None, offset=None, fields=list()):
Expand Down
10 changes: 8 additions & 2 deletions pysnow/resource.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# -*- coding: utf-8 -*-

import logging

from copy import deepcopy

from .request import SnowRequest
from .url_builder import URLBuilder

logger = logging.getLogger('pysnow')


class Resource(object):
"""Creates a new :class:`Resource` object
Expand All @@ -31,8 +35,10 @@ def __init__(self, base_url=None, base_path=None, api_path=None, parameters=None

self.parameters = deepcopy(parameters)

logger.debug('(RESOURCE_ADD) Object: %s, chunk_size: %d' % (self, kwargs.get('chunk_size')))

def __repr__(self):
return '<%s [%s]>' % (self.__class__.__name__, self.path)
return '<%s [%s] at %s>' % (self.__class__.__name__, self.path, hex(id(self)))

@property
def path(self):
Expand All @@ -42,7 +48,7 @@ def path(self):
def _request(self):
parameters = deepcopy(self.parameters)

return SnowRequest(url_builder=self._url_builder, parameters=parameters, **self.kwargs)
return SnowRequest(url_builder=self._url_builder, parameters=parameters, parent=self, **self.kwargs)

def get(self, query, limit=None, offset=None, fields=list()):
"""Queries the API resource
Expand Down
12 changes: 6 additions & 6 deletions pysnow/url_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

class URLBuilder(object):
def __init__(self, base_url, base_path, api_path):
self._base_url = base_url
self._base_path = base_path
self._api_path = api_path
self.base_url = base_url
self.base_path = base_path
self.api_path = api_path
self.full_path = base_path + api_path

self._resource_url = "%(base_url)s%(base_path)s%(api_path)s" % (
self._resource_url = "%(base_url)s%(full_path)s" % (
{
'base_url': base_url,
'base_path': base_path,
'api_path': api_path
'full_path': self.full_path
}
)

Expand Down
2 changes: 0 additions & 2 deletions tests/test_oauth_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def test_oauth_session_user_override(self):
c = OAuthClient(instance="test", client_id="test1", client_secret="test2",
session="testsess", user="testuser", password="testpass")

self.assertEqual(isinstance(c.session, OAuth2Session), True)
self.assertEqual(c._user, None)
self.assertEqual(c._password, None)

Expand All @@ -86,7 +85,6 @@ def test_set_token(self):
c.set_token(self.mock_token)

self.assertEqual(c.token, self.mock_token)
self.assertEqual(isinstance(c.session, OAuth2Session), True)

def test_request_without_token(self):
"""OauthClient should raise MissingToken when creating query when no token has been set"""
Expand Down

0 comments on commit 4ce9d56

Please sign in to comment.