Skip to content

Commit

Permalink
Fixed points from code review
Browse files Browse the repository at this point in the history
1) Unit tests for is_online
2) LTA retry delay is function parameter to download_all
3) partialmethod import for Python 2.7
  • Loading branch information
gbaier committed Aug 19, 2019
1 parent 3d1852d commit 73c7f03
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
16 changes: 10 additions & 6 deletions sentinelsat/sentinel.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def get_product_odata(self, id, full=False):


def is_online(self, id):
"""Return whether a product is online
"""Returns whether a product is online
Parameters
----------
Expand All @@ -445,10 +445,14 @@ def is_online(self, id):
True if online, False if in LTA
"""
# Check https://scihub.copernicus.eu/userguide/ODataAPI#Products_entity for more information

url = urljoin(self.api_url, u"odata/v1/Products('{}')/Online/$value".format(id))
with self.session.get(url, auth=self.session.auth, timeout=self.timeout) as r:
if r.status_code == 200:
return r.text == 'true'
if r.status_code == 200 and r.text == 'true':
return True
elif r.status_code == 200 and r.text == 'false':
return False
else:
raise SentinelAPIError('Could not verify whether product {} is online'.format(id), r)

Expand Down Expand Up @@ -576,7 +580,7 @@ def _trigger_offline_retrieval(self, url):
raise SentinelAPILTAError("Unexpected response from SciHub", r)
return r.status_code

def download_all(self, products, directory_path='.', max_attempts=10, checksum=True, n_concurrent_dl=2):
def download_all(self, products, directory_path='.', max_attempts=10, checksum=True, n_concurrent_dl=2, lta_retry_delay=600):
"""Download a list of products.
Takes a list of product IDs as input. This means that the return value of query() can be
Expand All @@ -603,6 +607,8 @@ def download_all(self, products, directory_path='.', max_attempts=10, checksum=T
Defaults to True.
n_concurrent_dl : integer
number of concurrent downloads
lta_retry_delay : integer
how long to wait between requests to the long term archive. Default is 600 seconds.
Raises
------
Expand All @@ -621,8 +627,6 @@ def download_all(self, products, directory_path='.', max_attempts=10, checksum=T
downloading or triggering failed
"""

lta_retry_delay = 600 # try to request a new product from the LTA every 600 seconds

product_ids = list(products)
self.logger.info("Will download %d products using %d workers", len(product_ids), n_concurrent_dl)

Expand Down
14 changes: 13 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import re
import shutil
from contextlib import contextmanager
from functools import partialmethod
from test.support import EnvironmentVarGuard

import pytest
Expand All @@ -13,6 +12,19 @@
from sentinelsat import InvalidChecksumError, SentinelAPIError, SentinelAPI
from sentinelsat.scripts.cli import cli

try: # Python 3.4 and greater import
from functools import partialmethod
except ImportError: # Older versions of Python, including 2.7
# solution taken from https://gist.github.com/carymrobbins/8940382

from functools import partial

class partialmethod(partial):
def __get__(self, instance, owner):
if instance is None:
return self
return partial(self.func, instance,
*(self.args or ()), **(self.keywords or {}))

@pytest.fixture(scope='session')
def run_cli(credentials):
Expand Down
35 changes: 35 additions & 0 deletions tests/test_odata.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,38 @@ def test_get_product_odata_scihub_down(read_fixture_file):
with pytest.raises(SentinelAPIError) as excinfo:
api.get_product_odata('8df46c9e-a20c-43db-a19a-4240c2ed3b8b')
assert "The Sentinels Scientific Data Hub will be back soon!" in excinfo.value.msg


@pytest.mark.mock_api
def test_is_online():
api = SentinelAPI("mock_user", "mock_password")

uuid = '98ca202b-2155-4181-be88-4358b2cbaaa0'
invalid_uuid = '98ca202b-2155-4181-be88-xxxxxxxxxxxx'

request_url = "https://scihub.copernicus.eu/apihub/odata/v1/Products('{}')/Online/$value"

with requests_mock.mock() as rqst:
rqst.get(
request_url.format(uuid),
text="true", status_code=200
)
assert api.is_online(uuid) == True

with requests_mock.mock() as rqst:
rqst.get(
request_url.format(uuid),
text="false", status_code=200
)
assert api.is_online(uuid) == False


with requests_mock.mock() as rqst:
rqst.get(
request_url.format(invalid_uuid),
text='{{"error":{{"code":null,"message":{{"lang":"en","value":'
'Invalid key ({}) to access Products}}}}}}'.format(invalid_uuid),
status_code=200
)
with pytest.raises(SentinelAPIError) as excinfo:
api.is_online(invalid_uuid)

0 comments on commit 73c7f03

Please sign in to comment.