Generalizes http request for json data in connectors

This commit is contained in:
Mouse Reeve 2020-05-09 12:39:58 -07:00
parent 3a8d84e9b1
commit 5924e8ed63
3 changed files with 25 additions and 46 deletions

View file

@ -206,6 +206,20 @@ def get_date(date_string):
return None return None
def get_data(url):
''' wrapper for request.get '''
resp = requests.get(
url,
headers={
'Accept': 'application/activity+json; charset=utf-8',
},
)
if not resp.ok:
resp.raise_for_status()
data = response.json()
return data
class SearchResult: class SearchResult:
''' standardized search result object ''' ''' standardized search result object '''
def __init__(self, title, key, author, year): def __init__(self, title, key, author, year):

View file

@ -7,7 +7,7 @@ from django.core.files.base import ContentFile
from django.db import transaction from django.db import transaction
from fedireads import models from fedireads import models
from .abstract_connector import AbstractConnector, SearchResult, get_date from .abstract_connector import AbstractConnector, SearchResult, get_date, get_data
from .abstract_connector import match_from_mappings, update_from_mappings from .abstract_connector import match_from_mappings, update_from_mappings
@ -45,15 +45,7 @@ class Connector(AbstractConnector):
return book return book
# no book was found, so we start creating a new one # no book was found, so we start creating a new one
response = requests.get( data = get_data(remote_id)
remote_id,
headers={
'Accept': 'application/activity+json; charset=utf-8',
},
)
if not response.ok:
response.raise_for_status()
data = response.json()
if data['book_type'] == 'work': if data['book_type'] == 'work':
work_data = data work_data = data
@ -99,16 +91,7 @@ class Connector(AbstractConnector):
def update_book(self, book, data=None): def update_book(self, book, data=None):
''' add remote data to a local book ''' ''' add remote data to a local book '''
if not data: if not data:
response = requests.get( data = get_data(book.remote_id)
book.remote_id,
headers={
'Accept': 'application/activity+json; charset=utf-8',
},
)
if not response.ok:
response.raise_for_status()
data = response.json()
match = match_from_mappings(data, {}) match = match_from_mappings(data, {})
if match: if match:
@ -150,16 +133,7 @@ class Connector(AbstractConnector):
except ObjectDoesNotExist: except ObjectDoesNotExist:
pass pass
resp = requests.get( data = get_data(remote_id)
remote_id,
headers={
'Accept': 'application/activity+json; charset=utf-8',
},
)
if not resp.ok:
resp.raise_for_status()
data = resp.json()
# ingest a new author # ingest a new author
author = models.Author(remote_id=remote_id) author = models.Author(remote_id=remote_id)

View file

@ -8,7 +8,7 @@ from django.db import transaction
from fedireads import models from fedireads import models
from .abstract_connector import AbstractConnector, SearchResult from .abstract_connector import AbstractConnector, SearchResult
from .abstract_connector import update_from_mappings from .abstract_connector import update_from_mappings
from .abstract_connector import get_date from .abstract_connector import get_date, get_data
from .openlibrary_languages import languages from .openlibrary_languages import languages
@ -136,21 +136,14 @@ class Connector(AbstractConnector):
def load_book_data(self, olkey): def load_book_data(self, olkey):
''' query openlibrary for data on a book ''' ''' query openlibrary for data on a book '''
response = requests.get('%s/works/%s.json' % (self.books_url, olkey)) url = '%s/works/%s.json' % (self.books_url, olkey)
if not response.ok: return get_data(url)
response.raise_for_status()
data = response.json()
return data
def load_edition_data(self, olkey): def load_edition_data(self, olkey):
''' query openlibrary for editions of a work ''' ''' query openlibrary for editions of a work '''
response = requests.get( url = '%s/works/%s/editions.json' % (self.books_url, olkey)
'%s/works/%s/editions.json' % (self.books_url, olkey)) return get_data(url)
if not response.ok:
response.raise_for_status()
data = response.json()
return data
def expand_book_data(self, book): def expand_book_data(self, book):
@ -179,11 +172,9 @@ class Connector(AbstractConnector):
except models.Author.DoesNotExist: except models.Author.DoesNotExist:
pass pass
response = requests.get('%s/authors/%s.json' % (self.base_url, olkey)) url = '%s/authors/%s.json' % (self.base_url, olkey)
if not response.ok: data = get_data(url)
response.raise_for_status()
data = response.json()
author = models.Author(openlibrary_key=olkey) author = models.Author(openlibrary_key=olkey)
mappings = { mappings = {
'birth_date': ('born', get_date), 'birth_date': ('born', get_date),