Skip to main content

Python lib to interact with the NPDC stack

Project description

pynpdc

pynpdc is a library for accessing the Norwegian Polar Data Centre using Python3. It provides clients with simple methods for logging in and out as well as fetching and manipulating datasets and attachments.

It is based on the following REST APIs:

Getting started

Use

pip3 install pynpdc

to install pynpdc into your project.

Examples for reading datasets

Get ids and titles from public datasets filtered by a search query

from pynpdc import DatasetClient, DATASET_LIFE_ENTRYPOINT

client = DatasetClient(DATASET_LIFE_ENTRYPOINT)

query = "fimbulisen"
for dataset in client.get_datasets(q=query):
    print(dataset.id, dataset.content["title"])

Get ids and titles from draft datasets for a logged in user

from getpass import getpass
from pynpdc import (
    AUTH_LIFE_ENTRYPOINT,
    DATASET_LIFE_ENTRYPOINT,
    APIException,
    AuthClient,
    DatasetClient,
    DatasetType,
)

auth_client = AuthClient(AUTH_LIFE_ENTRYPOINT)

print("Email: ", end="")
user = input()
password = getpass()

try:
    account = auth_client.login(user, password)
except APIException:
    print("Login failed")
    exit()

client = DatasetClient(DATASET_LIFE_ENTRYPOINT, auth=account)
for dataset in client.get_datasets(type=DatasetType.DRAFT):
    print(dataset.id, dataset.content["title"])

Get metadata for a certain public dataset

import json
from pynpdc import DATASET_LIFE_ENTRYPOINT, DatasetClient

client = DatasetClient(DATASET_LIFE_ENTRYPOINT)

ID = "fdd9eaf1-b426-41af-835d-80b8d55f54db"
dataset = client.get_dataset(ID)
if dataset is None:
    print("dataset not found")
else:
    print(json.dumps(dataset.content, indent=2))

Examples for reading attachments

Get attachments metadata of a certain public dataset

from pynpdc import DatasetClient, DATASET_LIFE_ENTRYPOINT

client = DatasetClient(DATASET_LIFE_ENTRYPOINT)

ID = "19e96642-8b66-48c7-a66f-50dd05cc6eee"
attachments = client.get_attachments(ID)
for attachment in attachments:
    print(f"{attachment.filename} ({attachment.byte_size} bytes)")

Download all attachments from a certain public dataset as zip file

The zip file will be downloaded to the same folder as the script

from os import path
from pynpdc import DATASET_LIFE_ENTRYPOINT, DatasetClient

client = DatasetClient(DATASET_LIFE_ENTRYPOINT)

target_directory = path.dirname(__file__)
ID = "19e96642-8b66-48c7-a66f-50dd05cc6eee"
filepath = client.download_attachments_as_zip(ID, target_directory)
print(filepath)

Examples for creating, updating and deleting datasets and attachments

:warning: Never use the live endpoints for testing your code because it will add a lot of noise. Even though it is not possible to publish datasets with pytest this noise should be avoided.

urllib3 helps to get rid of the InsecureRequestWarning when you deal with staging entrypoints. If you get the error

ModuleNotFoundError: No module named 'urllib3'

install the module with:

pip3 install urllib3

CRUD dataset (create, read, update, delete)

import urllib3

from pynpdc import (
    AUTH_STAGING_ENTRYPOINT,
    DATASET_STAGING_ENTRYPOINT,
    APIException,
    AuthClient,
    DatasetClient,
)

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Create token by logging in

auth_client = AuthClient(AUTH_STAGING_ENTRYPOINT, verify_ssl=False)
user = "foo@example.org"
password = "1234123412341234"

try:
    account = auth_client.login(user, password)
except APIException as e:
    print("Login failed", e.status_code)
    exit()

# Create a client to talk to the dataset API

dataset_client = DatasetClient(
    DATASET_STAGING_ENTRYPOINT, auth=account, verify_ssl=False
)

# Create a dataset

content = {"title": "pynpdc example from readme"}
dataset = dataset_client.create_dataset(content)
ID = dataset.id

# Read this dataset and show title

dataset = dataset_client.get_dataset(ID)
print(dataset.content["title"])

# Update the dataset

content["title"] = "updated pytest example from readme"
dataset_client.update_dataset(ID, content)

# Read this dataset again and show title

dataset = dataset_client.get_dataset(ID)
print(dataset.content["title"])

# Delete this dataset

dataset_client.delete_dataset(ID)

# Reading this dataset again will return None

dataset = dataset_client.get_dataset(ID)
print(dataset, "is None")

CRUD attachment (create, read, update, delete)

import urllib3

from pynpdc import (
    AUTH_STAGING_ENTRYPOINT,
    DATASET_STAGING_ENTRYPOINT,
    APIException,
    AuthClient,
    DatasetClient,
)

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Create token by logging in

auth_client = AuthClient(AUTH_STAGING_ENTRYPOINT, verify_ssl=False)
user = "foo@example.org"
password = "1234123412341234"

try:
    account = auth_client.login(user, password)
except APIException as e:
    print("Login failed", e.status_code, e.response.request.url)
    exit()

# Create a client to talk to the dataset API

dataset_client = DatasetClient(
    DATASET_STAGING_ENTRYPOINT, auth=account, verify_ssl=False
)

# Create a dataset

content = {"title": "pynpdc example from readme"}
dataset = dataset_client.create_dataset(content)

# Add an attachment

attachment = dataset_client.upload_attachment(
    dataset.id,
    __file__,  # path of this Python script
    title="Optional title",
    description="Optional description",
)

# Read attachment metadata

attachment = dataset_client.get_attachment(dataset.id, attachment.id)
print(f"{attachment.title} ({attachment.filename})")

# Update attachment metadata (all the keys have to be provided)

updated_meta = {
    "description": attachment.description,
    "filename": attachment.filename,
    "prefix": "/",
    "title": "Updated title",
}
attachment = dataset_client.update_attachment(dataset.id, attachment.id, **updated_meta)

# Read attachment metadata again

attachment = dataset_client.get_attachment(dataset.id, attachment.id)
print(f"{attachment.title} ({attachment.filename})")

# Delete the attachment

dataset_client.delete_attachment(dataset.id, attachment.id)

# Reading attachment metadata again will return None

attachment = dataset_client.get_attachment(dataset.id, attachment.id)
print(attachment, "is None")

# Delete this dataset

dataset_client.delete_dataset(dataset.id)

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

pynpdc-0.19.1.tar.gz (25.7 kB view details)

Uploaded Source

Built Distribution

pynpdc-0.19.1-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file pynpdc-0.19.1.tar.gz.

File metadata

  • Download URL: pynpdc-0.19.1.tar.gz
  • Upload date:
  • Size: 25.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pynpdc-0.19.1.tar.gz
Algorithm Hash digest
SHA256 25b2d7d8eb21fcde6fdb2bce5190cd403b2dae7ec3f158dd03eeb4b45c6e5c3e
MD5 0f94f55120df58f4fa458fb5c9bb8a04
BLAKE2b-256 96f37a8f137f2445e62f56f209a02d9ffcbec5e6701354fd189f2c9962a6fa7c

See more details on using hashes here.

File details

Details for the file pynpdc-0.19.1-py3-none-any.whl.

File metadata

  • Download URL: pynpdc-0.19.1-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pynpdc-0.19.1-py3-none-any.whl
Algorithm Hash digest
SHA256 82556602e19e70c9c94d430f15f4825bf6f83548ae3d84b0696348aea26dc940
MD5 cb668b0ae005dd9d6db67d978b7ea1df
BLAKE2b-256 8e3eeb394bd5518dfff07263d68436f5f248112a4e3137d876d2348297e310b9

See more details on using hashes here.

Supported by

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