# API Wrapper
## Install package
* from PyPi:
pip install apimapper
* from source:
pip install -e .
## Unit Testing
>pytest
## Usage
* See https://github.com/acdh-oeaw/apimapper/blob/develop/demo/APIMapper%20Demo.ipynb
* Multiple APIs (GND and VIAF) mapped to a common JSON schema
VIAF only returns VIAF ID - which is contructed into a url using a "rule"
```
from apimapper import APIMapper
from apimapper import config
GND_PERSON_MAP = {config.DIRECT: {'uri': 'id',
'label': 'label'}}
VIAF_PERSON_MAP = {config.RESULT: 'result',
config.FILTER: {'nametype': 'personal'},
config.DIRECT: {'label': 'displayForm'},
config.RULES: {'uri': {config.RULE: '"http://www.viaf.org/viaf/{p1}"',
config.FIELDS: {'p1': 'viafid'}}}}
GND_PERSON_SOURCE = {config.URL: 'https://lobid.org/gnd/search',
config.QUERY_FIELD: 'q',
config.PAYLOAD: {'format':'json:suggest',
'filter': 'type:Person'}}
VIAF_PERSON_SOURCE = {config.URL: 'http://www.viaf.org/viaf/AutoSuggest',
config.QUERY_FIELD: 'query'}
gnd = APIMapper(GND_PERSON_SOURCE, GND_PERSON_MAP)
viaf = APIMapper(VIAF_PERSON_SOURCE, VIAF_PERSON_MAP)
apis = [gnd, viaf]
results = []
for api in apis:
res = api.fetch_results('Pratchett')
results.extend(res)
print(results)
```
* Using mapping rules
Splitting the GND label field into meaningful subparts
```
from apimapper import APIMapper
from apimapper import config
GND_PERSON_SOURCE = {config.URL: 'https://lobid.org/gnd/search',
config.QUERY_FIELD: 'q',
config.PAYLOAD: {'format':'json:suggest',
'filter': 'type:Person'}}
GND_PERSON_MAP = {config.DIRECT: {'label': 'label',
'uri': 'id'},
config.RULES: {'source': {config.RULE: '"GND"'},
'label_name': {config.RULE: '"{p1}".split("|")[0].strip()',
config.FIELDS: {'p1': 'label'}},
'label_year': {config.RULE: '"{p1}".split("|")[1].strip() if "{p1}".split("|")[1].strip()[0].isnumeric() else ""',
config.FIELDS: {'p1': 'label'}},
'label_profession': {config.RULE: '"{p1}".split("|")[2].strip() if "{p1}".split("|")[1].strip()[0].isnumeric() else "{p1}".split("|")[1].strip()',
config.FIELDS: {'p1': 'label'}}}}
api = APIMapper(GND_PERSON_SOURCE, GND_PERSON_MAP)
res = api.fetch_results('Rosalind Franklin')
```
* More example usage in apimapper/demo
Raw data
{
"_id": null,
"home_page": "https://github.com/acdh-oeaw/apimapper",
"name": "apimapper",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "API Mapper",
"author": "Saranya Balasubramanian, Ksenia Zaytseva",
"author_email": "saranya.balasubramanian@oeaw.ac.at",
"download_url": "https://files.pythonhosted.org/packages/68/c6/eab4525895270166631c93dcf55687b5bd0b37e5f83be5b2445fd2488628/apimapper-0.7.9.tar.gz",
"platform": null,
"description": "# API Wrapper\n\n## Install package\n* from PyPi:\n pip install apimapper\n* from source:\n pip install -e .\n\n## Unit Testing\n >pytest\n\n## Usage\n\n* See https://github.com/acdh-oeaw/apimapper/blob/develop/demo/APIMapper%20Demo.ipynb\n* Multiple APIs (GND and VIAF) mapped to a common JSON schema\n\nVIAF only returns VIAF ID - which is contructed into a url using a \"rule\"\n```\nfrom apimapper import APIMapper\nfrom apimapper import config\n\nGND_PERSON_MAP = {config.DIRECT: {'uri': 'id',\n 'label': 'label'}}\n\nVIAF_PERSON_MAP = {config.RESULT: 'result',\n config.FILTER: {'nametype': 'personal'},\n config.DIRECT: {'label': 'displayForm'},\n config.RULES: {'uri': {config.RULE: '\"http://www.viaf.org/viaf/{p1}\"',\n config.FIELDS: {'p1': 'viafid'}}}}\n\nGND_PERSON_SOURCE = {config.URL: 'https://lobid.org/gnd/search',\n config.QUERY_FIELD: 'q',\n config.PAYLOAD: {'format':'json:suggest',\n 'filter': 'type:Person'}}\n\nVIAF_PERSON_SOURCE = {config.URL: 'http://www.viaf.org/viaf/AutoSuggest',\n config.QUERY_FIELD: 'query'}\n\n\ngnd = APIMapper(GND_PERSON_SOURCE, GND_PERSON_MAP)\nviaf = APIMapper(VIAF_PERSON_SOURCE, VIAF_PERSON_MAP)\napis = [gnd, viaf]\nresults = []\nfor api in apis:\n res = api.fetch_results('Pratchett')\n results.extend(res)\n\nprint(results)\n```\n\n* Using mapping rules\n\nSplitting the GND label field into meaningful subparts\n```\nfrom apimapper import APIMapper\nfrom apimapper import config\n\nGND_PERSON_SOURCE = {config.URL: 'https://lobid.org/gnd/search',\n config.QUERY_FIELD: 'q',\n config.PAYLOAD: {'format':'json:suggest',\n 'filter': 'type:Person'}}\n\nGND_PERSON_MAP = {config.DIRECT: {'label': 'label',\n 'uri': 'id'},\n config.RULES: {'source': {config.RULE: '\"GND\"'},\n 'label_name': {config.RULE: '\"{p1}\".split(\"|\")[0].strip()',\n config.FIELDS: {'p1': 'label'}},\n 'label_year': {config.RULE: '\"{p1}\".split(\"|\")[1].strip() if \"{p1}\".split(\"|\")[1].strip()[0].isnumeric() else \"\"',\n config.FIELDS: {'p1': 'label'}},\n 'label_profession': {config.RULE: '\"{p1}\".split(\"|\")[2].strip() if \"{p1}\".split(\"|\")[1].strip()[0].isnumeric() else \"{p1}\".split(\"|\")[1].strip()',\n config.FIELDS: {'p1': 'label'}}}}\n\napi = APIMapper(GND_PERSON_SOURCE, GND_PERSON_MAP)\nres = api.fetch_results('Rosalind Franklin')\n```\n\n* More example usage in apimapper/demo\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "API Mapper",
"version": "0.7.9",
"split_keywords": [
"api",
"mapper"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "6c8753017e236081c5b986e35ef08a5c54542f6251a66c6e33074a94baca0d30",
"md5": "d725500ea1aebf190111718224759c52",
"sha256": "6bb4d07c92697763cac4d1dfd279b4e11d94fc2f35e40f417ea42be638028ac5"
},
"downloads": -1,
"filename": "apimapper-0.7.9-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "d725500ea1aebf190111718224759c52",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 8183,
"upload_time": "2023-04-20T11:53:49",
"upload_time_iso_8601": "2023-04-20T11:53:49.752415Z",
"url": "https://files.pythonhosted.org/packages/6c/87/53017e236081c5b986e35ef08a5c54542f6251a66c6e33074a94baca0d30/apimapper-0.7.9-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "68c6eab4525895270166631c93dcf55687b5bd0b37e5f83be5b2445fd2488628",
"md5": "f4d84b613b8f8ae64626a67ee59e1255",
"sha256": "7aedb0c9c8b9f61978b15334fbb284db7fa15e5a4b8bf36dfe2988a5c9b82e40"
},
"downloads": -1,
"filename": "apimapper-0.7.9.tar.gz",
"has_sig": false,
"md5_digest": "f4d84b613b8f8ae64626a67ee59e1255",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7325,
"upload_time": "2023-04-20T11:53:51",
"upload_time_iso_8601": "2023-04-20T11:53:51.855567Z",
"url": "https://files.pythonhosted.org/packages/68/c6/eab4525895270166631c93dcf55687b5bd0b37e5f83be5b2445fd2488628/apimapper-0.7.9.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-20 11:53:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "acdh-oeaw",
"github_project": "apimapper",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "apimapper"
}