-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Simon Ternes
committed
Nov 6, 2024
1 parent
e8731a6
commit 3e82ea7
Showing
8 changed files
with
232 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
### Only runs clean when api is running | ||
|
||
import requests | ||
import json | ||
import sys; | ||
base_url = 'http://localhost:8000/fairdi/nomad/latest/api/v1' | ||
|
||
response = requests.post( | ||
f'{base_url}/entries/query', | ||
json={ | ||
'query': { | ||
'results.material.elements': { | ||
'all': ['Ti', 'O'] | ||
} | ||
}, | ||
'pagination': { | ||
'page_size': 1 | ||
}, | ||
'required': { | ||
'include': ['entry_id'] | ||
} | ||
}) | ||
response_json = response.json() | ||
|
||
print(json.dumps(response.json(), indent=2)) | ||
|
||
|
||
|
||
|
||
|
||
if(response_json['data'] == []): | ||
print("No data in system.") | ||
sys.exit() | ||
|
||
first_entry_id = response_json['data'][0]['entry_id'] | ||
|
||
response = requests.post( | ||
f'{base_url}/entries/{first_entry_id}/archive/query', | ||
json={ | ||
'required': { | ||
'workflow': { | ||
'calculation_result_ref': { | ||
'energy': '*', | ||
'system_ref': { | ||
'chemical_composition': '*' | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
response_json = response.json() | ||
print(json.dumps(response_json, indent=2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
### Only runs clean when api is running | ||
|
||
import requests | ||
import json | ||
|
||
nomad_url = 'http://localhost:8000/prod/v1/api/v1' | ||
|
||
def get_authentication_token( username, password): | ||
'''Get the token for accessing your NOMAD unpublished uploads remotely''' | ||
try: | ||
response = requests.get( | ||
nomad_url + 'auth/token', params=dict(username=username, password=password), timeout=10) | ||
token = response.json().get('access_token') | ||
if token: | ||
return token | ||
|
||
print('response is missing token: ') | ||
print(response.json()) | ||
return | ||
except Exception: | ||
print('something went wrong trying to get authentication token') | ||
return | ||
|
||
|
||
def create_dataset(token, dataset_name): | ||
'''Create a dataset to group a series of NOMAD entries''' | ||
try: | ||
response = requests.post( | ||
nomad_url + 'datasets/', | ||
headers={'Authorization': f'Bearer {token}', 'Accept': 'application/json'}, | ||
json={"dataset_name": dataset_name}, | ||
timeout=10 | ||
) | ||
dataset_id = response.json().get('dataset_id') | ||
if dataset_id: | ||
return dataset_id | ||
|
||
print('response is missing dataset_id: ') | ||
print(response.json()) | ||
return | ||
except Exception: | ||
print('something went wrong trying to create a dataset') | ||
return | ||
|
||
def upload_to_NOMAD(token, upload_file): | ||
'''Upload a single file for NOMAD upload, e.g., zip format''' | ||
with open(upload_file, 'rb') as f: | ||
try: | ||
response = requests.post( | ||
nomad_url + 'uploads', | ||
headers={'Authorization': f'Bearer {token}', 'Accept': 'application/json'}, | ||
data=f, timeout=30) | ||
upload_id = response.json().get('upload_id') | ||
if upload_id: | ||
return upload_id | ||
|
||
print('response is missing upload_id: ') | ||
print(response.json()) | ||
return | ||
except Exception: | ||
print('something went wrong uploading to NOMAD') | ||
return | ||
|
||
def check_upload_status(token, upload_id): | ||
''' | ||
# upload success => returns 'Process publish_upload completed successfully' | ||
# publish success => 'Process publish_upload completed successfully' | ||
''' | ||
try: | ||
response = requests.get( | ||
nomad_url + 'uploads/' + upload_id, | ||
headers={'Authorization': f'Bearer {token}'}, timeout=30) | ||
status_message = response.json().get('data').get('last_status_message') | ||
if status_message: | ||
return status_message | ||
|
||
print('response is missing status_message: ') | ||
print(response.json()) | ||
return | ||
except Exception: | ||
print('something went wrong trying to check the status of upload' + upload_id) | ||
# upload gets deleted from the upload staging area once published...or in this case something went wrong | ||
return | ||
|
||
def edit_upload_metadata(token, upload_id, metadata): | ||
''' | ||
Example of new metadata: | ||
upload_name = 'Test_Upload_Name' | ||
metadata = { | ||
"metadata": { | ||
"upload_name": upload_name, | ||
"references": ["https://doi.org/xx.xxxx/xxxxxx"], | ||
"datasets": dataset_id, | ||
"embargo_length": 0, | ||
"coauthors": ["[email protected]"], | ||
"comment": 'This is a test upload...' | ||
}, | ||
} | ||
''' | ||
|
||
try: | ||
response = requests.post( | ||
nomad_url+'uploads/' + upload_id + '/edit', | ||
headers={'Authorization': f'Bearer {token}', 'Accept': 'application/json'}, | ||
json=metadata, timeout=30) | ||
return response | ||
except Exception: | ||
print('something went wrong trying to add metadata to upload' + upload_id) | ||
return | ||
|
||
def publish_upload(token, upload_id): | ||
'''Publish an upload''' | ||
try: | ||
response = requests.post( | ||
nomad_url+'uploads/' + upload_id + '/action/publish', | ||
headers={'Authorization': f'Bearer {token}', 'Accept': 'application/json'}, | ||
timeout=30) | ||
return response | ||
except Exception: | ||
print('something went wrong trying to publish upload: ' + upload_id) | ||
return | ||
|
||
print(get_authentication_token("admin", "password")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
data: | ||
m_def: nomad_unitov_plugin.schema_packages.hysprint_package.HySprint_Sample | ||
m_def: nomad_unitov_plugin.schema_packages.unitov_package.Unitov_Sample | ||
name: Markus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters