Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
e2aa6c9
getjp2header image endpoint
akash5100 Jun 6, 2022
b018b05
use pydantic for i/o parameters
akash5100 Jun 7, 2022
d4946d9
Add dict function that renames Json to json
dgarciabriseno Jun 7, 2022
d228d5c
Remove print statement
akash5100 Jun 7, 2022
8d9e66c
Merge branch 'develop' into json-workaround
akash5100 Jun 8, 2022
f791895
Merge pull request #1 from dgarciabriseno/json-workaround
akash5100 Jun 8, 2022
cf48785
initial setup
akash5100 Jun 10, 2022
ef72ef4
Initialize the Base io
akash5100 Jun 14, 2022
d3595e3
Initialize the Base core
akash5100 Jun 14, 2022
a8e73ff
Replace string with OutputType
akash5100 Jun 15, 2022
8151856
Update hvpy/tests/test_get_jp2_image.py
akash5100 Jun 15, 2022
6e3b992
Update hvpy/tests/test_get_jp2_image.py
akash5100 Jun 15, 2022
e3d33fd
Update hvpy/core.py
akash5100 Jun 15, 2022
0c6842d
Update hvpy/core.py
akash5100 Jun 15, 2022
fe0dcb2
Update hvpy/core.py
akash5100 Jun 15, 2022
b39b3b4
Adds missing import
akash5100 Jun 15, 2022
3376820
Update hvpy/io.py
akash5100 Jun 15, 2022
20ec51f
Update hvpy/io.py
akash5100 Jun 15, 2022
388390f
isort fix
akash5100 Jun 15, 2022
7292e0c
isort fix
akash5100 Jun 15, 2022
d92def4
Update setup.cfg
Jun 15, 2022
a869baf
Merge branch 'main' of github.com:akash5100/python-api into main
akash5100 Jun 16, 2022
3138b3f
fix precommit failure
akash5100 Jun 17, 2022
9d105f2
Fix failing test
akash5100 Jun 17, 2022
739609a
Test raw and default response
akash5100 Jun 17, 2022
5669f7d
Merge branch 'main' into develop
akash5100 Jun 17, 2022
1e385f8
Precommit fixed
akash5100 Jun 18, 2022
0b23be1
Update hvpy/core.py
akash5100 Jun 18, 2022
e261c3b
Update hvpy/tests/test_get_jp2_image.py
akash5100 Jun 18, 2022
7727fab
Update hvpy/tests/test_get_jp2_image.py
akash5100 Jun 18, 2022
2d2f1b1
Update hvpy/io.py
akash5100 Jun 18, 2022
ff90d2c
Merge branch 'develop' of github.com:akash5100/python-api into develop
akash5100 Jun 18, 2022
1490305
Update hvpy/core.py
akash5100 Jun 18, 2022
51bc5aa
Add test and template docstring
akash5100 Jun 18, 2022
44c3e58
Fix merge conflict
akash5100 Jun 18, 2022
60a7349
Update io.py and core.py
akash5100 Jun 19, 2022
009d5da
Update docstring
akash5100 Jun 20, 2022
15009d4
Update docstring
akash5100 Jun 21, 2022
dd239c0
Update docstring
akash5100 Jun 22, 2022
21a7e0b
Update docstring
akash5100 Jun 22, 2022
d5f2c9b
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 Jun 22, 2022
b6146db
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 Jun 22, 2022
742ced1
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 Jun 22, 2022
765b043
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 Jun 22, 2022
ea00653
Update hvpy/api_groups/jpeg2000/get_jp2_image.py
akash5100 Jun 22, 2022
46dd707
Fix codestyle
akash5100 Jun 22, 2022
9f348d8
tests refactor
akash5100 Jun 23, 2022
40544f6
update docstring
akash5100 Jun 23, 2022
9d54697
Update get_jp2_image.py
akash5100 Jun 24, 2022
2084784
Remove Optional from getJP2Image endpoint
akash5100 Jun 25, 2022
95cba5a
Test for value error
akash5100 Jun 25, 2022
7c6c5b9
Fix alias for Json
akash5100 Jun 27, 2022
6582a1b
Check for error message
akash5100 Jun 28, 2022
3b76310
Test for IO module
akash5100 Jun 28, 2022
206f544
Update test and import endpoint to io.py
akash5100 Jun 29, 2022
81d592d
Update hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py
akash5100 Jun 29, 2022
a4b274b
Update test
akash5100 Jun 29, 2022
45983a8
Merge branch 'develop' of github.com:akash5100/python-api into develop
akash5100 Jun 29, 2022
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ ipython_config.py
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
Pipfile
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
Expand Down
1 change: 0 additions & 1 deletion hvpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
from .api import * # NOQA
from .version import __version__
13 changes: 0 additions & 13 deletions hvpy/api.py

This file was deleted.

Empty file added hvpy/api_groups/__init__.py
Empty file.
Empty file.
47 changes: 47 additions & 0 deletions hvpy/api_groups/jpeg2000/get_jp2_image.py
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.
73 changes: 73 additions & 0 deletions hvpy/api_groups/jpeg2000/tests/test_get_jp2_image.py
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}
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/"
47 changes: 47 additions & 0 deletions hvpy/core.py
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):
"""
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.
"""

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()
return parse_response(response, input_parameters.get_output_type())
31 changes: 31 additions & 0 deletions hvpy/io.py
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/"


class HvpyParameters(BaseModel):
def dict(self):
"""
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):
return BASE_URL + self.__class__.__name__[:-15] + "/"


class OutputType(Enum):
Raw = auto()
String = auto()
Json = auto()
9 changes: 0 additions & 9 deletions hvpy/tests/test_fake.py

This file was deleted.

11 changes: 11 additions & 0 deletions hvpy/tests/test_io.py
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//"
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ setup_requires =
setuptools_scm
install_requires =
requests>=2.27.0
pydantic>=1.9.1

[options.extras_require]
tests =
Expand Down