API Mapper
Project description
# API Wrapper
## Install package
* from PyPi:
pip install apimapper
* from source:
pip install -e .
## Unit Testing
tests> python -m pytest
## Usage
* 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
## Install package
* from PyPi:
pip install apimapper
* from source:
pip install -e .
## Unit Testing
tests> python -m pytest
## Usage
* 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
apimapper-0.6.tar.gz
(5.0 kB
view details)
Built Distribution
File details
Details for the file apimapper-0.6.tar.gz
.
File metadata
- Download URL: apimapper-0.6.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12569976cd0845e0c5693ec1c60047c5fa363b90e9807ddae4d3aaaecafc2e9a |
|
MD5 | 41fa17a1c48f5f42298db3e1e40e327e |
|
BLAKE2b-256 | cb0e98d72c62c5715abf3bfed3d490472ff848b6788ce87be176f2b20b0334a3 |
File details
Details for the file apimapper-0.6-py3-none-any.whl
.
File metadata
- Download URL: apimapper-0.6-py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e10d64922c53c4de499c50fb49ce60ec4f2a06babd0c6bc0a550403807279fa0 |
|
MD5 | 8876bbb32cb21b91453847da81f21d71 |
|
BLAKE2b-256 | 8ad71207fc9c4239d086823c30020e632752c8118020c5499c013157dd96d14c |