Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
znicholls committed Aug 22, 2024
1 parent 0a20f71 commit 168ca25
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/10.feature.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Added the `--reserve-doi` flag to the `openscm-zenodo update-metadata` command.
Also added [`get_reserved_doi`][openscm_zenodo.zenodo.get_reserved_doi].
12 changes: 7 additions & 5 deletions src/openscm_zenodo/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

import openscm_zenodo
from openscm_zenodo.logging import setup_logging
from openscm_zenodo.zenodo import ZenodoDomain, ZenodoInteractor, create_new_version
from openscm_zenodo.zenodo import (
ZenodoDomain,
ZenodoInteractor,
create_new_version,
get_reserved_doi,
)

app = typer.Typer()

Expand Down Expand Up @@ -213,10 +218,7 @@ def update_metadata_command(
)

if reserve_doi:
reserved_doi = update_metadata_response.json()["metadata"]["prereserve_doi"][
"doi"
]
print(reserved_doi)
print(get_reserved_doi(update_metadata_response))


@app.command(name="upload-files")
Expand Down
23 changes: 23 additions & 0 deletions src/openscm_zenodo/zenodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,26 @@ def create_new_version(
zenoodo_interactor.publish(new_deposition_id)

return str(new_deposition_id)


def get_reserved_doi(zenodo_record_response: requests.models.Response) -> str:
"""
Get the reserved DOI from a Zenodo record response
We think that this works
with basically any response related to retrieving a record from Zenodo,
because it basically just looks at the metadata field.
However, it may not support all responses.
You have been warned.
Parameters
----------
zenodo_record_response
The Zenodo response for a record, from which to get the reserved DOI.
Returns
-------
:
The record's reserved DOI
"""
return zenodo_record_response.json()["metadata"]["prereserve_doi"]["doi"]
4 changes: 2 additions & 2 deletions tests/integration/test_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import pytest
import requests

from openscm_zenodo.zenodo import ZenodoDomain, ZenodoInteractor
from openscm_zenodo.zenodo import ZenodoDomain, ZenodoInteractor, get_reserved_doi


@pytest.mark.zenodo_token
Expand Down Expand Up @@ -58,7 +58,7 @@ def test_default_end_to_end_flow(test_data_dir):
assert isinstance(update_metadata_response, requests.models.Response)

# Just a test that this exists really, but handy trick to know
reserved_doi = update_metadata_response.json()["metadata"]["prereserve_doi"]["doi"]
reserved_doi = get_reserved_doi(update_metadata_response)
assert "10.5281/zenodo" in reserved_doi

bucket_url = zenoodo_interactor.get_bucket_url(deposition_id=new_deposition_id)
Expand Down

0 comments on commit 168ca25

Please sign in to comment.