Skip to main content

Python package to connect to a FDP API

Project description

fdpAPIconnector.py

Python package for communictaion with a FairDataPoint (FDP) API. This is based on the fairdatapoint-client api client but adapted for FAIRDataTeam/FAIRDataPoint.

Usage

Import the FDPClient within your Python or Jupyter script.

from fdpAPIconnector.fdpclient import FDPClient

Create an instance of the client and login

fdp_baseurl = "http://localhost:8088"
email = "abmin@FDP.org"
password = "SuperSecretePassword"

fdpclient=FDPClient(fdp_baseurl,email,password)

You can also use an api_token

fdpclient=FDPClient(fdp_baseurl,api_token = 'APITOKEN123456789')

You may change the templates used for catalogs, datasets and distributions

fdpclient=FDPClient(fdp_baseurl,email,password,
                    catalog_template='./<pathToTemplates>/catalog_template.ttl',
                    dataset_template='./<pathToTemplates>/dataset_template.ttl,
                    distribution_template='./<pathToTemplates>/distribution_template.ttl
                    )

Create a new Catalog

newcat=fdpclient.createCatalogRDF(DESCRIPTION='My First Catalog',
                               title='My First Catalog',
                               version="1.0.0",
                               ispartof=fdpclient.publicurl,
                               publisher='Projectmanagementteam',
                               LANGUAGE='http://id.loc.gov/vocabulary/iso639-1/en',
                               HOMEPAGE=fdpclient.baseurl)

pprint(newcat)
id=fdpclient.create(type='catalog',data=newcat)
print(id)

Read the created Catalog

cat_meta=fdpclient.read(type='catalog',id=id)
pprint(cat_meta.serialize(format="turtle"))

Update/Modify the created catalog

fdpclient.update(type='catalog', id=id, data=modified.cat_meta.serialize(format="turtle"))

Publish the created catalog

sate_published=json.dumps({"current":"PUBLISHED"})

fdpclient.update(type='catalog',id=id,subtype='meta/state',data=sate_published,format="json-ld")

Delete the created catalog

fdpclient.delete(type='catalog',id=id)

All functions are also available for datasets and distributions e.g. create:

newdat=fdpclient.createDatasetRDF(title="COVAC-DM Study",
                               version="1.0.0",
                               catalogid=id,
                               publishername="Projectmanagementteam",
                               ISSUED=datetime.datetime.now(),
                               MODIFIED=datetime.datetime.now(),
                               LANGUAGE='http://id.loc.gov/vocabulary/iso639-1/en',
                               KEYWORDS=["diabetes mellitus type 1","diabetes mellitus type 2","COVID-19"],
                               DESCRIPTION="<https://biobank.medunigraz.at/.../biobank/pdf/Kohorten/5006_21_COVAC-DM_Study.pdf>",
                               CONTACTPOINT = "patrick.nitsche@medunigraz.at",
                               themes_list=["http://www.w3.org/ns/dcat#theme","http://identifiers.org/icd/Q87.8"])

newdis=fdpclient.createDistributionRDF(title="Distribution", # HTML distribution
                               version="1.0.0",
                               datasetid=dat_id,
                               publishername="Projectmanagementteam",
                               ISSUED=datetime.datetime.now(),
                               MODIFIED=datetime.datetime.now(),
                               LANGUAGE='http://id.loc.gov/vocabulary/iso639-1/en',
                               mediatype="WSI", #text/html
                               FORMAT="SVS",
                               BYTESIZE="10737418240",
                               ACCESSURL="https://youtu.be/dQw4w9WgXcQ", # URL of OS
                               DOWNLOADURL="http://download.url")
dat_id=fdpclient.create(type='dataset',data=newdat)
dis_id=fdpclient.create(type='distribution',data=newdis)

Subtypes

E.g. Get metadata (memberships and state) for Catalog

cat_meta_state=fdpclient.read(type='catalog',id=id,subtype='meta')

Templates

Templates are used to create the RDF turtle format for the FDP resources. You might want to use your own templates:

fdpclient=FDPClient(fdp_baseurl,email,password,
                    catalog_template='./templates/catalog_template.ttl',
                    dataset_template='./templates/dataset_template.ttl',
                    distribution_template='./templates/distribution_template.ttl')

The template should look like this:

# default_catalog_template

@prefix dcat: <http://www.w3.org/ns/dcat#>.
@prefix dct: <http://purl.org/dc/terms/>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix loc: <§§BASEURL/>.
@prefix n0: <http://>.

loc:new
    a dcat:Catalog, dcat:Resource;
    dct:description "§§DESCRIPTION";
    dct:hasVersion "§§VERSION";
    dct:isPartOf <§§ISPARTOF>;
    dct:language <§§LANGUAGE>;
    dct:license <§§LICENSE>;
    dct:publisher [ a foaf:Agent; foaf:name "§§PUBLISHERNAME"];
    dct:rights <§§RIGHTS>;
    dct:title "§§TITLE";
    foaf:homePage <§§HOMEPAGE>;
    .

Variables that will be replaced have the prefix §§.

You might want to extend the information of the publisher:

Important you should only have one variable per line because each line containing a variable that was not set is thrown out.

@prefix dcat: <http://www.w3.org/ns/dcat#>.
@prefix dct: <http://purl.org/dc/terms/>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix loc: <§§BASEURL/>.
@prefix n0: <http://>.

loc:new
    a dcat:Catalog, dcat:Resource;
    dct:description "§§DESCRIPTION";
    dct:hasVersion "§§VERSION";
    dct:isPartOf <§§ISPARTOF>;
    dct:language <§§LANGUAGE>;
    dct:license <§§LICENSE>;
    dct:publisher [ a foaf:Agent; foaf:name "§§PUBLISHERNAME"; 
                                  rdf:Description [
                                      vcard:hasEmail [rdf:resource "§§PUBLISHEREMAIL"]; 
                                      vcard:hasUID [rdf:resource "§§PUBLISHERUID"]
                                  ]
                  ];
    dct:rights <§§RIGHTS>;
    dct:title "§§TITLE";
    foaf:homePage <§§HOMEPAGE>;
    .

Build and upload to PyPI

# Intsall build and twine
python3 -m pip install --upgrade build
python3 -m pip install --upgrade twine

# Build the package
python3 -m build

# to test the package first 
python3 -m twine upload --repository testpypi dist/*
python3 -m pip install --index-url https://test.pypi.org/simple/ --no-deps fdpAPIconnector

# upload the package
python3 -m twine upload dist/*
# username: __token__
# password: For the password, use the token value, including the pypi- prefix

TODOs

  • allow "login" via api-token instead of email and password
  • auto check for strings and urls(or iri) for RDF turtle creation
  • Create a nice documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fdpAPIconnector-0.0.5.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fdpAPIconnector-0.0.5-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file fdpAPIconnector-0.0.5.tar.gz.

File metadata

  • Download URL: fdpAPIconnector-0.0.5.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for fdpAPIconnector-0.0.5.tar.gz
Algorithm Hash digest
SHA256 4dddfbea820b6c931f883b8499787bcf320f9ec66d25a3f0784d786fe6157bf6
MD5 6ef45e593b833b26848554a5e6566e4a
BLAKE2b-256 01ed9d7d8728c1d3bc500877473f326c2dbc66a64d82c6bb7a14d72cc1fe12f4

See more details on using hashes here.

File details

Details for the file fdpAPIconnector-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for fdpAPIconnector-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b2de325eb82fae40abc068134f5414cda3593f42b3e60a46eacae248378b7a5b
MD5 44aa5a0ea746569907bc9a7c3d26c059
BLAKE2b-256 de19c0f7e08c321675d5809df2bd86541a4b551aae503cc8b130a25a51e387d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page