Skip to content

Commit

Permalink
Merge pull request #7 from freud-digital/5-refactor-fetch_entitiy
Browse files Browse the repository at this point in the history
5 refactor fetch entitiy
  • Loading branch information
csae8092 authored Aug 4, 2022
2 parents e95821b + ec3c2e4 commit 611c015
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ max-line-length = 120
exclude =
.git,
build,
env
env
play.py
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[![flake8 Lint](https://github.com/freud-digital/frd-autocomplete/actions/workflows/lint.yml/badge.svg)](https://github.com/freud-digital/frd-autocomplete/actions/workflows/lint.yml)
[![Test](https://github.com/freud-digital/frd-autocomplete/actions/workflows/test.yml/badge.svg)](https://github.com/freud-digital/frd-autocomplete/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/freud-digital/frd-autocomplete/branch/main/graph/badge.svg?token=miLq3rRrcq)](https://codecov.io/gh/freud-digital/frd-autocomplete)

# frd-autocomplete

A fastapi proxy to query several different APIs and unify their response through a single API interface
Expand Down
32 changes: 32 additions & 0 deletions app/fixtures/baserow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 21,
"order": "21.00000000000000000000",
"name": "Freud, Sigmund",
"frd_id": "frd_person_21",
"gnd_id": "https://d-nb.info/gnd/118535315",
"birth_year": "1856",
"place_of_birth": [
{
"id": 1,
"value": "Příbor"
}
],
"death_year": "1939",
"place_of_death": [
{
"id": 2,
"value": "London"
}
],
"bio": "",
"profession": [],
"pmb": "",
"labels": []
}
]
}
100 changes: 100 additions & 0 deletions app/fixtures/zotero.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
[
{
"key": "V9N337QB",
"version": 50,
"library": {
"type": "group",
"id": 4690432,
"name": "sigmund-freud",
"links": {
"alternate": {
"href": "https://www.zotero.org/groups/sigmund-freud",
"type": "text/html"
}
}
},
"links": {
"self": {
"href": "https://api.zotero.org/groups/4690432/items/V9N337QB",
"type": "application/json"
},
"alternate": {
"href": "https://www.zotero.org/groups/sigmund-freud/items/V9N337QB",
"type": "text/html"
}
},
"meta": {
"createdByUser": {
"id": 9548878,
"username": "ch2103",
"name": "",
"links": {
"alternate": {
"href": "https://www.zotero.org/ch2103",
"type": "text/html"
}
}
},
"creatorSummary": "Breuer and Freud",
"parsedDate": "1895",
"numChildren": 0
},
"data": {
"key": "V9N337QB",
"version": 50,
"itemType": "book",
"title": "Studien über Hysterie",
"creators": [
{
"creatorType": "author",
"firstName": "Josef",
"lastName": "Breuer"
},
{
"creatorType": "author",
"firstName": "Sigmund",
"lastName": "Freud"
}
],
"abstractNote": "",
"series": "",
"seriesNumber": "",
"volume": "",
"numberOfVolumes": "",
"edition": "",
"place": "Leipzig Wien",
"publisher": "Deuticke",
"date": "1895",
"numPages": "269",
"language": "ger",
"ISBN": "",
"shortTitle": "",
"url": "http://phaidra.univie.ac.at/o:350215",
"accessDate": "2022-07-19T11:05:41Z",
"archive": "",
"archiveLocation": "",
"libraryCatalog": "usearch.univie.ac.at",
"callNumber": "I-176203",
"rights": "",
"extra": "",
"tags": [
{
"tag": "Fallstudiensammlung",
"type": 1
},
{
"tag": "Hysterie",
"type": 1
},
{
"tag": "Psychoanalyse",
"type": 1
}
],
"collections": [],
"relations": {},
"dateAdded": "2022-07-19T11:06:44Z",
"dateModified": "2022-07-19T11:06:44Z"
}
}
]
76 changes: 7 additions & 69 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
MINIMAL_CHARS,
MINIMAL_CHARS_ERROR
)
from app.utils import populate_baserow_response, populate_zotero_response


app = FastAPI()

URL = "{}{}/?user_field_names=true"
Expand Down Expand Up @@ -48,45 +51,11 @@ async def fetch_entitiy(
else:
if entity_type == 'bibl':
url = f"{ZOTERO_API}?q={q}"
print(url)
r = requests.get(url)
data = r.json()

if format == 'teicompleter':
result = {
"tc:suggestion": []
}
for x in data:
item_data = x['data']
item_title = item_data.get('title', 'no title provided')
item_place = item_data.get('place', 'no place provided')
item_date = item_data.get('date', 'no date provided')
item = {
"tc:value": f"#frd_bibl_{x['key']}",
"tc:description": f"{item_title}, {item_place}, {item_date}"
}
result['tc:suggestion'].append(item)

return result

elif format == 'select2':
result = {
"results": [],
}
for x in data['data']:
item_data = x['data']
item_title = item_data.get('title', 'no title provided')
item_place = item_data.get('place', 'no place provided')
item_date = item_data.get('date', 'no date provided')
item = {
"id": f"#frd_bibl_{x['key']}",
"text": f"{item_title}, {item_place}, {item_date}"
}
result['results'].append(item)

return result
elif format == 'original':
return data
result = populate_zotero_response(data, format=format)
return result
else:
table_id = BASEROW_TABLE_MAPPING[entity_type]['table_id']
query_field = BASEROW_TABLE_MAPPING[entity_type]['ac_query_field_id']
Expand All @@ -104,36 +73,5 @@ async def fetch_entitiy(
}
)
data = r.json()

