Skip to content

Commit

Permalink
Merge pull request #28 from Ankush-Chander/master
Browse files Browse the repository at this point in the history
add 'Metadata Plus' support.
  • Loading branch information
fabiobatalha authored Jun 15, 2020
2 parents 6885fe0 + 7cffff7 commit 090b62c
Showing 1 changed file with 32 additions and 30 deletions.
62 changes: 32 additions & 30 deletions crossref/restful.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ def do_http_request(self, method, endpoint, data=None, files=None, timeout=100,
action = requests.get

if custom_header:
headers = {'user-agent': custom_header}
headers = custom_header
else:
headers = {'user-agent': str(Etiquette())}

if method == 'post':
result = action(endpoint, data=data, files=files, timeout=timeout, headers=headers)
else:
Expand Down Expand Up @@ -114,9 +113,13 @@ class Endpoint:

CURSOR_AS_ITER_METHOD = False

def __init__(self, request_url=None, request_params=None, context=None, etiquette=None, throttle=True):
def __init__(self, request_url=None, request_params=None, context=None, etiquette=None, throttle=True, crossref_plus_token=None):
self.do_http_request = HTTPRequest(throttle=throttle).do_http_request
self.etiquette = etiquette or Etiquette()
self.custom_header = {'user-agent': str(self.etiquette)}
self.crossref_plus_token = crossref_plus_token
if crossref_plus_token:
self.custom_header["Crossref-Plus-API-Token"] = self.crossref_plus_token
self.request_url = request_url or build_url_endpoint(self.ENDPOINT, context)
self.request_params = request_params or dict()
self.context = context or ''
Expand All @@ -130,7 +133,7 @@ def _rate_limits(self):
'get',
request_url,
only_headers=True,
custom_header=str(self.etiquette),
custom_header=self.custom_header,
throttle=False
)

Expand Down Expand Up @@ -168,7 +171,7 @@ def version(self):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
).json()

return result['message-version']
Expand Down Expand Up @@ -209,7 +212,7 @@ def count(self):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
).json()

return int(result['message']['total-results'])
Expand Down Expand Up @@ -247,7 +250,7 @@ def all(self):
request_url = build_url_endpoint(self.ENDPOINT, context)
request_params = {}

return iter(self.__class__(request_url, request_params, context, self.etiquette))
return iter(self.__class__(request_url, request_params, context, etiquette=self.etiquette, crossref_plus_token=self.crossref_plus_token))

def __iter__(self):
request_url = str(self.request_url)
Expand All @@ -258,7 +261,7 @@ def __iter__(self):
'get',
self.request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand All @@ -280,7 +283,7 @@ def __iter__(self):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand All @@ -304,7 +307,7 @@ def __iter__(self):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -804,7 +807,7 @@ def facet(self, facet_name, facet_count=100):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
).json()

return result['message']['facets']
Expand Down Expand Up @@ -942,17 +945,15 @@ def doi(self, doi, only_message=True):
'/'.join([self.ENDPOINT, doi])
)
request_params = {}

result = self.do_http_request(
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
return

result = result.json()

return result['message'] if only_message is True else result
Expand Down Expand Up @@ -981,7 +982,7 @@ def agency(self, doi, only_message=True):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1022,7 +1023,7 @@ def doi_exists(self, doi):
request_url,
data=request_params,
only_headers=True,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1146,7 +1147,7 @@ def funder(self, funder_id, only_message=True):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1187,7 +1188,7 @@ def funder_exists(self, funder_id):
request_url,
data=request_params,
only_headers=True,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1347,7 +1348,7 @@ def member(self, member_id, only_message=True):
'deposits-orcids-current': False, 'deposits-licenses-backfile': False,
'deposits-affiliations-backfile': True, 'deposits-update-policies-backfile': False,
'deposits-resource-links-current': False, 'deposits-affiliations-current': True},
'names': ['Hogrefe Publishing Group'], 'breakdowns': {'dois-by-issued-year':
'names': ['Hogrefe Publishing Group'], 'breakdowns': {'dois-by-issued-year':
[[2003, 2329], [2004, 2264], [2002, 2211], [2005, 2204], [2006, 2158], [2007, 2121], [2016, 1954],
[2008, 1884], [2015, 1838], [2012, 1827], [2013, 1805], [2014, 1796], [2009, 1760], [2010, 1718],
[2011, 1681], [2001, 1479], [2000, 1477], [1999, 1267], [2017, 767], [1997, 164], [1996, 140],
Expand All @@ -1365,7 +1366,7 @@ def member(self, member_id, only_message=True):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1406,7 +1407,7 @@ def member_exists(self, member_id):
request_url,
data=request_params,
only_headers=True,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1452,7 +1453,7 @@ def type(self, type_id, only_message=True):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1488,7 +1489,7 @@ def all(self):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1530,7 +1531,7 @@ def type_exists(self, type_id):
request_url,
data=request_params,
only_headers=True,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1580,7 +1581,7 @@ def prefix(self, prefix_id, only_message=True):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1664,7 +1665,7 @@ def journal(self, issn, only_message=True):
'get',
request_url,
data=request_params,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand Down Expand Up @@ -1706,7 +1707,7 @@ def journal_exists(self, issn):
request_url,
data=request_params,
only_headers=True,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

if result.status_code == 404:
Expand All @@ -1733,6 +1734,7 @@ def __init__(self, prefix, api_user, api_key, etiquette=None,
use_test_server=False):
self.do_http_request = HTTPRequest(throttle=False).do_http_request
self.etiquette = etiquette or Etiquette()
self.custom_header = {'user-agent': str(self.etiquette)}
self.prefix = prefix
self.api_user = api_user
self.api_key = api_key
Expand Down Expand Up @@ -1772,7 +1774,7 @@ def register_doi(self, submission_id, request_xml):
data=params,
files=files,
timeout=10,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

return result
Expand Down Expand Up @@ -1802,7 +1804,7 @@ def request_doi_status_by_filename(self, file_name, data_type='result'):
endpoint,
data=params,
timeout=10,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

return result
Expand Down Expand Up @@ -1832,7 +1834,7 @@ def request_doi_status_by_batch_id(self, doi_batch_id, data_type='result'):
endpoint,
data=params,
timeout=10,
custom_header=str(self.etiquette)
custom_header=self.custom_header
)

return result

0 comments on commit 090b62c

Please sign in to comment.