Skip to content

Commit 23dd102

Browse files
authored
Use DataSource enum to validate source_id in the facade functions (#80)
* added validator for source_id and id * validator for source id * fix typehint error * fix the failing test
1 parent c574a62 commit 23dd102

File tree

10 files changed

+74
-28
lines changed

10 files changed

+74
-28
lines changed

hvpy/api_groups/jpeg2000/get_jp2_image.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from typing import Union
12
from datetime import datetime
23

34
from pydantic import Field, validator
45

6+
from hvpy.datasource import DataSource
57
from hvpy.io import HvpyParameters, OutputType
6-
from hvpy.utils import convert_date_to_isoformat
8+
from hvpy.utils import _data_source_to_int, convert_date_to_isoformat
79

810

911
class getJP2ImageInputParameters(HvpyParameters):
@@ -32,10 +34,11 @@ class getJP2ImageInputParameters(HvpyParameters):
3234
"""
3335

3436
date: datetime
35-
sourceId: int
37+
sourceId: Union[int, DataSource]
3638
jpip: bool = False
3739
Json: bool = Field(False, alias="json")
3840
_date_validator = validator("date", allow_reuse=True)(convert_date_to_isoformat)
41+
_source_id_validator = validator("sourceId", allow_reuse=True)(_data_source_to_int)
3942

4043
def get_output_type(self) -> OutputType:
4144
"""

hvpy/api_groups/jpeg2000/get_jpx.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from typing import Optional
1+
from typing import Union, Optional
22
from datetime import datetime
33

44
from pydantic import validator
55

6+
from hvpy.datasource import DataSource
67
from hvpy.io import HvpyParameters, OutputType
7-
from hvpy.utils import convert_date_to_isoformat
8+
from hvpy.utils import _data_source_to_int, convert_date_to_isoformat
89

910

1011
class getJPXInputParameters(HvpyParameters):
@@ -44,12 +45,13 @@ class getJPXInputParameters(HvpyParameters):
4445

4546
startTime: datetime
4647
endTime: datetime
47-
sourceId: int
48+
sourceId: Union[int, DataSource]
4849
linked: bool = True
4950
verbose: bool = False
5051
jpip: bool = False
5152
cadence: Optional[int] = None
5253
_date_validator = validator("startTime", "endTime", allow_reuse=True)(convert_date_to_isoformat)
54+
_source_id_validator = validator("sourceId", allow_reuse=True)(_data_source_to_int)
5355

5456
def get_output_type(self) -> OutputType:
5557
"""

hvpy/api_groups/jpeg2000/get_jpx_closest_to_mid_point.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from typing import List
1+
from typing import List, Union
22
from datetime import datetime
33

44
from pydantic import validator
55

6+
from hvpy.datasource import DataSource
67
from hvpy.io import HvpyParameters, OutputType
7-
from hvpy.utils import convert_date_to_unix
8+
from hvpy.utils import _data_source_to_int, convert_date_to_unix
89

910

1011
class getJPXClosestToMidPointInputParameters(HvpyParameters):
@@ -39,11 +40,12 @@ class getJPXClosestToMidPointInputParameters(HvpyParameters):
3940

4041
startTimes: List[datetime]
4142
endTimes: List[datetime]
42-
sourceId: int
43+
sourceId: Union[int, DataSource]
4344
linked: bool = True
4445
verbose: bool = False
4546
jpip: bool = False
4647
_date_validator = validator("startTimes", "endTimes", allow_reuse=True)(convert_date_to_unix)
48+
_source_id_validator = validator("sourceId", allow_reuse=True)(_data_source_to_int)
4749

4850
def get_output_type(self) -> OutputType:
4951
"""

hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
from hvpy import getJP2Image
44
from hvpy.api_groups.jpeg2000.get_jp2_image import getJP2ImageInputParameters
5+
from hvpy.datasource import DataSource
56

67

78
def test_str_response(date):
8-
response = getJP2Image(date=date, sourceId=14, jpip=True, json=False)
9+
response = getJP2Image(date=date, sourceId=DataSource.AIA_335, jpip=True, json=False)
910
assert isinstance(response, str)
1011
assert response.startswith("jpip://")
1112

@@ -18,7 +19,7 @@ def test_json_response(date):
1819

1920

2021
def test_raw_response(date):
21-
response = getJP2Image(date=date, sourceId=14, jpip=False, json=False)
22+
response = getJP2Image(date=date, sourceId=DataSource.AIA_335, jpip=False, json=False)
2223
assert isinstance(response, bytes)
2324

2425

@@ -28,7 +29,7 @@ def test_raw_response_with_json(date):
2829

2930

3031
def test_default_response(date):
31-
response = getJP2Image(date=date, sourceId=14)
32+
response = getJP2Image(date=date, sourceId=DataSource.AIA_335)
3233
assert isinstance(response, bytes)
3334

3435

hvpy/api_groups/jpeg2000/tests/test_get_jpx.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
from hvpy import getJPX
44
from hvpy.api_groups.jpeg2000.get_jpx import getJPXInputParameters
5+
from hvpy.datasource import DataSource
56

67

78
def test_raw_response(start_time, end_time):
89
response = getJPX(
910
startTime=start_time,
1011
endTime=end_time,
11-
sourceId=14,
12+
sourceId=DataSource.AIA_335,
1213
linked=False,
1314
verbose=False,
1415
jpip=False,
@@ -21,7 +22,7 @@ def test_str_response(start_time, end_time):
2122
response = getJPX(
2223
startTime=start_time,
2324
endTime=end_time,
24-
sourceId=14,
25+
sourceId=DataSource.AIA_335,
2526
linked=False,
2627
verbose=False,
2728
jpip=True,

hvpy/api_groups/jpeg2000/tests/test_get_jpx_closest_to_mid_point.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,53 @@
22

33
from hvpy import getJPXClosestToMidPoint
44
from hvpy.api_groups.jpeg2000.get_jpx_closest_to_mid_point import getJPXClosestToMidPointInputParameters
5+
from hvpy.datasource import DataSource
56

67

78
def test_raw_response(start_times, end_times):
89
response = getJPXClosestToMidPoint(
9-
startTimes=start_times, endTimes=end_times, sourceId=14, linked=False, verbose=False, jpip=False
10+
startTimes=start_times,
11+
endTimes=end_times,
12+
sourceId=DataSource.AIA_335,
13+
linked=False,
14+
verbose=False,
15+
jpip=False,
1016
)
1117
assert isinstance(response, bytes)
1218

1319

1420
def test_str_response(start_times, end_times):
1521
response = getJPXClosestToMidPoint(
16-
startTimes=start_times, endTimes=end_times, sourceId=14, linked=False, verbose=False, jpip=True
22+
startTimes=start_times,
23+
endTimes=end_times,
24+
sourceId=14,
25+
linked=False,
26+
verbose=False,
27+
jpip=True,
1728
)
1829
assert isinstance(response, str)
1930
assert response.startswith("jpip://")
2031

2132

2233
def test_json_response(start_times, end_times):
2334
response = getJPXClosestToMidPoint(
24-
startTimes=start_times, endTimes=end_times, sourceId=14, linked=False, verbose=True, jpip=True
35+
startTimes=start_times,
36+
endTimes=end_times,
37+
sourceId=DataSource.AIA_335,
38+
linked=False,
39+
verbose=True,
40+
jpip=True,
2541
)
2642
assert isinstance(response, dict)
2743
assert response["uri"].startswith("jpip://")
2844

2945
response = getJPXClosestToMidPoint(
30-
startTimes=start_times, endTimes=end_times, sourceId=14, linked=False, verbose=True, jpip=False
46+
startTimes=start_times,
47+
endTimes=end_times,
48+
sourceId=14,
49+
linked=False,
50+
verbose=True,
51+
jpip=False,
3152
)
3253
assert isinstance(response, dict)
3354
assert response["uri"].startswith("https://")

hvpy/api_groups/official_clients/get_closest_image.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from typing import Optional
1+
from typing import Union, Optional
22
from datetime import datetime
33

44
from pydantic import validator
55

6+
from hvpy.datasource import DataSource
67
from hvpy.io import HvpyParameters, OutputType
7-
from hvpy.utils import convert_date_to_isoformat
8+
from hvpy.utils import _data_source_to_int, convert_date_to_isoformat
89

910

1011
class getClosestImageInputParameters(HvpyParameters):
@@ -30,9 +31,10 @@ class getClosestImageInputParameters(HvpyParameters):
3031
"""
3132

3233
date: datetime
33-
sourceId: int
34+
sourceId: Union[int, DataSource]
3435
callback: Optional[str] = None
3536
_date_validator = validator("date", allow_reuse=True)(convert_date_to_isoformat)
37+
_source_id_validator = validator("sourceId", allow_reuse=True)(_data_source_to_int)
3638

3739
def get_output_type(self) -> OutputType:
3840
"""

hvpy/api_groups/official_clients/tests/test_get_closest_image.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
from hvpy import getClosestImage
44
from hvpy.api_groups.official_clients.get_closest_image import getClosestImageInputParameters
5+
from hvpy.datasource import DataSource
56

67

78
def test_json_res(date):
8-
response = getClosestImage(date=date, sourceId=14)
9+
response = getClosestImage(date=date, sourceId=DataSource.AIA_335)
910
assert isinstance(response, dict)
1011

1112

hvpy/facade.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime
33

44
from hvpy.core import execute_api_call
5+
from hvpy.datasource import DataSource
56
from hvpy.parameters import *
67
from hvpy.utils import _add_shared_docstring
78

@@ -28,7 +29,7 @@
2829
@_add_shared_docstring(getJP2ImageInputParameters)
2930
def getJP2Image(
3031
date: datetime,
31-
sourceId: int,
32+
sourceId: Union[int, DataSource],
3233
jpip: bool = False,
3334
json: bool = False,
3435
) -> Union[bytes, str, Dict[str, Any]]:
@@ -75,7 +76,7 @@ def getJP2Header(
7576
def getJPXClosestToMidPoint(
7677
startTimes: List[datetime],
7778
endTimes: List[datetime],
78-
sourceId: int,
79+
sourceId: Union[int, DataSource],
7980
linked: bool = True,
8081
verbose: bool = False,
8182
jpip: bool = False,
@@ -115,7 +116,7 @@ def getJPXClosestToMidPoint(
115116
def getJPX(
116117
startTime: List[datetime],
117118
endTime: List[datetime],
118-
sourceId: int,
119+
sourceId: Union[int, DataSource],
119120
linked: bool = True,
120121
verbose: bool = False,
121122
jpip: bool = False,
@@ -175,7 +176,7 @@ def getStatus() -> Union[bytes, str, Dict[str, Any]]:
175176
@_add_shared_docstring(getClosestImageInputParameters)
176177
def getClosestImage(
177178
date: datetime,
178-
sourceId: int,
179+
sourceId: Union[int, DataSource],
179180
callback: Optional[str] = None,
180181
) -> Union[bytes, str, Dict[str, Any]]:
181182
"""
@@ -424,7 +425,7 @@ def reQueueMovie(
424425

425426
@_add_shared_docstring(getMovieStatusInputParameters)
426427
def getMovieStatus(
427-
id: str,
428+
id: Union[int, DataSource],
428429
format: str,
429430
verbose: bool = False,
430431
callback: Optional[str] = None,
@@ -454,7 +455,7 @@ def getMovieStatus(
454455

455456
@_add_shared_docstring(downloadMovieInputParameters)
456457
def downloadMovie(
457-
id: str,
458+
id: Union[int, DataSource],
458459
format: str,
459460
hq: bool = False,
460461
) -> Union[bytes, str, Dict[str, Any]]:
@@ -527,7 +528,7 @@ def shortenURL(
527528

528529
@_add_shared_docstring(getTileInputParameters)
529530
def getTile(
530-
id: int,
531+
id: Union[int, DataSource],
531532
x: int,
532533
y: int,
533534
imageScale: int,

hvpy/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ def convert_date_to_unix(v: list) -> str:
4949
return ",".join([str(int(datetime.timestamp(d))) for d in v])
5050

5151

52+
def _data_source_to_int(source: Union[int, DataSource]) -> int:
53+
"""
54+
Converts a `~hvpy.DataSource` to an integer.
55+
56+
Parameters
57+
----------
58+
source
59+
The `~hvpy.DataSource` to convert.
60+
"""
61+
return _to_datasource(source).value
62+
63+
5264
def _to_datasource(val: Union[int, DataSource]) -> DataSource:
5365
"""
5466
Validates the input and converts it to a DataSource enum.

0 commit comments

Comments
 (0)