Skip to content

Commit

Permalink
Merge pull request #69 from clowder-framework/support-v2-API-key
Browse files Browse the repository at this point in the history
Change v2 endpoints to API Keys instead of Bearer tokens
  • Loading branch information
max-zilla authored Jun 5, 2023
2 parents 0e33cea + 4ec3067 commit ac11358
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## 3.0.1 - 2023-05-25

### Changed
This version updates Clowder 2 functionality to use API Key headers instead of Bearer tokens.

## 3.0.0 - 2022-12-16
This version adds Clowder 2 support and removes the old method of extractor registration in favor of reliance on heartbeats.

Expand Down
6 changes: 2 additions & 4 deletions pyclowder/api/v1/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,11 @@ def download_metadata(connector, client, datasetid, extractor=None):
datasetid -- the dataset to fetch metadata of
extractor -- extractor name to filter results (if only one extractor's metadata is desired)
"""
headers = {"Authorization": "Bearer " + client.key}

filterstring = "" if extractor is None else "&extractor=%s" % extractor
url = '%s/api/datasets/%s/metadata?key=%s' % (client.host, datasetid, client.key)
url = '%s/api/datasets/%s/metadata?key=%s' % (client.host, datasetid, client.key + filterstring)

# fetch data
result = requests.get(url, stream=True, headers=headers,
result = requests.get(url, stream=True,
verify=connector.ssl_verify if connector else True)
result.raise_for_status()

Expand Down
19 changes: 9 additions & 10 deletions pyclowder/api/v2/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def create_empty(connector, client, datasetname, description, parentid=None, spa

url = '%s/api/v2/datasets' % client.host
headers = {"Content-Type": "application/json",
"Authorization": "Bearer " + client.key}
"X-API-KEY": client.key}
result = requests.post(url, headers=headers,
data=json.dumps({"name": datasetname, "description": description}),
verify=connector.ssl_verify if connector else True)
Expand All @@ -52,8 +52,7 @@ def delete(connector, client , datasetid):
client -- ClowderClient containing authentication credentials
datasetid -- the dataset to delete
"""
headers = {"Authorization": "Bearer " + client.key}

headers = {"X-API-KEY": client.key}
url = "%s/api/v2/datasets/%s" % (client.host, datasetid)

result = requests.delete(url, headers=headers, verify=connector.ssl_verify if connector else True)
Expand Down Expand Up @@ -97,7 +96,7 @@ def download(connector, client, datasetid):

connector.message_process({"type": "dataset", "id": datasetid}, "Downloading dataset.")

headers = {"Authorization": "Bearer " + client.key}
headers = {"X-API-KEY": client.key}
# fetch dataset zipfile
url = '%s/api/v2/datasets/%s/download' % (client.host, datasetid)
result = requests.get(url, stream=True, headers=headers,
Expand All @@ -121,7 +120,7 @@ def download_metadata(connector, client, datasetid, extractor=None):
datasetid -- the dataset to fetch metadata of
extractor -- extractor name to filter results (if only one extractor's metadata is desired)
"""
headers = {"Authorization": "Bearer " + client.key}
headers = {"X-API-KEY": client.key}

filterstring = "" if extractor is None else "&extractor=%s" % extractor
url = '%s/api/v2/datasets/%s/metadata' % (client.host, datasetid)
Expand All @@ -142,7 +141,7 @@ def get_info(connector, client, datasetid):
client -- ClowderClient containing authentication credentials
datasetid -- the dataset to get info of
"""
headers = {"Authorization": "Bearer " + client.key}
headers = {"X-API-KEY": client.key}

url = "%s/api/v2/datasets/%s" % (client.host, datasetid)

Expand All @@ -161,7 +160,7 @@ def get_file_list(connector, client, datasetid):
client -- ClowderClient containing authentication credentials
datasetid -- the dataset to get filelist of
"""
headers = {"Authorization": "Bearer " + client.key}
headers = {"X-API-KEY": client.key}

url = "%s/api/v2/datasets/%s/files" % (client.host, datasetid)

Expand All @@ -181,7 +180,7 @@ def remove_metadata(connector, client, datasetid, extractor=None):
extractor -- extractor name to filter deletion
!!! ALL JSON-LD METADATA WILL BE REMOVED IF NO extractor PROVIDED !!!
"""
headers = {"Authorization": "Bearer " + client.key}
headers = {"X-API-KEY": client.key}

filterstring = "" if extractor is None else "&extractor=%s" % extractor
url = '%s/api/v2/datasets/%s/metadata' % (client.host, datasetid)
Expand All @@ -202,7 +201,7 @@ def submit_extraction(connector, client, datasetid, extractorname):
extractorname -- registered name of extractor to trigger
"""
headers = {'Content-Type': 'application/json',
"Authorization": "Bearer " + client.key}
"X-API-KEY": client.key}

url = "%s/api/v2/datasets/%s/extractions?key=%s" % (client.host, datasetid)

Expand All @@ -225,7 +224,7 @@ def upload_metadata(connector, client, datasetid, metadata):
metadata -- the metadata to be uploaded
"""
headers = {'Content-Type': 'application/json',
"Authorization": "Bearer " + client.key}
"X-API-KEY": client.key}
connector.message_process({"type": "dataset", "id": datasetid}, "Uploading dataset metadata.")


Expand Down
16 changes: 7 additions & 9 deletions pyclowder/api/v2/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def download(connector, client, fileid, intermediatefileid=None, ext=""):
intermediatefileid = fileid

url = '%s/api/v2/files/%s' % (client.host, intermediatefileid)
headers = {"Authorization": "Bearer " + client.key}
headers = {"X-API-KEY": client.key}
result = connector.get(url, stream=True, verify=connector.ssl_verify if connector else True, headers=headers)

(inputfile, inputfilename) = tempfile.mkstemp(suffix=ext)
Expand All @@ -90,7 +90,7 @@ def download_info(connector, client, fileid):
"""

url = '%s/api/v2/files/%s/metadata' % (client.host, fileid)
headers = {"Authorization": "Bearer " + client.key}
headers = {"X-API-KEY": client.key}
# fetch data
result = connector.get(url, stream=True, verify=connector.ssl_verify if connector else True, headers=headers)

Expand All @@ -109,7 +109,7 @@ def download_metadata(connector,client, fileid, extractor=None):

filterstring = "" if extractor is None else "?extractor=%s" % extractor
url = '%s/api/v2/files/%s/metadata?%s' % (client.host, fileid, filterstring)
headers = {"Authorization": "Bearer " + client.key}
headers = {"X-API-KEY": client.key}

# fetch data
result = connector.get(url, stream=True, verify=connector.ssl_verify if connector else True, headers=headers)
Expand All @@ -130,7 +130,7 @@ def submit_extraction(connector, client, fileid, extractorname):
url = "%s/api/v2/files/%s/extractions?key=%s" % (client.host, fileid, client.key)
result = connector.post(url,
headers={'Content-Type': 'application/json',
"Authorization": "Bearer " + client.key},
"X-API-KEY": client.key},
data=json.dumps({"extractor": extractorname}),
verify=connector.ssl_verify if connector else True)

Expand All @@ -149,9 +149,7 @@ def upload_metadata(connector, client, fileid, metadata):

connector.message_process({"type": "file", "id": fileid}, "Uploading file metadata.")
headers = {'Content-Type': 'application/json',
'Authorization':'Bearer ' + client.key}
print(metadata)
as_json = json.dumps(metadata)
'X-API-KEY': client.key}
url = '%s/api/v2/files/%s/metadata' % (client.host, fileid)
result = connector.post(url, headers=headers, data=json.dumps(metadata),
verify=connector.ssl_verify if connector else True)
Expand Down Expand Up @@ -285,7 +283,7 @@ def upload_to_dataset(connector, client, datasetid, filepath, check_duplicate=Fa
m = MultipartEncoder(
fields={'file': (filename, open(filepath, 'rb'))}
)
headers = {"Authorization": "Bearer " + client.key,
headers = {"X-API-KEY": client.key,
'Content-Type': m.content_type}
result = connector.post(url, data=m, headers=headers,
verify=connector.ssl_verify if connector else True)
Expand Down Expand Up @@ -322,7 +320,7 @@ def _upload_to_dataset_local(connector, client, datasetid, filepath):
m = MultipartEncoder(
fields={'file': (filename, open(filepath, 'rb'))}
)
headers = {"Authorization": "Bearer " + client.key,
headers = {"X-API-KEY": client.key,
'Content-Type': m.content_type}
result = connector.post(url, data=m, headers=headers,
verify=connector.ssl_verify if connector else True)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='pyclowder',
version='3.0.0',
version='3.0.1',
description='Python SDK for the Clowder Data Management System',
long_description=long_description,

Expand Down

0 comments on commit ac11358

Please sign in to comment.