Usage¶
Most users will only need to call careful.httpx.make_careful_client.
careful.httpx.make_careful_client
¶
This function patches an httpx.Client so that all requests made with the client support
retries, throttling, and development caching.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Client | None
|
A pre-configured |
None
|
retry_attempts
|
int
|
Maximum number of retries. If non-zero will retry up to this many times
with increasing wait times, starting with |
0
|
retry_wait_seconds
|
float
|
Number of seconds to sleep between first attempt and first retry. Subsequent attempts will increase exponentially (2x, 4x, 8x, etc.) |
10
|
should_retry
|
ResponsePredicate
|
Predicate function that takes a |
retry_default_rule
|
requests_per_minute
|
int
|
Maximum number of requests per minute. (e.g. 30 will throttle to ~2s between requests) |
0
|
cache_storage
|
CacheStorage | None
|
An object that implements the cache storage interface. |
None
|
cache_write_only
|
bool
|
Update cache, but never read from it. |
False
|
should_cache
|
ResponsePredicate
|
Predicate function that takes a |
_cache_200s
|
cache_keyfunc
|
CacheKeyfunc
|
Function that takes request details and returns a unique cache key. |
_default_keyfunc
|
Source code in src/careful/httpx/__init__.py
Throttling¶
If requests_per_minute is set, standard (non-retry) requests will automatically
sleep for a short period to target the given rate.
For example, at 30rpm, the sleep time on a fast request will be close to 2 seconds.
client = make_careful_client(requests_per_minute=20)
for page in range(10):
# will sleep ~3 seconds each time
client.get(f"https://example.com?page={page}")
Retries¶
If retry_attempts is set, responses will be passed to should_retry.
Responses that are rejected (return True) will be retried after a wait based on
retry_wait_seconds.
Each retry will wait twice as long as the one before.
client = make_careful_client(retry_attempts=2, retry_wait_seconds=30)
# will try, wait 30s, try again, wait 60s, try again, then give up & return the 500
client.get("https://httpbin.org/status/500")
TODO: should_retry
Development Caching¶
Why development caching?
This feature is named as a reminder that this is not true HTTP caching, which should take various headers into account. Look at libraries like hishel if that's what you are after.
The purpose of this feature is to allow you to cache all of your HTTP requests during development. Often when writing a scraper or crawler, you wind up hitting the site you are working on more often than you'd like-- each time you iterate on your code you're likely making redundant requests to pages that haven't changed.
By caching all successful requests (configurable with the should_cache parameter),
you can easily re-run scrapers without making redundant HTTP requests.
This means much faster development & happier upstream servers.
To enable development caching, assign a MemoryCache,
FileCache, or SqliteCache to
the cache_storage property of a scrapelib.Scraper.
client = make_careful_client(
cache_storage=FileStorage("_cache")
)
# only one HTTP request is made
client.get("https://example.com")
client.get("https://example.com")
client.get("https://example.com")
client.get("https://example.com")
# on subsequent runs, zero will be made until _cache is cleared
Multiple Enhancements¶
When multiple features are applied, the order of wrapping ensures that:
- the cache is checked first, and bypasses throttling if hit
- retries use their own delays, but are not throttled separately
Cache Storage Options¶
These options are available for cache_storage:
careful.httpx.MemoryCache
¶
Bases: CacheStorage
In memory cache for request responses.
Example:
make_careful_client(
cache_storage=MemoryCache(),
)
Source code in src/careful/httpx/dev_cache.py
careful.httpx.FileCache
¶
Bases: CacheStorage
File-based cache for request responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
str
|
directory for storing responses |
required |
Example:
make_careful_client(
cache_storage=FileCache("_httpcache/"),
)
Source code in src/careful/httpx/dev_cache.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
careful.httpx.SqliteCache
¶
Bases: CacheStorage
sqlite cache for request responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_path
|
str
|
path for SQLite database file |
required |
Example:
make_careful_client(
cache_storage=SQLiteCache("_cache.db"),
)
Source code in src/careful/httpx/dev_cache.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |