Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Module to retrieve Figshare metadata for public deposit (no token) #4

Merged
merged 18 commits into from
Feb 17, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[run]
38 changes: 38 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Python package

on:
push:
paths-ignore:
- '**.md'
- 'LICENSE'
pull_request:
paths-ignore:
- '**.md'
- 'LICENSE'

jobs:
build-n-test:
runs-on: ubuntu-latest
# See: https://github.com/marketplace/actions/skip-based-on-commit-message
if: "!contains(github.event.head_commit.message, 'ci skip') || !contains(github.event.head_commit.message, 'skip ci')"
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9']

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install pytest pytest-cov
python setup.py install
- name: Test with pytest
run: |
pytest --cov-report term-missing --cov-config=.coveragerc --cov=readme_tool tests
8 changes: 8 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env python

import uvicorn

from readme_tool.figshare import app

if __name__ == "__main__":
uvicorn.run(app, port=8000)
8 changes: 8 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[pytest]
testpaths =
tests
norecursedirs=dist build .tox scripts
addopts =
--doctest-modules
-r a
-v
87 changes: 87 additions & 0 deletions readme_tool/figshare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from typing import Union
import requests

from fastapi import FastAPI, HTTPException
# from pydantic import BaseModel

api_version = 'v1'

app = FastAPI()


def figshare_metadata_readme(figshare_dict: dict) -> dict:
"""
Function to provide shortened dict for README metadata

:param figshare_dict: Figshare API response

:return: README metadata based on Figshare response
"""
single_str_citation = figshare_dict['citation']

# Handle period in author list. Assume no period in dataset title
astrochun marked this conversation as resolved.
Show resolved Hide resolved
author_list = ([single_str_citation.split('):')[0] + ').'])
author_list += [str_row + '.' for str_row in single_str_citation.split('): ')[1].split('. ')]

readme_dict = {
'article_id': figshare_dict['id'],
'title': figshare_dict['title'],
'description': figshare_dict['description'],
'doi': f"https://doi.org/{figshare_dict['doi']}",
'preferred_citation': author_list,
'license': figshare_dict['license'],
'summary': figshare_dict['description'],
'references': figshare_dict['references'],
}
return readme_dict


@app.get(f'/api/{api_version}/figshare/'+'{article_id}')
def figshare_get(article_id: int, stage: bool = False) -> Union[dict, HTTPException]:
"""
API call to retrieve Figshare metadata

\f
:param article_id: Figshare article ID
:param stage: Figshare stage or production API.
Stage is available for Figshare institutions

:return: Figshare API response
"""

if not stage:
base_url = "https://api.figshare.com"
else:
base_url = "https://api.figsh.com"

url = f"{base_url}/v2/articles/{article_id}"

response = requests.get(url)
if response.status_code != 200:
raise HTTPException(
status_code=response.status_code,
detail=response.json(),
)
else:
return response.json()


@app.get(f'/api/{api_version}/metadata/'+'{article_id}')
async def metadata_get(article_id: int, stage: bool = False) -> dict:
"""
API call for README metadata based on Figshare response

\f
:param article_id: Figshare article ID
:param stage: Figshare stage or production API.
Stage is available for Figshare institutions

:return: README metadata API response
"""

try:
figshare_dict = figshare_get(article_id, stage=stage)
readme_dict = figshare_metadata_readme(figshare_dict)
return readme_dict
except HTTPException as e:
raise e
Empty file added tests/__init__.py
Empty file.
52 changes: 52 additions & 0 deletions tests/test_figshare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from fastapi.testclient import TestClient

from readme_tool.figshare import app, api_version

client = TestClient(app)

article_id = 12966581
stage_article_id = 6941778


def test_figshare_get():
# Production
response = client.get(f'/api/{api_version}/figshare/{article_id}')
assert response.status_code == 200
assert isinstance(response.json(), dict)
assert response.json()['id'] == article_id

# Check for incorrect entry
response = client.get(f'/api/{api_version}/figshare/{stage_article_id}')
assert response.status_code == 404

# Stage
response = client.get(f'/api/{api_version}/figshare/{stage_article_id}?stage=True')
assert response.status_code == 200
assert isinstance(response.json(), dict)
assert response.json()['id'] == stage_article_id

# Check for incorrect entry
response = client.get(f'/api/{api_version}/figshare/{article_id}?stage=True')
assert response.status_code == 404


def test_metadata_get():
# Production
response = client.get(f'/api/{api_version}/metadata/{article_id}')
assert response.status_code == 200
assert isinstance(response.json(), dict)
assert response.json()['article_id'] == article_id

# Check for incorrect entry
response = client.get(f'/api/{api_version}/metadata/{stage_article_id}')
assert response.status_code == 404

# Stage
response = client.get(f'/api/{api_version}/metadata/{stage_article_id}?stage=True')
assert response.status_code == 200
assert isinstance(response.json(), dict)
assert response.json()['article_id'] == stage_article_id

# Check for incorrect entry
response = client.get(f'/api/{api_version}/metadata/{article_id}?stage=True')
assert response.status_code == 404