sckott / habanero

client for Crossref search API

Home Page:https://habanero.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Content Negotiation fails with TypeError for non-dois

goerz opened this issue · comments

Content negotiation throws a TypeError if it is called as e.g.

habanero.cn.content_negotiation(query_bibliographic=query, limit=1)

whith some string query. This is due to the definition of CNRequest:

def CNRequest(url, ids = None, format = None, style = None,
        locale = None, **kwargs):

    ...

    if(len(ids) == 1):
        ...

With the above call, ids is passed as None, and the query is in the kwargs. The line that takes the len of ids then fails.

Thanks for the issue @goerz

Hmm. For kwargs with content_negotiation I only support kwargs passed to requests.get, e.g. timeout, proxies - curl modifiers.

Field queries, e.g., query_bibliographic are only supported on the Crossref API, and the Crossref API is only used in content_negotation method when format="citeproc-json"

What's the use case? So I can try to direct you to the appropriate method. I'm not sure what the use case is because field queries are meant to filter a search down further, and CN is meant for getting citations for identifiers (there's no search component in CN)

The use case is "get the most likely BibTeX for a free-form query".

I can easily get around this assuming the "most likely record" has a DOI, by first doing record = Crossref().works(query_bibliographic=query, limit=1)['message']['items'][0] (get the most likely record for a query), extracting the DOI as record['DOI'], and then using the DOI for the content-negotiation (habanero.cn.content_negotiation(ids=DOI)).

Nonetheless, there's a bug in the CNRequest function: the function will always crashes if called with ids=None. This is clearly incompatible ids=None as the default value in the function declaration. As currently implemented, instead of

def CNRequest(url, ids = None, format = None, style = None,
locale = None, **kwargs):
you'd have to define

def CNRequest(url, ids, format = None, style = None,
        locale = None, **kwargs):

and require that ids is either a string, an int, or a list.

Of course, this will push up the problem up to any place where CNRequest is currently called without specifying ids.

Note that

def content_negotiation(ids = None, format = "bibtex", style = 'apa',
locale = "en-US", url = None, **kwargs):
also has incompatible default values. You probably want to check in that function that ids=None and format="bibtex" are not used together (although ids=None may still be OK for other formats, like the citeproc-json).

Thanks. For that use case. works is the best fit here for that use case, using code as you showed.

Agree, there's definitely a bug, and thanks for the details on it. i'll fix

@goerz can you reinstall and try again [sudo] pip [or pip3] install git+git://github.com/sckott/habanero.git#egg=habanero - ids param is now required (no default) and must be a str or a list of str's