Yelp/bravado

Attempting to use as Accela client

Closed this issue · 1 comments

The error in a nutshell: AttributeError: 'dict' object has no attribute 'decode'


Hello! I am attempting to use this library as a client library for an internal project. The samples were great and worked as expected.

I am however having some issues using the below swagger/open api implementation up and running:

https://developer.accela.com/docs/api_reference/api-auth.html#

from bravado.client import SwaggerClient
from bravado.swagger_model import load_file
import os
client = SwaggerClient.from_url('https://developer.accela.com/api/v4/v4-records.json')
# ERROR: requests.exceptions.JSONDecodeError: Invalid control character '\r' at: line 14 column 184 (char 547)
Traceback (most recent call last):
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\requests\models.py", line 960, in json
    return complexjson.loads(self.content.decode(encoding), **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\simplejson\__init__.py", line 514, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\simplejson\decoder.py", line 386, in decode
    obj, end = self.raw_decode(s)
               ^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\simplejson\decoder.py", line 416, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
simplejson.errors.JSONDecodeError: Invalid control character '\r' at: line 14 column 184 (char 547)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\foo\PycharmProjects\accelapy\main.py", line 9, in <module>
    client = SwaggerClient.from_url('https://developer.accela.com/api/v4/api-auth.json')
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\bravado\client.py", line 98, in from_url
    spec_dict = loader.load_spec(spec_url)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\bravado\swagger_model.py", line 111, in load_spec
    return response.json()
           ^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\bravado\requests_client.py", line 160, in json
    return self._delegate.json(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\requests\models.py", line 968, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

requests.exceptions.JSONDecodeError: Invalid control character '\r' at: line 14 column 184 (char 547)

I also tried:

client = SwaggerClient.from_url('https://developer.accela.com/api/v4/v4-records.json', config={'validate_requests': False,'validate_responses': True,})

to see if I could somehow bypass the validation process, but had the same error. I went ahead and removed the offending \rs in the api spec by downloading the spec and removing, and got a second error:

client = SwaggerClient.from_url(load_file(os.getcwd() + '/api-auth.json'))
Traceback (most recent call last):
  File "C:\Users\foo\PycharmProjects\accelapy\main.py", line 12, in <module>
    client = SwaggerClient.from_url(load_file(os.getcwd() + '/api-auth.json'))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\bravado\client.py", line 98, in from_url
    spec_dict = loader.load_spec(spec_url)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\bravado\swagger_model.py", line 101, in load_spec
    response = request(
               ^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\bravado\swagger_model.py", line 70, in request
    if is_file_scheme_uri(url):
       ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\PycharmProjects\accelapy\venv\Lib\site-packages\bravado\swagger_model.py", line 25, in is_file_scheme_uri
    return urlparse.urlparse(url).scheme == u'file'
           ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\AppData\Local\Programs\Python\Python312\Lib\urllib\parse.py", line 394, in urlparse
    url, scheme, _coerce_result = _coerce_args(url, scheme)
                                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\AppData\Local\Programs\Python\Python312\Lib\urllib\parse.py", line 133, in _coerce_args
    return _decode_args(args) + (_encode_result,)
           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\AppData\Local\Programs\Python\Python312\Lib\urllib\parse.py", line 117, in _decode_args
    return tuple(x.decode(encoding, errors) if x else '' for x in args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\foo\AppData\Local\Programs\Python\Python312\Lib\urllib\parse.py", line 117, in <genexpr>
    return tuple(x.decode(encoding, errors) if x else '' for x in args)
                 ^^^^^^^^
AttributeError: 'dict' object has no attribute 'decode'

This one, im not too sure what this means. I validated the json and its valid.

Here is the json that I formatted: api-auth.json

Thanks for making the library!

Interesting, I was able to get this (potentially) working by doing the following:

client = SwaggerClient.from_url('https://raw.githubusercontent.com/michaelachrisco/accelapy/main/api-auth.json')

In other words, something is different between getting the api via the physical file system and the url.