@@ -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 )
0 commit comments