Skip to content

Commit

Permalink
Merge pull request #12 from qiqb-osaka/feature/proxy
Browse files Browse the repository at this point in the history
Add optional proxy setting which can be set in riqu config.
  • Loading branch information
snuffkin authored Apr 10, 2024
2 parents 2998bc2 + 018c575 commit 998430c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion quri_parts/riqu/backend/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class RiquConfig:
ValueError: If ``url`` or ``api_token`` is None.
"""

def __init__(self, url: str, api_token: str) -> None:
def __init__(self, url: str, api_token: str, proxy: Optional[str] = None) -> None:
super().__init__()

if url is None:
Expand All @@ -375,6 +375,8 @@ def __init__(self, url: str, api_token: str) -> None:
raise ValueError("api_token should not be None.")
self._api_token: str = api_token

self._proxy: str = proxy

@property
def url(self) -> str:
return self._url
Expand All @@ -383,6 +385,10 @@ def url(self) -> str:
def api_token(self) -> str:
return self._api_token

@property
def proxy(self) -> Optional[str]:
return self._proxy

@staticmethod
def from_file(section: str = "default", path: str = "~/.riqu") -> "RiquConfig":
"""Reads configuration information from a file.
Expand Down Expand Up @@ -414,6 +420,7 @@ def from_file(section: str = "default", path: str = "~/.riqu") -> "RiquConfig":
[sectioB]
url=<base URL>
api_token=<API token>
proxy=http://<proxy>:<port>
If ``sectionA`` settings are to be used, initialize ``RiquSamplingBackend`` as follows
Expand All @@ -429,6 +436,7 @@ def from_file(section: str = "default", path: str = "~/.riqu") -> "RiquConfig":
config = RiquConfig(
url=parser[section]["url"],
api_token=parser[section]["api_token"],
proxy=parser[section].get("proxy", None)
)
return config

Expand All @@ -454,6 +462,8 @@ def __init__(
# construct JobApi
rest_config = Configuration()
rest_config.host = config.url
if config.proxy:
rest_config.proxy = config.proxy
api_client = ApiClient(
configuration=rest_config,
header_name="q-api-token",
Expand Down
22 changes: 21 additions & 1 deletion tests/riqu/backend/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
url=test_url
api_token=test_api_token
[option]
url=test_url
api_token=test_api_token
proxy=http://testproxy:port
[wrong]
url=test_url
"""
Expand Down Expand Up @@ -450,6 +455,7 @@ def test_from_file(self, mocker):
# Assert
assert actual.url == "default_url"
assert actual.api_token == "default_api_token"
assert actual.proxy is None

def test_from_file__section(self, mocker):
# Arrange
Expand All @@ -461,6 +467,19 @@ def test_from_file__section(self, mocker):
# Assert
assert actual.url == "test_url"
assert actual.api_token == "test_api_token"
assert actual.proxy is None

def test_from_file__optional(self, mocker):
# Arrange
mocker.patch("builtins.open", mock_open(read_data=config_file_data))

# Act
actual = RiquConfig.from_file(section="option")

# Assert
assert actual.url == "test_url"
assert actual.api_token == "test_api_token"
assert actual.proxy == "http://testproxy:port"

def test_from_file__wrong(self, mocker):
# Arrange
Expand All @@ -478,11 +497,12 @@ def test_from_file__wrong(self, mocker):

def test_properties(self):
# Act
actual = RiquConfig("dummpy_url", "dummy_api_token")
actual = RiquConfig("dummpy_url", "dummy_api_token", "http://dummy:1234")

# Assert
assert actual.url == "dummpy_url"
assert actual.api_token == "dummy_api_token"
assert actual.proxy == "http://dummy:1234"


class TestRiquSamplingBackend:
Expand Down

0 comments on commit 998430c

Please sign in to comment.