opendoor-labs / rets

RETS Python 3 Client
MIT License
89 stars 43 forks source link

metadata returns empty tuple, get_resource returns error #34

Closed Valachio closed 5 years ago

Valachio commented 5 years ago

After logging in, when I try to get metadata, I get an empty tuple. When I try to use get_resource, I get the error xml.etree.ElementTree.ParseError: no element found: line 1, column 0.

It seems that no data is accessible to me after logging in. Any tips on why this is happening? Thanks in advance.

client = RetsClient(
login_url=login_url,
username=username,
password=password,
rets_version='1.7',
)

print(client.metadata) # This returns empty tuple

resource = client.get_resource('Property') # This returns the following error: xml.etree.ElementTree.ParseError: no element found: line 1, column 0
martinxsliu commented 5 years ago

That sounds like your RETS server might not be returning valid XML. Could you use a debugger to see what the response from the server is?

Using ipython:

client = ...
resource = client.get_resource('Property') # Exception is raised

# ipython magic function to drop you into the stack trace of the exception
%debug

# up and down commands take you up and down a frame. navigate to the parse_xml function
up

# See what the response body and headers are
response.content
response.headers
Valachio commented 5 years ago

@martinxsliu Hey Martin. Just ran the operations you recommended.

For response.content, the response was simply b''

For response.headers, the response was {'RETS-Version': 'RETS/1.7', 'cache-control': 'private', 'Content-Type': 'text/xml;charset=utf-8', 'MIME-Version': '1.0', 'Date': 'Sun, 28 Oct 2018 04:02:48 GMT', 'RETS-Server': 'StratusRETS/1.7', 'Server': 'StratusRETS/1.7', 'Content-Length': '0'}

I am working with RETS feed provided by TREB (Toronto Real Estate Board)


P.S. The full traceback when I ran resource = client.get_resource('Property') iPython is as follows:

Traceback (most recent call last):

  File "/home/valachio/bungol/bungolenv/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 3267, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-3-b98bc56e900e>", line 1, in <module>
    resource = client.get_resource('Property')

  File "/home/valachio/bungol/bungolenv/lib/python3.5/site-packages/rets/client/client.py", line 68, in get_resource
    for resource in self.resources:

  File "/home/valachio/bungol/bungolenv/lib/python3.5/site-packages/rets/client/client.py", line 64, in resources
    self._resources = self._fetch_resources()

  File "/home/valachio/bungol/bungolenv/lib/python3.5/site-packages/rets/client/client.py", line 74, in _fetch_resources
    metadata = get_metadata_data(self.http, 'resource')

  File "/home/valachio/bungol/bungolenv/lib/python3.5/site-packages/rets/client/utils.py", line 5, in get_metadata_data
    metadata_structs = http_client.get_metadata(type_, **kwargs)

  File "/home/valachio/bungol/bungolenv/lib/python3.5/site-packages/rets/http/client.py", line 119, in get_metadata
    return parse_metadata(self._get_metadata(type_, id_))

  File "/home/valachio/bungol/bungolenv/lib/python3.5/site-packages/rets/http/parsers/parse.py", line 76, in parse_metadata
    elem = parse_xml(response)

  File "/home/valachio/bungol/bungolenv/lib/python3.5/site-packages/rets/http/parsers/parse.py", line 18, in parse_xml
    root = XML(response.content)

  File "/usr/lib/python3.5/xml/etree/ElementTree.py", line 1334, in XML
    return parser.close()

  File "<string>", line unknown
ParseError: no element found: line 1, column 0
martinxsliu commented 5 years ago

So the server isn't returning anything in its response, which is not what's expected according to the RETS standard. Unfortunately, a lot of these issues come down to small variations in server implementations.

You could try using the RetsHttpClient instead:

from rets.http import RetsHttpClient

client = RetsHttpClient(
    login_url='http://my.rets.server/rets/login',
    username='username',
    password='password',
    # Alternatively authenticate using user agent password
    # user_agent='rets-python/0.3',
    # user_agent_password=''
)
client.login()

# This line should fail with the same exception!
client.get_metadata('resource')

# See if this works?
client.get_metadata('class', resource='Property')

Does your rets provider have any technical guidance? Does using another tool (e.g. https://github.com/summera/retscli) work?

Valachio commented 5 years ago

@martinxsliu I tried using RetsHttpClient. I can log in but got the same error (xml.etree.ElementTree.ParseError: no element found: line 1, column 0) when I did client.get_metadata('class', resource='Property').

I did however manage to get things working with librets w/ Python bindings. So I'll be sticking with that until any further issues come up. I appreciate you taking the time to help.

wimby commented 5 years ago

Hey, I got similar issue with empty metadata, but the server returned code 20513: Miscellaneous Error. I find out that RetsHttpClient sends id and type lowercased in _get_metadata method (which according to this: https://www.nar.realtor/retsorg.nsf/retsproto1.7d6.pdf section 11.1.2, seems to be wrong). With payload patched to ID and Type instead it worked like a charm. cc @Valachio

martinxsliu commented 5 years ago

Closed by https://github.com/opendoor-labs/rets/pull/45