Query Open Library. Open Library always limits the result to 1000 items due to performance issues. Pass limit=False to fetch all matching results by making multiple requests to the server. Please note that an iterator is returned instead of list when limit=False is
(self, q=None, **kw)
| 172 | return self._call_write("new", query, comment, action) |
| 173 | |
| 174 | def query(self, q=None, **kw): |
| 175 | """Query Open Library. |
| 176 | |
| 177 | Open Library always limits the result to 1000 items due to |
| 178 | performance issues. Pass limit=False to fetch all matching |
| 179 | results by making multiple requests to the server. Please note |
| 180 | that an iterator is returned instead of list when limit=False is |
| 181 | passed.:: |
| 182 | |
| 183 | >>> ol.query({'type': '/type/type', 'limit': 2}) #doctest: +SKIP |
| 184 | [{'key': '/type/property'}, {'key': '/type/type'}] |
| 185 | |
| 186 | >>> ol.query(type='/type/type', limit=2) #doctest: +SKIP |
| 187 | [{'key': '/type/property'}, {'key': '/type/type'}] |
| 188 | """ |
| 189 | q = dict(q or {}) |
| 190 | q.update(kw) |
| 191 | q = marshal(q) |
| 192 | |
| 193 | def unlimited_query(q): |
| 194 | q["limit"] = 1000 |
| 195 | q.setdefault("offset", 0) |
| 196 | q.setdefault("sort", "key") |
| 197 | |
| 198 | while True: |
| 199 | result = self.query(q) |
| 200 | yield from result |
| 201 | if len(result) < 1000: |
| 202 | break |
| 203 | q["offset"] += len(result) |
| 204 | |
| 205 | if "limit" in q and q["limit"] is False: |
| 206 | return unlimited_query(q) |
| 207 | else: |
| 208 | response = self._request("/query.json", params={"query": json.dumps(q)}) |
| 209 | return unmarshal(response.json()) |
| 210 | |
| 211 | def search(self, query, limit=10, offset=0, fields: list[str] | None = None): |
| 212 | return self._request( |
no test coverage detected