if format == 'teicompleter':
result = {
"tc:suggestion": []
}
for x in data['results']:
item = {
"tc:value": f"#{x['frd_id']}",
"tc:description": x['name']
}
result['tc:suggestion'].append(item)

return result

elif format == 'select2':
result = {
"results": [],
}
for x in data['results']:
item = {
"id": f"#{x['frd_id']}",
"text": x['name']
}
result['results'].append(item)

return result

elif format == 'original':

return {
"table_id": url,
"data": data
}
result = populate_baserow_response(data, format=format)
return result
52 changes: 52 additions & 0 deletions app/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import unittest
import json
import os

from .utils import zotero_description, populate_baserow_response, populate_zotero_response

file_path = os.path.dirname(os.path.realpath(__file__))
fixtures_path = os.path.join(file_path, 'fixtures')

with open(os.path.join(fixtures_path, "zotero.json")) as f:
zotero_data = json.load(f)

with open(os.path.join(fixtures_path, "baserow.json")) as f:
baserow_data = json.load(f)


class TestUtilityFunctions(unittest.TestCase):

def test_zotero_description(self):
item_data = zotero_data[0]['data']
data = zotero_description(item_data)
print(data)
self.assertEqual(data['id'], '#frd_bibl_V9N337QB')
self.assertTrue(data['value'].startswith('Studien über Hysterie'))

def test_populate_zotero_response(self):
data = populate_zotero_response(zotero_data, "select2")
keys = data['results'][0].keys()
self.assertIn('id', keys)
data = populate_zotero_response(zotero_data)
keys = data['tc:suggestion'][0].keys()
self.assertIn('tc:description', keys)
data = populate_zotero_response(zotero_data, format='original')
keys = data['result'][0].keys()
self.assertIn('key', keys)

def test_baserow_population_select2(self):
data = populate_baserow_response(baserow_data, format="select2")
keys = data['results'][0].keys()
self.assertIn('id', keys)

def test_baserow_population_teicompleter(self):
data = populate_baserow_response(baserow_data)
keys = data['tc:suggestion'][0].keys()
self.assertIn('tc:description', keys)
self.assertTrue('tc:description', 'Freud, Sigmund')

def test_baserow_population_original(self):
data = populate_baserow_response(baserow_data, format='original')
keys = data['results']['results'][0].keys()
self.assertIn('frd_id', keys)
self.assertTrue(data['results']['results'][0]['name'], 'Freud, Sigmund')
76 changes: 76 additions & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
def zotero_description(item_data: dict) -> dict:
item_title = item_data.get('title', 'no title provided')
item_place = item_data.get('place', 'no place provided')
item_date = item_data.get('date', 'no date provided')
item_id = f"#frd_bibl_{item_data['key']}"
item_description = f"{item_title}, {item_place}, {item_date}"
return {
"id": item_id,
"value": item_description
}


def populate_zotero_response(data: list, format: str = "teicompleter") -> dict:
if format == 'select2':
result = {
"results": [],
}
for x in data:
item_data = zotero_description(x['data'])
item = {
"id": item_data['id'],
"text": item_data['value']
}
result['results'].append(item)
return result

elif format == 'original':
return {
"result": data
}
else:
result = {
"tc:suggestion": []
}
for x in data:
item_data = zotero_description(x['data'])
item = {
"tc:value": item_data['id'],
"tc:description": item_data['value']
}
result['tc:suggestion'].append(item)
return result


def populate_baserow_response(data: dict, format: str = "teicompleter") -> dict:
if format == 'select2':
result = {
"results": [],
}
for x in data['results']:
item = {
"id": f"#{x['frd_id']}",
"text": x['name']
}
result['results'].append(item)

return result

elif format == 'original':

return {
"results": data
}

else:
result = {
"tc:suggestion": []
}
for x in data['results']:
item = {
"tc:value": f"#{x['frd_id']}",
"tc:description": x['name']
}
result['tc:suggestion'].append(item)

return result
6 changes: 6 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#/bin/bash

coverage run -m pytest -v
coverage report
coverage html
xdg-open ./htmlcov/index.html

0 comments on commit 611c015

Please sign in to comment.