Skip to content

Commit d6146aa

Browse files
committed
Merge branch 'migration' of https://github.com/geetu040/openml-python into runs-migration-stacked
2 parents c6351e0 + baa3a38 commit d6146aa

6 files changed

Lines changed: 103 additions & 163 deletions

File tree

openml/_api/clients/http.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,13 @@ def __init__( # noqa: PLR0913
116116
server: str,
117117
base_url: str,
118118
api_key: str,
119-
timeout_seconds: int,
120119
retries: int,
121120
retry_policy: RetryPolicy,
122121
cache: HTTPCache | None = None,
123122
) -> None:
124123
self.server = server
125124
self.base_url = base_url
126125
self.api_key = api_key
127-
self.timeout_seconds = timeout_seconds
128126
self.retries = retries
129127
self.retry_policy = retry_policy
130128
self.cache = cache
@@ -238,6 +236,8 @@ def _validate_response(
238236
raise OpenMLServerError(f"URI too long! ({url})")
239237

240238
retry_raise_e: Exception | None = None
239+
code: int | None = None
240+
message: str = ""
241241

242242
try:
243243
code, message = self._parse_exception_response(response)
@@ -276,26 +276,25 @@ def _validate_response(
276276

277277
def _request( # noqa: PLR0913
278278
self,
279+
session: requests.Session,
279280
method: str,
280281
url: str,
281282
params: Mapping[str, Any],
282283
data: Mapping[str, Any],
283284
headers: Mapping[str, str],
284-
timeout: float | int,
285285
files: Mapping[str, Any] | None,
286286
**request_kwargs: Any,
287287
) -> tuple[Response | None, Exception | None]:
288288
retry_raise_e: Exception | None = None
289289
response: Response | None = None
290290

291291
try:
292-
response = requests.request(
292+
response = session.request(
293293
method=method,
294294
url=url,
295295
params=params,
296296
data=data,
297297
headers=headers,
298-
timeout=timeout,
299298
files=files,
300299
**request_kwargs,
301300
)
@@ -343,7 +342,6 @@ def request(
343342
headers = request_kwargs.pop("headers", {}).copy()
344343
headers.update(self.headers)
345344

346-
timeout = request_kwargs.pop("timeout", self.timeout_seconds)
347345
files = request_kwargs.pop("files", None)
348346

349347
if use_cache and not reset_cache and self.cache is not None:
@@ -355,14 +353,15 @@ def request(
355353
except Exception:
356354
raise # propagate unexpected cache errors
357355

356+
session = requests.Session()
358357
for retry_counter in range(1, retries + 1):
359358
response, retry_raise_e = self._request(
359+
session=session,
360360
method=method,
361361
url=url,
362362
params=params,
363363
data=data,
364364
headers=headers,
365-
timeout=timeout,
366365
files=files,
367366
**request_kwargs,
368367
)
@@ -377,6 +376,8 @@ def request(
377376
delay = self.retry_func(retry_counter)
378377
time.sleep(delay)
379378

379+
session.close()
380+
380381
assert response is not None
381382

382383
if use_cache and self.cache is not None:

openml/_api/setup/builder.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ def build(cls, config: Config) -> APIBackendBuilder:
3939
server=primary_api_config.server,
4040
base_url=primary_api_config.base_url,
4141
api_key=primary_api_config.api_key,
42-
timeout_seconds=config.connection.timeout_seconds,
4342
retries=config.connection.retries,
4443
retry_policy=config.connection.retry_policy,
4544
cache=http_cache,
@@ -57,7 +56,6 @@ def build(cls, config: Config) -> APIBackendBuilder:
5756
server=fallback_api_config.server,
5857
base_url=fallback_api_config.base_url,
5958
api_key=fallback_api_config.api_key,
60-
timeout_seconds=config.connection.timeout_seconds,
6159
retries=config.connection.retries,
6260
retry_policy=config.connection.retry_policy,
6361
cache=http_cache,

openml/_api/setup/config.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class APIConfig:
1919
class ConnectionConfig:
2020
retries: int
2121
retry_policy: RetryPolicy
22-
timeout_seconds: int
2322

2423

2524
@dataclass
@@ -52,7 +51,6 @@ class Config:
5251
default_factory=lambda: ConnectionConfig(
5352
retries=5,
5453
retry_policy=RetryPolicy.HUMAN,
55-
timeout_seconds=10,
5654
)
5755
)
5856

openml/testing.py

Lines changed: 31 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@
1111
import unittest
1212
from pathlib import Path
1313
from typing import ClassVar
14-
from urllib.parse import urljoin
1514

1615
import requests
1716

1817
import openml
1918
from openml._api import HTTPCache, HTTPClient, MinIOClient
20-
from openml.enums import RetryPolicy
19+
from openml.enums import APIVersion, RetryPolicy
2120
from openml.exceptions import OpenMLServerException
2221
from openml.tasks import TaskType
2322

@@ -282,96 +281,42 @@ def _check_fold_timing_evaluations( # noqa: PLR0913
282281
assert evaluation <= max_val
283282

284283

285-
class TestAPIBase(unittest.TestCase):
286-
server: str
287-
base_url: str
288-
api_key: str
289-
timeout_seconds: int
290-
retries: int
291-
retry_policy: RetryPolicy
292-
dir: str
293-
ttl: int
284+
class TestAPIBase(TestBase):
294285
cache: HTTPCache
295-
http_client: HTTPClient
296-
297-
def setUp(self) -> None:
298-
self.server = "https://test.openml.org/"
299-
self.base_url = "api/v1/xml"
300-
self.api_key = "normaluser"
301-
self.timeout_seconds = 10
302-
self.retries = 3
303-
self.retry_policy = RetryPolicy.HUMAN
304-
self.dir = "test_cache"
305-
self.ttl = 60 * 60 * 24 * 7
306-
307-
self.cache = self._get_http_cache(
308-
path=Path(self.dir),
309-
ttl=self.ttl,
310-
)
311-
self.http_client = self._get_http_client(
312-
server=self.server,
313-
base_url=self.base_url,
314-
api_key=self.api_key,
315-
timeout_seconds=self.timeout_seconds,
316-
retries=self.retries,
317-
retry_policy=self.retry_policy,
318-
cache=self.cache,
319-
)
320-
self.minio_client = self._get_minio_client(path=Path(self.dir))
286+
http_clients: dict[APIVersion, HTTPClient]
287+
minio_client: MinIOClient
321288

322-
if self.cache.path.exists():
323-
shutil.rmtree(self.cache.path)
289+
def setUp(self, n_levels: int = 1, tmpdir_suffix: str = "") -> None:
290+
super().setUp(n_levels=n_levels, tmpdir_suffix=tmpdir_suffix)
324291

325-
def tearDown(self) -> None:
326-
if self.cache.path.exists():
327-
shutil.rmtree(self.cache.path)
292+
retries = self.connection_n_retries
293+
retry_policy = RetryPolicy.HUMAN if self.retry_policy == "human" else RetryPolicy.ROBOT
294+
ttl = openml._backend.get_config_value("cache.ttl")
295+
cache_dir = self.static_cache_dir
328296

329-
def _get_http_cache(
330-
self,
331-
path: Path,
332-
ttl: int,
333-
) -> HTTPCache:
334-
return HTTPCache(
335-
path=path,
297+
self.cache = HTTPCache(
298+
path=cache_dir,
336299
ttl=ttl,
337300
)
338-
339-
def _get_http_client( # noqa: PLR0913
340-
self,
341-
server: str,
342-
base_url: str,
343-
api_key: str,
344-
timeout_seconds: int,
345-
retries: int,
346-
retry_policy: RetryPolicy,
347-
cache: HTTPCache | None = None,
348-
) -> HTTPClient:
349-
return HTTPClient(
350-
server=server,
351-
base_url=base_url,
352-
api_key=api_key,
353-
timeout_seconds=timeout_seconds,
354-
retries=retries,
355-
retry_policy=retry_policy,
356-
cache=cache,
357-
)
358-
359-
def _get_minio_client(
360-
self,
361-
path: Path | None = None,
362-
) -> MinIOClient:
363-
return MinIOClient(path=path)
364-
365-
def _get_url(
366-
self,
367-
server: str | None = None,
368-
base_url: str | None = None,
369-
path: str | None = None,
370-
) -> str:
371-
server = server if server else self.server
372-
base_url = base_url if base_url else self.base_url
373-
path = path if path else ""
374-
return urljoin(self.server, urljoin(self.base_url, path))
301+
self.http_clients = {
302+
APIVersion.V1: HTTPClient(
303+
server="https://test.openml.org/",
304+
base_url="api/v1/xml/",
305+
api_key="normaluser",
306+
retries=retries,
307+
retry_policy=retry_policy,
308+
cache=self.cache,
309+
),
310+
APIVersion.V2: HTTPClient(
311+
server="http://localhost:8002/",
312+
base_url="",
313+
api_key="",
314+
retries=retries,
315+
retry_policy=retry_policy,
316+
cache=self.cache,
317+
),
318+
}
319+
self.minio_client = MinIOClient(path=cache_dir)
375320

376321

377322
def check_task_existence(

tests/test_api/test_http.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,25 @@
44
import pytest
55
from openml.testing import TestAPIBase
66
import os
7+
from urllib.parse import urljoin
8+
from openml.enums import APIVersion
9+
from openml._api import HTTPClient
710

811

912
class TestHTTPClient(TestAPIBase):
13+
http_client: HTTPClient
14+
15+
def setUp(self):
16+
super().setUp()
17+
self.http_client = self.http_clients[APIVersion.V1]
18+
19+
def _prepare_url(self, path: str | None = None) -> str:
20+
server = self.http_client.server
21+
base_url = self.http_client.base_url
22+
return urljoin(server, urljoin(base_url, path))
23+
1024
def test_cache(self):
11-
url = self._get_url(path="task/31")
25+
url = self._prepare_url(path="task/31")
1226
params = {"param1": "value1", "param2": "value2"}
1327

1428
key = self.cache.get_key(url, params)
@@ -18,6 +32,7 @@ def test_cache(self):
1832
"test",
1933
"api",
2034
"v1",
35+
"xml",
2136
"task",
2237
"31",
2338
"param1=value1&param2=value2",
@@ -68,7 +83,7 @@ def test_get_with_cache_creates_cache(self):
6883

6984
# verify cache directory structure exists
7085
cache_key = self.cache.get_key(
71-
self._get_url(path="task/1"),
86+
self._prepare_url(path="task/1"),
7287
{},
7388
)
7489
cache_path = self.cache._key_to_path(cache_key)
@@ -94,7 +109,7 @@ def test_get_cache_expires(self):
94109
self.cache.ttl = 1
95110
path = "task/1"
96111

97-
url = self._get_url(path=path)
112+
url = self._prepare_url(path=path)
98113
key = self.cache.get_key(url, {})
99114
cache_path = self.cache._key_to_path(key) / "meta.json"
100115

@@ -115,7 +130,7 @@ def test_get_cache_expires(self):
115130
def test_get_reset_cache(self):
116131
path = "task/1"
117132

118-
url = self._get_url(path=path)
133+
url = self._prepare_url(path=path)
119134
key = self.cache.get_key(url, {})
120135
cache_path = self.cache._key_to_path(key) / "meta.json"
121136

0 commit comments

Comments
 (0)