Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
Call api until it returns messages successfully.
:param params: all the parameters needed by an api
:return: businesses in returned documents
"""
while True:
try:
resp = self.__session.get(self.__searchURL, params=params)
re = resp.json()
if 'result' in re:
result = re['result']
return result
else:
return []
except simplejson.scanner.JSONDecodeError:
print >> perr, 'JSONDecodeError error!!!'
sleep(self.__delay)
continue
except requests.ConnectionError:
print >> perr, 'ConnectionError error!!!'
sleep(self.__delay)
continue
code = course['CourseCode']
try:
object = Section.objects.get(code=code)
object.perms = course['PermCount']
if object.perms: int(object.perms)
object.spots = int(course['SeatsTotal'])
object.filled = int(course['SeatsFilled'])
object.save()
self.stdout.write('enrollments for section "%s" refreshed\n' % object.code)
except Section.DoesNotExist:
self.stdout.write('skipping unknown section "%s"\n' % code)
except simplejson.scanner.JSONDecodeError:
self.stdout.write('error accessing "%s"' % (ENROLLMENTS_URL % t))
def decode(self, s, _w=WHITESPACE.match, _PY3=PY3):
"""Return the Python representation of ``s`` (a ``str`` or ``unicode``
instance containing a JSON document)
"""
if _PY3 and isinstance(s, bytes):
s = str(s, self.encoding)
obj, end = self.raw_decode(s)
end = _w(s, end).end()
if end != len(s):
raise JSONDecodeError("Extra data", s, end, len(s))
return obj
# we support not specifying a token to just output POSTs in the logger
if self.token is None:
return
header = {'Authorization': 'token ' + self.token}
url = "{api}/repos/{repo}/{endpoint}".format(api=GITHUB_API_URL,
repo=self.repo,
endpoint=endpoint)
try:
resp = requests.post(url, json=data, headers=header)
# the GitHub API servers are flaky sometimes; check if we got valid
# JSON and if not, let's try again just once
resp.json()
except JSONDecodeError:
logger.warning("expected JSON, but received: %s", resp.content)
logger.warning("retrying...")
resp = requests.post(url, json=data, headers=header)
# pylint: disable=no-member
if resp.status_code != requests.codes.created:
raise GitHubApiError(url, resp, "POST didn't return HTTP 201")
def _result_processor(self, result):
# Save last full response
self.response = result
result_json = None
try:
result_json = result.json()
except simplejson.scanner.JSONDecodeError:
result_text = result.text.strip()
# HTTP methods other than GET and OPTIONS are allowed to return empty result
if result.request.method in ("GET", "OPTIONS") or result_text:
raise APIException("JSONDecodeError: {}".format(result_text or "empty result"))
if result_json and "error" in result_json:
if isinstance(result_json["error"], dict):
raise APIException(
"{}: {}".format(result_json["error"]["klass"], result_json["error"]["message"]))
else:
raise APIException(
"{}: {}".format(result_json.get("status", None), result_json["error"]))
# Check HTTP response status
if not result:
raise APIException("The request failed with HTTP status {}: {}".format(
def callAPI(self, params):
"""
Call api until it returns messages successfully.
:param params: all the parameters needed by an api
:return: businesses in returned documents
"""
while True:
try:
resp = self.__session.get(self.__searchURL, params=params)
re = resp.json()
if 'hit' in re['result']['hits']:
return re['result']['hits']['hit']
else:
return []
except simplejson.scanner.JSONDecodeError:
print >> perr, 'JSONDecodeError error!!!'
sleep(self.__delay)
continue
except requests.ConnectionError:
print >> perr, 'ConnectionError error!!!'
sleep(self.__delay)
continue
def _decode_json(self, url, response):
if response.text:
if response.status_code in [ReturnCodes.NO_CONTENT]:
cts_error(
"{method} {url:id} Non-empty response despite NO_CONTENT {code} return "
"code",
method=response.request.method, url=url,
code=ReturnCodes.NO_CONTENT)
try:
response_body = json.loads(response.text, object_pairs_hook=OrderedDict)
except (JSONDecodeError, ValueError) as err:
method = response.request.method if response.request else ''
cts_error("{method} {url:id} Unable to parse. Error {err:exception}", method=method, url=url, err=err)
return None
else:
if response.status_code in [ReturnCodes.NO_CONTENT, ReturnCodes.CREATED,
ReturnCodes.ACCEPTED]:
response_body = dict()
else:
cts_error("{url:id} Unexpected empty response", url=url)
response_body = None
if not self._is_controlled_by_framework:
self._log_response(response, response_body)
return response_body
response = self._client.client.post(url, data=post)
else:
response = self._client.client.get(url)
# Now, if a location tag was returned to us, follow it and get the
# newly returned response data
loc = response.headers.get('location', None)
if loc:
response = self._client.client.get(loc)
url = loc
# Try to parse the JSON body. If no body was returned, this fails and
# thats OK sometimes.
try:
soul = response.json()
except simplejson.scanner.JSONDecodeError:
log.debug('No JSON found. Returning the raw text')
return response.raw_response.text
# Now dig deep into the python rightscale library itself and create our
# own Resource object by hand.
resource = rightscale.rightscale.Resource(
path=url,
response=response,
soul=soul,
client=self._client.client)
return resource
def _msg(response):
"""Get response error message."""
try:
return response.json().get('message')
except simplejson.scanner.JSONDecodeError:
return response.text
except Exception: # pylint: disable=W0703
return 'Unexpected error.'