Skip to content
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

Switch the baseurl with set_api #41

Merged
merged 15 commits into from Jul 25, 2022
1 change: 1 addition & 0 deletions hvpy/__init__.py
@@ -1,2 +1,3 @@
from .facade import * # NOQA
from .io import set_api_url
from .version import __version__
17 changes: 17 additions & 0 deletions hvpy/api_groups/jpeg2000/tests/test_get_jp2_header.py
@@ -1,5 +1,8 @@
import os

import pytest

import hvpy
from hvpy import getJP2Header
from hvpy.api_groups.jpeg2000.get_jp2_header import getJP2HeaderInputParameters

Expand All @@ -23,3 +26,17 @@ def test_error_handling():
def test_url_property():
params = getJP2HeaderInputParameters(id=9838343)
assert params.url == "https://api.helioviewer.org/v2/getJP2Header/"


def test_set_api_url():
params = getJP2HeaderInputParameters(id=9838343)
assert params.url == "https://api.helioviewer.org/v2/getJP2Header/"

os.environ["HVPY_BASE_URL"] = "https://localhost:3000/"
params = getJP2HeaderInputParameters(id=9838343)
assert params.url == "https://localhost:3000/getJP2Header/"
os.environ.clear()

hvpy.set_api_url("https://api.beta.helioviewer.org/")
params = getJP2HeaderInputParameters(id=9838343)
assert params.url == "https://api.beta.helioviewer.org/getJP2Header/"
26 changes: 22 additions & 4 deletions hvpy/io.py
@@ -1,11 +1,10 @@
import os
from enum import Enum, auto
from typing import Any, Dict

from pydantic import BaseModel

__all__ = ["HvpyParameters", "OutputType"]

BASE_URL = "https://api.helioviewer.org/v2/"
akash5100 marked this conversation as resolved.
Show resolved Hide resolved
__all__ = ["HvpyParameters", "OutputType", "set_api_url"]


class OutputType(Enum):
Expand Down Expand Up @@ -54,4 +53,23 @@ def url(self) -> str:
"""
Final API endpoint URL.
"""
return BASE_URL + self.__class__.__name__[:-15] + "/"
base_url = get_api_url()
return base_url + self.__class__.__name__[:-15] + "/"
akash5100 marked this conversation as resolved.
Show resolved Hide resolved


def get_api_url() -> str:
"""
Returns the base URL for all API calls.
"""
akash5100 marked this conversation as resolved.
Show resolved Hide resolved
if "PRIVATE_URL" in os.environ:
akash5100 marked this conversation as resolved.
Show resolved Hide resolved
base_url = os.environ["PRIVATE_URL"]
del os.environ["PRIVATE_URL"]
return base_url
akash5100 marked this conversation as resolved.
Show resolved Hide resolved
return os.environ.get("HVPY_BASE_URL", default="https://api.helioviewer.org/v2/")


def set_api_url(private_url: str) -> None:
"""
Sets the base URL for all API calls.
"""
os.environ["PRIVATE_URL"] = private_url