Skip to content

Commit 4af9cbe

Browse files
committed
Merge main resolve conflicts
2 parents ed35e69 + ec3ed63 commit 4af9cbe

9 files changed

Lines changed: 461 additions & 229 deletions

File tree

openml/_api/clients/http.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ def _key_to_path(self, key: str) -> Path:
8989
"""
9090
return self.path.joinpath(key)
9191

92+
def _get_body_filename_from_response(self, response: Response) -> str:
93+
content_type = response.headers.get("Content-Type", "").lower()
94+
95+
if "application/json" in content_type:
96+
return "body.json"
97+
98+
if "text/xml" in content_type:
99+
return "body.xml"
100+
101+
return "body.txt"
102+
103+
def _get_body_filename_from_path(self, path: Path) -> str:
104+
if (path / "body.json").exists():
105+
return "body.json"
106+
107+
if (path / "body.xml").exists():
108+
return "body.xml"
109+
110+
return "body.txt"
111+
92112
def load(self, key: str) -> Response:
93113
"""
94114
Load a cached HTTP response from disk.
@@ -112,31 +132,26 @@ def load(self, key: str) -> Response:
112132
"""
113133
path = self._key_to_path(key)
114134

115-
if not path.exists():
116-
raise FileNotFoundError(f"Cache entry not found: {path}")
117-
118135
meta_path = path / "meta.json"
119-
headers_path = path / "headers.json"
120-
body_path = path / "body.bin"
136+
meta_raw = meta_path.read_bytes() if meta_path.exists() else "{}"
137+
meta = json.loads(meta_raw)
121138

122-
if not (meta_path.exists() and headers_path.exists() and body_path.exists()):
123-
raise FileNotFoundError(f"Incomplete cache at {path}")
124-
125-
with meta_path.open("r", encoding="utf-8") as f:
126-
meta = json.load(f)
127-
128-
with headers_path.open("r", encoding="utf-8") as f:
129-
headers = json.load(f)
139+
headers_path = path / "headers.json"
140+
headers_raw = headers_path.read_bytes() if headers_path.exists() else "{}"
141+
headers = json.loads(headers_raw)
130142

143+
body_path = path / self._get_body_filename_from_path(path)
144+
if not body_path.exists():
145+
raise FileNotFoundError(f"Incomplete cache at {body_path}")
131146
body = body_path.read_bytes()
132147

133148
response = Response()
134-
response.status_code = meta["status_code"]
135-
response.url = meta["url"]
136-
response.reason = meta["reason"]
137149
response.headers = headers
138150
response._content = body
139-
response.encoding = meta["encoding"]
151+
response.status_code = meta.get("status_code")
152+
response.url = meta.get("url")
153+
response.reason = meta.get("reason")
154+
response.encoding = meta.get("encoding")
140155

141156
return response
142157

@@ -160,7 +175,9 @@ def save(self, key: str, response: Response) -> None:
160175
path = self._key_to_path(key)
161176
path.mkdir(parents=True, exist_ok=True)
162177

163-
(path / "body.bin").write_bytes(response.content)
178+
body_filename = self._get_body_filename_from_response(response)
179+
with (path / body_filename).open("wb") as f:
180+
f.write(response.content)
164181

165182
with (path / "headers.json").open("w", encoding="utf-8") as f:
166183
json.dump(dict(response.headers), f)

openml/_api/resources/base/resources.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import builtins
44
from abc import abstractmethod
5+
from collections.abc import Iterable
56
from pathlib import Path
67
from typing import TYPE_CHECKING, Any, Literal
78

@@ -15,6 +16,8 @@
1516

1617
if TYPE_CHECKING:
1718
from openml.evaluations import OpenMLEvaluation
19+
from openml.flows.flow import OpenMLFlow
20+
from openml.setups.setup import OpenMLSetup
1821

1922

2023
class DatasetAPI(ResourceAPI):
@@ -193,3 +196,24 @@ class SetupAPI(ResourceAPI):
193196
"""Abstract API interface for setup resources."""
194197

195198
resource_type: ResourceType = ResourceType.SETUP
199+
200+
@abstractmethod
201+
def list(
202+
self,
203+
limit: int,
204+
offset: int,
205+
*,
206+
setup: Iterable[int] | None = None,
207+
flow: int | None = None,
208+
tag: str | None = None,
209+
) -> list[OpenMLSetup]: ...
210+
211+
@abstractmethod
212+
def get(self, setup_id: int) -> OpenMLSetup: ...
213+
214+
@abstractmethod
215+
def exists(
216+
self,
217+
flow: OpenMLFlow,
218+
param_settings: builtins.list[dict[str, Any]],
219+
) -> int | bool: ...

0 commit comments

Comments
 (0)