-
Notifications
You must be signed in to change notification settings - Fork 4
Design Generic function for the getJP2Image endpoint #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
58 commits
Select commit
Hold shift + click to select a range
e2aa6c9
getjp2header image endpoint
akash5100 b018b05
use pydantic for i/o parameters
akash5100 d4946d9
Add dict function that renames Json to json
dgarciabriseno d228d5c
Remove print statement
akash5100 8d9e66c
Merge branch 'develop' into json-workaround
akash5100 f791895
Merge pull request #1 from dgarciabriseno/json-workaround
akash5100 cf48785
initial setup
akash5100 ef72ef4
Initialize the Base io
akash5100 d3595e3
Initialize the Base core
akash5100 a8e73ff
Replace string with OutputType
akash5100 8151856
Update hvpy/tests/test_get_jp2_image.py
akash5100 6e3b992
Update hvpy/tests/test_get_jp2_image.py
akash5100 e3d33fd
Update hvpy/core.py
akash5100 0c6842d
Update hvpy/core.py
akash5100 fe0dcb2
Update hvpy/core.py
akash5100 b39b3b4
Adds missing import
akash5100 3376820
Update hvpy/io.py
akash5100 20ec51f
Update hvpy/io.py
akash5100 388390f
isort fix
akash5100 7292e0c
isort fix
akash5100 d92def4
Update setup.cfg
a869baf
Merge branch 'main' of github.com:akash5100/python-api into main
akash5100 3138b3f
fix precommit failure
akash5100 9d105f2
Fix failing test
akash5100 739609a
Test raw and default response
akash5100 5669f7d
Merge branch 'main' into develop
akash5100 1e385f8
Precommit fixed
akash5100 0b23be1
Update hvpy/core.py
akash5100 e261c3b
Update hvpy/tests/test_get_jp2_image.py
akash5100 7727fab
Update hvpy/tests/test_get_jp2_image.py
akash5100 2d2f1b1
Update hvpy/io.py
akash5100 ff90d2c
Merge branch 'develop' of github.com:akash5100/python-api into develop
akash5100 1490305
Update hvpy/core.py
akash5100 51bc5aa
Add test and template docstring
akash5100 44c3e58
Fix merge conflict
akash5100 60a7349
Update io.py and core.py
akash5100 009d5da
Update docstring
akash5100 15009d4
Update docstring
akash5100 dd239c0
Update docstring
akash5100 21a7e0b
Update docstring
akash5100 d5f2c9b
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 b6146db
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 742ced1
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 765b043
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 ea00653
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 46dd707
Fix codestyle
akash5100 9f348d8
tests refactor
akash5100 40544f6
update docstring
akash5100 9d54697
Update get_jp2_image.py
akash5100 2084784
Remove Optional from getJP2Image endpoint
akash5100 95cba5a
Test for value error
akash5100 7c6c5b9
Fix alias for Json
akash5100 6582a1b
Check for error message
akash5100 3b76310
Test for IO module
akash5100 206f544
Update test and import endpoint to io.py
akash5100 81d592d
Update hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py
akash5100 a4b274b
Update test
akash5100 45983a8
Merge branch 'develop' of github.com:akash5100/python-api into develop
akash5100 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1 @@ | ||
| from .api import * # NOQA | ||
| from .version import __version__ |
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| from datetime import datetime | ||
|
|
||
| from pydantic import Field, validator | ||
|
|
||
| from hvpy.io import HvpyParameters, OutputType | ||
|
|
||
|
|
||
| class getJP2ImageInputParameters(HvpyParameters): | ||
| """ | ||
| Handles the input parameters of the `getJP2Image API. | ||
|
|
||
| <https://api.helioviewer.org/docs/v2/api/api_groups/jpeg2000.html#getjp2image>`__. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| date : datetime | ||
| Desired date/time of the JP2 image. | ||
| sourceId : int | ||
| Unique image datasource identifier. | ||
| jpip : bool, optional | ||
| Returns a JPIP URI instead of the binary data of the image if set to `True`, defaults to `False`. | ||
| json : bool, optional | ||
| Returns the JSON if set to `True`, defaults to `False`. | ||
| """ | ||
|
|
||
| date: datetime | ||
| sourceId: int | ||
| jpip: bool = False | ||
| Json: bool = Field(False, alias="json") | ||
|
|
||
| @validator("date") | ||
| def convert_date_to_isoformat(cls, v): | ||
| """ | ||
| Converts the date from a datetime object to a string in the ISO format. | ||
| """ | ||
| return v.isoformat() + "Z" | ||
|
|
||
| def get_output_type(self): | ||
| """ | ||
| Returns the output type of the API call. | ||
| """ | ||
| if self.Json and self.jpip: | ||
| return OutputType.Json | ||
| elif not self.Json and self.jpip: | ||
| return OutputType.String | ||
| else: | ||
| return OutputType.Raw | ||
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
| from pydantic import ValidationError | ||
|
|
||
| from hvpy.api_groups.jpeg2000.get_jp2_image import getJP2ImageInputParameters | ||
| from hvpy.core import execute_api_call | ||
|
|
||
|
|
||
| def test_str_response(): | ||
| date_obj = datetime(2022, 1, 1, 23, 59, 59) | ||
| params = {"date": date_obj, "sourceId": 14, "jpip": True, "json": False} | ||
| params = getJP2ImageInputParameters(**params) | ||
| response = execute_api_call(input_parameters=params) | ||
| assert isinstance(response, str) | ||
| assert response.startswith("jpip://") | ||
|
|
||
|
|
||
| def test_json_response(): | ||
| date_obj = datetime(2022, 1, 1, 23, 59, 59) | ||
| params = {"date": date_obj, "sourceId": 14, "jpip": True, "json": True} | ||
| params = getJP2ImageInputParameters(**params) | ||
| response = execute_api_call(input_parameters=params) | ||
| assert isinstance(response, dict) | ||
| assert "uri" in response | ||
| assert response["uri"].startswith("jpip://") | ||
|
|
||
|
|
||
| def test_raw_response(): | ||
| date_obj = datetime(2022, 1, 1, 23, 59, 59) | ||
| params = {"date": date_obj, "sourceId": 14, "jpip": False, "json": False} | ||
| params = getJP2ImageInputParameters(**params) | ||
| response = execute_api_call(input_parameters=params) | ||
| assert isinstance(response, bytes) | ||
|
|
||
|
|
||
| def test_raw_response_with_json(): | ||
| date_obj = datetime(2022, 1, 1, 23, 59, 59) | ||
| params = {"date": date_obj, "sourceId": 14, "jpip": False, "json": True} | ||
| params = getJP2ImageInputParameters(**params) | ||
| response = execute_api_call(input_parameters=params) | ||
| assert isinstance(response, bytes) | ||
|
|
||
|
|
||
| def test_default_response(): | ||
| date_obj = datetime(2022, 1, 1, 23, 59, 59) | ||
| params = {"date": date_obj, "sourceId": 14} | ||
| params = getJP2ImageInputParameters(**params) | ||
| response = execute_api_call(input_parameters=params) | ||
| assert isinstance(response, bytes) | ||
|
|
||
|
|
||
| def test_error_handling(): | ||
| params = {"sourceId": 14, "jpip": True, "json": True} | ||
| with pytest.raises(ValidationError, match="getJP2ImageInputParameters\ndate\n field required"): | ||
| getJP2ImageInputParameters(**params) | ||
|
|
||
|
|
||
| def test_unknown_parameters(): | ||
| date_obj = datetime(2022, 1, 1, 23, 59, 59) | ||
| params = {"date": date_obj, "sourceId": 14, "jpip": True, "json": True, "should_reject_this": True} | ||
nabobalis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| params = getJP2ImageInputParameters(**params) | ||
| response = execute_api_call(input_parameters=params) | ||
| assert isinstance(response, dict) | ||
| assert "uri" in response | ||
| assert response["uri"].startswith("jpip://") | ||
|
|
||
|
|
||
| def test_url_property(): | ||
| date_obj = datetime(2022, 1, 1, 23, 59, 59) | ||
| params = {"date": date_obj, "sourceId": 14, "jpip": True, "json": True} | ||
| params = getJP2ImageInputParameters(**params) | ||
| assert params.url == "https://api.helioviewer.org/v2/getJP2Image/" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import requests | ||
|
|
||
| from hvpy.io import HvpyParameters, OutputType | ||
|
|
||
|
|
||
| def parse_response(response: requests.Response, output_parameters: OutputType): | ||
nabobalis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Parses the response from the API call based on the output type. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| response : requests.Response | ||
| The response from the API call. | ||
| output_parameters : OutputType | ||
| The output type. | ||
|
|
||
| Returns | ||
| ------- | ||
| binary | str | json | ||
| The parsed response. | ||
| """ | ||
nabobalis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if output_parameters == OutputType.Raw: | ||
| return response.content | ||
| elif output_parameters == OutputType.String: | ||
| return response.content.decode("utf-8") | ||
| elif output_parameters == OutputType.Json: | ||
| return response.json() | ||
|
|
||
|
|
||
| def execute_api_call(input_parameters: HvpyParameters): | ||
| """ | ||
| Executes the API call and returns a parsed response. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| input_parameters : HvpyParameters | ||
| The input parameters. | ||
|
|
||
| Returns | ||
| ------- | ||
| Parsed response from the API. | ||
| """ | ||
|
|
||
| response = requests.get(input_parameters.url, params=input_parameters.dict()) | ||
| response.raise_for_status() | ||
nabobalis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return parse_response(response, input_parameters.get_output_type()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| from enum import Enum, auto | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| BASE_URL = "https://api.helioviewer.org/v2/" | ||
dgarciabriseno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| class HvpyParameters(BaseModel): | ||
| def dict(self): | ||
akash5100 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Pydantic doesn't allow using lowercase 'json' as a field, so we | ||
| override it. | ||
| """ | ||
| d = super().dict() | ||
| if "Json" in d: | ||
| d["json"] = d["Json"] | ||
| del d["Json"] | ||
| return d | ||
|
|
||
| def get_output_type(self): | ||
| return OutputType.Raw | ||
|
|
||
| @property | ||
| def url(self): | ||
akash5100 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return BASE_URL + self.__class__.__name__[:-15] + "/" | ||
|
|
||
|
|
||
| class OutputType(Enum): | ||
| Raw = auto() | ||
| String = auto() | ||
| Json = auto() | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| from hvpy.io import HvpyParameters, OutputType | ||
|
|
||
|
|
||
| def test_default_get_output_type_is_raw(): | ||
| params = HvpyParameters() | ||
| assert params.get_output_type() == OutputType.Raw | ||
|
|
||
|
|
||
| def test_url_property(): | ||
| params = HvpyParameters() | ||
| assert params.url == "https://api.helioviewer.org/v2//" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.