Skip to content

Commit beb3a17

Browse files
make gdcapiwrapper pip installable
1 parent c198004 commit beb3a17

File tree

9 files changed

+109
-5
lines changed

9 files changed

+109
-5
lines changed

LICENSE renamed to LICENSE.txt

File renamed without changes.

MANIFEST.in

Whitespace-only changes.

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ The GDC API drives the GDC Data and Submission Portals and provides programmatic
99

1010
## Features implemented
1111
- Downloading a Single File using GET
12-
- Downloading Multiple Files using POST
12+
- Downloading Multiple Files using POST
13+
14+
## Usage
15+
`pip install gdc-api-wrapper`

setup.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# coding: utf-8
2+
3+
import os
4+
import re
5+
6+
import setuptools
7+
8+
9+
def ascii_bytes_from(path, *paths):
10+
"""
11+
Return the ASCII characters in the file specified by *path* and *paths*.
12+
The file path is determined by concatenating *path* and any members of
13+
*paths* with a directory separator in between.
14+
"""
15+
file_path = os.path.join(path, *paths)
16+
with open(file_path) as f:
17+
ascii_bytes = f.read()
18+
return ascii_bytes
19+
20+
21+
# read required text from files
22+
thisdir = os.path.dirname(__file__)
23+
init_py = ascii_bytes_from(thisdir, "src", "gdcapiwrapper", "__init__.py")
24+
readme = ascii_bytes_from(thisdir, "README.md")
25+
# This allows users to check installed version with:
26+
# `python -c 'from gdcapiwrapper import __version__; print(__version__)'`
27+
version = re.search('__version__ = "([^"]+)"', init_py).group(1)
28+
29+
install_requires = ["requests[security]", "responses", "tqdm"]
30+
31+
test_requires = ["pytest", "pytest-xdist", "coverage", "pytest-cov", "coveralls"]
32+
33+
setuptools.setup(
34+
name="gdc-api-wrapper",
35+
version=version,
36+
maintainer="Histolab Developers",
37+
maintainer_email="[email protected]",
38+
author="E. Arbitrio",
39+
description="A simple Python wrapper for the GDC Application Programming Interface",
40+
long_description=readme,
41+
long_description_content_type="text/markdown",
42+
url="https://github.com/histolab/gdc-api-wrapper",
43+
download_url="https://github.com/histolab/gdc-api-wrapper",
44+
install_requires=install_requires,
45+
tests_require=test_requires,
46+
extras_require={"testing": test_requires},
47+
packages=setuptools.find_packages("src", exclude=["tests"]),
48+
classifiers=[
49+
"Programming Language :: Python :: 3",
50+
"Programming Language :: Python :: 3.6",
51+
"Programming Language :: Python :: 3.7",
52+
"Programming Language :: Python :: 3.8",
53+
"License :: OSI Approved :: Apache Software License",
54+
"Operating System :: OS Independent",
55+
"Intended Audience :: Science/Research",
56+
],
57+
package_dir={"": "src"},
58+
include_package_data=True,
59+
entry_points={},
60+
test_suite="pytest",
61+
zip_safe=True,
62+
python_requires=">=3.6",
63+
)

src/__init__.py

Whitespace-only changes.

gdcapiwrapper/__init__.py renamed to src/gdcapiwrapper/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# encoding: utf-8
22

33
import os
4-
54
import requests
65

6+
__version__ = "0.1"
77
GDC_API_TOKEN = os.environ.get("GCC_API_TOKEN", None)
88
GDC_API_BASE_URL = os.environ.get("GDC_API_BASE_URL", "https://api.gdc.cancer.gov/")
99

@@ -17,6 +17,8 @@ class APITokenMissingError(Exception):
1717

1818

1919
request = requests.get(f"{GDC_API_BASE_URL}/status")
20+
21+
2022
if request.status_code != 200:
2123
raise APIBaseURLStatusError(
2224
f"{GDC_API_BASE_URL} status: {request.status_code}."

gdcapiwrapper/data.py renamed to src/gdcapiwrapper/data.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,33 @@
1919

2020

2121
class Data(object):
22+
""" Provides Data objects for https://api.gdc.cancer.gov/data/ `Data Endpoints`
23+
24+
Includes endpoints for file(s) download
25+
"""
26+
2227
@classmethod
2328
def download(
2429
cls, uuid: str, path: str = ".", name: str = None
2530
) -> Tuple[Response, str]:
31+
"""Download file and return the related api response and the file path
32+
33+
Parameters
34+
---------
35+
uuid : str
36+
File UUID
37+
path: str
38+
Local path where save file (default: current path)
39+
name: str
40+
Filename. If not provided it will be saved with UUID as name
41+
42+
Returns
43+
-------
44+
tuple
45+
response, filename absolute path
46+
"""
2647
url = f"{base_url}/{uuid}"
48+
2749
local_filename = uuid if not name else name
2850
with requests.get(url, stream=True) as r:
2951
total_size = int(r.headers.get("content-length", 0))
@@ -36,6 +58,20 @@ def download(
3658
def download_multiple(
3759
cls, uuid_list: list, path: str = "."
3860
) -> Tuple[Response, str]:
61+
"""Download multiple files and return the related api response and the file path
62+
63+
Parameters
64+
---------
65+
uuid_list : list
66+
List of UUID(s) to save
67+
path: str
68+
Local path where save file (default: current path)
69+
70+
Returns
71+
-------
72+
tuple
73+
response, filename absolute path
74+
"""
3975
with requests.post(base_url, stream=True, data={"ids": uuid_list}) as r:
4076
d = r.headers["content-disposition"]
4177
fname = re.findall("filename=(.+)", d)[0]
File renamed without changes.

tests/unit/test_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# encoding: utf-8
22

33
import os
4-
from unittest.mock import patch
4+
from unittest import mock
55

66
import pytest
77
from requests.exceptions import ChunkedEncodingError
@@ -20,7 +20,7 @@ def setup_class(cls):
2020
def test_download(self, tmpdir):
2121
base_url = "http://localhost:{port}/data".format(port=self.mock_server_port)
2222

23-
with patch.dict("gdcapiwrapper.data.__dict__", {"base_url": base_url}):
23+
with mock.patch.dict("gdcapiwrapper.data.__dict__", {"base_url": base_url}):
2424
response, filename = Data.download(
2525
uuid="fakeuuid", path=tmpdir, name="fakefilename"
2626
)
@@ -31,7 +31,7 @@ def test_download(self, tmpdir):
3131
def test_download_multiple(self, tmpdir):
3232
base_url = "http://localhost:{port}".format(port=self.mock_server_port)
3333
try:
34-
with patch.dict("gdcapiwrapper.data.__dict__", {"base_url": base_url}):
34+
with mock.patch.dict("gdcapiwrapper.data.__dict__", {"base_url": base_url}):
3535
response, filename = Data.download_multiple(
3636
uuid_list=["1", "2"], path=tmpdir
3737
)

0 commit comments

Comments
 (0)