-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from rbw0/branch0.3.2
Adds limit support in get_all() Enables easy access to last response (and request)
- Loading branch information
Showing
3 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
import inspect | ||
|
||
__author__ = "Robert Wikman <[email protected]>" | ||
__version__ = "0.3.1" | ||
__version__ = "0.3.2" | ||
|
||
|
||
class UnexpectedResponse(Exception): | ||
|
@@ -50,6 +50,10 @@ class NoResults(Exception): | |
pass | ||
|
||
|
||
class NoRequestExecuted(Exception): | ||
pass | ||
|
||
|
||
class QueryTypeError(TypeError): | ||
pass | ||
|
||
|
@@ -327,30 +331,63 @@ def __init__(self, method, table, **kwargs): | |
self.default_payload = kwargs.pop('default_payload') | ||
self.raise_on_empty = kwargs.pop('raise_on_empty') | ||
self.session = kwargs.pop('session') | ||
self.status_code = None | ||
self._last_response = None | ||
|
||
if method in ('GET', 'DELETE'): | ||
self.query = kwargs.pop('query') | ||
|
||
def _all_inner(self, fields): | ||
@property | ||
def last_response(self): | ||
"""Return _last_response after making sure an inner `requests.request` has been performed | ||
:raise: | ||
:NoRequestExecuted: If no request has been executed | ||
:return: last response | ||
""" | ||
if self._last_response is None: | ||
raise NoRequestExecuted("%s hasn't been executed" % self) | ||
return self._last_response | ||
|
||
@last_response.setter | ||
def last_response(self, response): | ||
""" Sets last_response property | ||
:param response: `requests.request` response | ||
""" | ||
self._last_response = response | ||
|
||
@property | ||
def status_code(self): | ||
"""Return last_response.status_code after making sure an inner `requests.request` has been performed | ||
:raise: | ||
:NoRequestExecuted: If no request has been executed | ||
:return: status_code of last_response | ||
""" | ||
if self.last_response is None: | ||
raise NoRequestExecuted("%s hasn't been executed" % self) | ||
|
||
return self.last_response.status_code | ||
|
||
def _all_inner(self, fields, limit): | ||
"""Yields all records for the query and follows links if present on the response after validating | ||
:return: List of records with content | ||
""" | ||
response = self.session.get(self._get_url(self.table), params=self._get_formatted_query(fields)) | ||
response = self.session.get(self._get_url(self.table), params=self._get_formatted_query(fields, limit)) | ||
yield self._get_content(response) | ||
while 'next' in response.links: | ||
self.url_link = response.links['next']['url'] | ||
response = self.session.get(self.url_link) | ||
yield self._get_content(response) | ||
|
||
def get_all(self, fields=list()): | ||
def get_all(self, fields=list(), limit=None): | ||
"""Wrapper method that takes whatever was returned by the _all_inner() generators and chains it in one result | ||
:param fields: List of fields to return in the result | ||
:param limit: Limits the number of records returned | ||
:return: Iterable chain object | ||
""" | ||
return itertools.chain.from_iterable(self._all_inner(fields)) | ||
return itertools.chain.from_iterable(self._all_inner(fields, limit)) | ||
|
||
def get_one(self, fields=list()): | ||
"""Convenience function for queries returning only one result. Validates response before returning. | ||
|
@@ -360,7 +397,7 @@ def get_one(self, fields=list()): | |
:MultipleResults: if more than one match is found | ||
:return: Record content | ||
""" | ||
response = self.session.get(self._get_url(self.table), params=self._get_formatted_query(fields)) | ||
response = self.session.get(self._get_url(self.table), params=self._get_formatted_query(fields, limit=1)) | ||
content = self._get_content(response) | ||
l = len(content) | ||
if l > 1: | ||
|
@@ -459,7 +496,7 @@ def _get_content(self, response): | |
:return: ServiceNow response content | ||
""" | ||
method = response.request.method | ||
self.status_code = response.status_code | ||
self.last_response = response | ||
|
||
server_error = { | ||
'summary': None, | ||
|
@@ -549,7 +586,7 @@ def _get_attachment_url(self, sys_id=None): | |
|
||
return "%s/%s" % (url_str, "upload") | ||
|
||
def _get_formatted_query(self, fields): | ||
def _get_formatted_query(self, fields, limit): | ||
""" | ||
Converts the query to a ServiceNow-interpretable format | ||
:return: ServiceNow query | ||
|
@@ -572,6 +609,9 @@ def _get_formatted_query(self, fields): | |
result = {'sysparm_query': sysparm_query} | ||
result.update(self.default_payload) | ||
|
||
if limit: | ||
result.update({'sysparm_limit': limit, 'sysparm_suppress_pagination_header': True}) | ||
|
||
if len(fields) > 0: | ||
if isinstance(fields, list): | ||
result.update({'sysparm_fields': ",".join(fields)}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters