Skip to content

Commit

Permalink
Basic calls to api working
Browse files Browse the repository at this point in the history
  • Loading branch information
nmohoric committed Apr 11, 2013
1 parent e3dac97 commit 03bc784
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions nyplcollections.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,45 @@

import requests

class NYPLsearch:
class NYPLsearch():
def __init__(self,token=None):
self.token = token or ""

try:
assert self.token
except:
AssertionError:
print "You will need to use your Authenticaton Token.\n
This can be retrieved at http://api.repo.nypl.org/sign_up"
except AssertionError:
print "You will need to use your Authenticaton Token.\n This can be retrieved at http://api.repo.nypl.org/sign_up"

self.base = "http://api.repo.nypl.org/api/v1/items"

def return_captures(self, uuid, withTitle=False, format='json'):
# Return the captures for a given uuid
# optional value withTitles=yes

def return_uuid(self, type, val, format='json'):
# Return the item-uuid for a identifier.
# Return the captures for a given uuid
# optional value withTitles=yes
def captures(self, uuid, withTitles=False, format='json'):
return self._get( '/'.join([self.base, uuid]) + '.' + format,
{ 'withTitles' : 'yes' if withTitles else 'no' } )

def return_mods(self, uuid, format='json'):
# Return a mods record for a given uuid

def search_mods(self, q, field=None, format='json'):
# Search across all (without field) or in specific field
# (valid fields at http://www.loc.gov/standards/mods/mods-outline.html)
# Return the item-uuid for a identifier.
def uuid(self, type, val, format='json'):
return self._get( '/'.join([self.base, type, val]) + '.' + format )


# Search across all (without field) or in specific field
# (valid fields at http://www.loc.gov/standards/mods/mods-outline.html)
def search(self, q, field=None, format='json'):
params = { 'q' : q }
if field:
params['field'] = field

return self._get( '/'.join([self.base, 'search']) + '.' + format, params )

# Return a mods record for a given uuid
def mods(self, uuid, format='json'):
return self._get( '/'.join([self.base, 'mods', uuid]) + '.' + format )


def _get(self, url, params=None):
headers = { "Authorization" : "Token token=" + self.token }
return requests.get(url, params=params, headers=headers)

0 comments on commit 03bc784

Please sign in to comment.