spn-client 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ __pycache__/
2
+ *.pyc
3
+ .pytest_cache/
4
+ dist/
5
+ build/
6
+ *.egg-info/
7
+ .venv/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mike Hammett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,79 @@
1
+ Metadata-Version: 2.5
2
+ Name: spn-client
3
+ Version: 0.1.0
4
+ Summary: A hardened client for archive.org's availability and Save Page Now (SPN2) APIs — process-wide rate pacing with a circuit breaker, dual-mode anonymous/S3-authenticated submission, and an explicit archive-outcome vocabulary.
5
+ Project-URL: Homepage, https://github.com/MHammett/spn-client
6
+ Project-URL: Issues, https://github.com/MHammett/spn-client/issues
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Requires-Python: >=3.10
10
+ Requires-Dist: requests<3.0,>=2.31.0
11
+ Description-Content-Type: text/markdown
12
+
13
+ # spn-client
14
+
15
+ A hardened Python client for archive.org's availability API and Save Page Now
16
+ (SPN2) — the pieces that are easy to get wrong when you actually run this
17
+ against archive.org at any volume:
18
+
19
+ - **Process-wide rate pacing with a circuit breaker.** archive.org throttles
20
+ per IP, not per thread or per endpoint. A naive per-thread backoff (or none
21
+ at all) looks fine in testing and then silently stops archiving anything
22
+ the first time it's run concurrently or against a busy queue.
23
+ - **Both submission modes.** Anonymous `GET /save/<url>` (capture runs inline,
24
+ answer comes back as a redirect) and S3-key-authenticated `POST /save`
25
+ (capture is queued, answer comes back as a `job_id` you poll for).
26
+ - **An explicit outcome vocabulary.** `submitted` is not `archived`. This
27
+ library only ever reports `archived: True` alongside a real snapshot URL
28
+ that came back from archive.org — never as a synonym for "we asked."
29
+ - **Staleness checking**, so callers can skip re-archiving a URL that already
30
+ has a recent-enough snapshot.
31
+
32
+ Every non-obvious piece of behavior in `client.py` is a documented response to
33
+ a specific, dated, measured incident against the real archive.org API — not
34
+ a guess.
35
+
36
+ ## Install
37
+
38
+ ```bash
39
+ pip install spn-client
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ ```python
45
+ import spn_client
46
+
47
+ result = spn_client.check("https://example.com/some-page")
48
+ if result["archived"] is False or result.get("snapshot_stale"):
49
+ submission = spn_client.submit(
50
+ "https://example.com/some-page",
51
+ access_key=ACCESS_KEY, # optional — omit for anonymous, lower-rate submission
52
+ secret_key=SECRET_KEY,
53
+ )
54
+ if submission["job_id"]:
55
+ # authenticated path: poll for the real outcome
56
+ status = spn_client.check_job_status(
57
+ submission["job_id"], access_key=ACCESS_KEY, secret_key=SECRET_KEY
58
+ )
59
+ ```
60
+
61
+ Call `spn_client.reset_rate_limit_state()` once at the start of each
62
+ independent run/process if you're running this as a long-lived worker —
63
+ the pacing/breaker state is process-wide and intentionally does not reset
64
+ itself, so a breaker tripped by one run would otherwise silently degrade
65
+ the next.
66
+
67
+ ## What this doesn't do
68
+
69
+ This is the archive.org client only — it has no opinion about:
70
+ - What staleness policy is right for your use case (`stale_days` is always a
71
+ caller-supplied parameter)
72
+ - How you track which URLs you've already archived (that's a caller-side
73
+ ledger/cache concern)
74
+ - Batch-loop concerns like a progress heartbeat or a wall-clock budget across
75
+ many submissions — those depend on your own operational needs
76
+
77
+ ## License
78
+
79
+ MIT
@@ -0,0 +1,67 @@
1
+ # spn-client
2
+
3
+ A hardened Python client for archive.org's availability API and Save Page Now
4
+ (SPN2) — the pieces that are easy to get wrong when you actually run this
5
+ against archive.org at any volume:
6
+
7
+ - **Process-wide rate pacing with a circuit breaker.** archive.org throttles
8
+ per IP, not per thread or per endpoint. A naive per-thread backoff (or none
9
+ at all) looks fine in testing and then silently stops archiving anything
10
+ the first time it's run concurrently or against a busy queue.
11
+ - **Both submission modes.** Anonymous `GET /save/<url>` (capture runs inline,
12
+ answer comes back as a redirect) and S3-key-authenticated `POST /save`
13
+ (capture is queued, answer comes back as a `job_id` you poll for).
14
+ - **An explicit outcome vocabulary.** `submitted` is not `archived`. This
15
+ library only ever reports `archived: True` alongside a real snapshot URL
16
+ that came back from archive.org — never as a synonym for "we asked."
17
+ - **Staleness checking**, so callers can skip re-archiving a URL that already
18
+ has a recent-enough snapshot.
19
+
20
+ Every non-obvious piece of behavior in `client.py` is a documented response to
21
+ a specific, dated, measured incident against the real archive.org API — not
22
+ a guess.
23
+
24
+ ## Install
25
+
26
+ ```bash
27
+ pip install spn-client
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ```python
33
+ import spn_client
34
+
35
+ result = spn_client.check("https://example.com/some-page")
36
+ if result["archived"] is False or result.get("snapshot_stale"):
37
+ submission = spn_client.submit(
38
+ "https://example.com/some-page",
39
+ access_key=ACCESS_KEY, # optional — omit for anonymous, lower-rate submission
40
+ secret_key=SECRET_KEY,
41
+ )
42
+ if submission["job_id"]:
43
+ # authenticated path: poll for the real outcome
44
+ status = spn_client.check_job_status(
45
+ submission["job_id"], access_key=ACCESS_KEY, secret_key=SECRET_KEY
46
+ )
47
+ ```
48
+
49
+ Call `spn_client.reset_rate_limit_state()` once at the start of each
50
+ independent run/process if you're running this as a long-lived worker —
51
+ the pacing/breaker state is process-wide and intentionally does not reset
52
+ itself, so a breaker tripped by one run would otherwise silently degrade
53
+ the next.
54
+
55
+ ## What this doesn't do
56
+
57
+ This is the archive.org client only — it has no opinion about:
58
+ - What staleness policy is right for your use case (`stale_days` is always a
59
+ caller-supplied parameter)
60
+ - How you track which URLs you've already archived (that's a caller-side
61
+ ledger/cache concern)
62
+ - Batch-loop concerns like a progress heartbeat or a wall-clock budget across
63
+ many submissions — those depend on your own operational needs
64
+
65
+ ## License
66
+
67
+ MIT
@@ -0,0 +1,30 @@
1
+ [project]
2
+ name = "spn-client"
3
+ version = "0.1.0"
4
+ description = "A hardened client for archive.org's availability and Save Page Now (SPN2) APIs — process-wide rate pacing with a circuit breaker, dual-mode anonymous/S3-authenticated submission, and an explicit archive-outcome vocabulary."
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ requires-python = ">=3.10"
8
+ dependencies = [
9
+ "requests>=2.31.0,<3.0",
10
+ ]
11
+
12
+ [project.urls]
13
+ Homepage = "https://github.com/MHammett/spn-client"
14
+ Issues = "https://github.com/MHammett/spn-client/issues"
15
+
16
+ [build-system]
17
+ requires = ["hatchling"]
18
+ build-backend = "hatchling.build"
19
+
20
+ [tool.hatch.build.targets.wheel]
21
+ packages = ["src/spn_client"]
22
+
23
+ [tool.pytest.ini_options]
24
+ testpaths = ["tests"]
25
+ addopts = "--disable-socket --allow-hosts=127.0.0.1,::1"
26
+
27
+ [tool.mypy]
28
+ python_version = "3.10"
29
+ strict = false
30
+ ignore_missing_imports = true
@@ -0,0 +1,43 @@
1
+ """spn-client: a hardened client for archive.org's availability and Save Page Now APIs."""
2
+
3
+ from spn_client.client import (
4
+ ARCHIVE_ARCHIVED,
5
+ ARCHIVE_CAPTURE_FAILED,
6
+ ARCHIVE_NOT_ATTEMPTED,
7
+ ARCHIVE_OUTCOME_LABELS,
8
+ ARCHIVE_PENDING,
9
+ ARCHIVE_SUBMIT_FAILED,
10
+ ARCHIVE_SUBMITTED,
11
+ DEFAULT_STALE_DAYS,
12
+ capture_capacity,
13
+ check,
14
+ check_job_status,
15
+ rate_limited_out,
16
+ reset_rate_limit_state,
17
+ service_health_note,
18
+ snapshot_raw_url,
19
+ submit,
20
+ system_status,
21
+ )
22
+
23
+ __version__ = "0.1.0"
24
+
25
+ __all__ = [
26
+ "ARCHIVE_ARCHIVED",
27
+ "ARCHIVE_CAPTURE_FAILED",
28
+ "ARCHIVE_NOT_ATTEMPTED",
29
+ "ARCHIVE_OUTCOME_LABELS",
30
+ "ARCHIVE_PENDING",
31
+ "ARCHIVE_SUBMIT_FAILED",
32
+ "ARCHIVE_SUBMITTED",
33
+ "DEFAULT_STALE_DAYS",
34
+ "capture_capacity",
35
+ "check",
36
+ "check_job_status",
37
+ "rate_limited_out",
38
+ "reset_rate_limit_state",
39
+ "service_health_note",
40
+ "snapshot_raw_url",
41
+ "submit",
42
+ "system_status",
43
+ ]
@@ -0,0 +1,22 @@
1
+ """Outbound-HTTP identity for spn-client.
2
+
3
+ A dedicated User-Agent, separate from any consuming project's own — this
4
+ package is meant to be embedded in several unrelated projects, and its
5
+ requests to archive.org should identify the library making them, not whatever
6
+ application happens to import it.
7
+ """
8
+
9
+ from importlib import metadata
10
+
11
+ try:
12
+ _VERSION = metadata.version("spn-client")
13
+ except metadata.PackageNotFoundError: # pragma: no cover - source/dev checkout
14
+ _VERSION = "0.0.0"
15
+
16
+ USER_AGENT = f"spn-client/{_VERSION}"
17
+
18
+ DEFAULT_HEADERS = {
19
+ "User-Agent": USER_AGENT,
20
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
21
+ "Accept-Language": "en-US,en;q=0.9",
22
+ }
@@ -0,0 +1,51 @@
1
+ """Secret-redaction helpers.
2
+
3
+ Vendored from a sister project's ``ci_core.redact`` module rather than taken
4
+ as a dependency on it — this package is deliberately decoupled from any
5
+ project-specific shared library, and these two functions are the only pieces
6
+ ``client.py`` needs.
7
+ """
8
+
9
+ import re
10
+
11
+ # The sensitive word a credential parameter ends with. Hyphenated spellings are
12
+ # not hypothetical: Azure OpenAI names its parameter `api-key` and Google sends
13
+ # `X-Goog-Api-Key`. An underscore-only list matched neither, nor
14
+ # `client_secret`, `refresh_token`, `subscription-key`, `password`, `auth` or
15
+ # `sig` — eight of twelve real-world spellings tested on 2026-09-04 passed
16
+ # straight through into whatever log or report the error text reached.
17
+ _SENSITIVE_WORD = (
18
+ r"(?:api[-_]?key|access[-_]?token|refresh[-_]?token|id[-_]?token"
19
+ r"|client[-_]?secret|subscription[-_]?key|session[-_]?key"
20
+ r"|key|token|secret|password|passwd|credentials?|auth|signature|sig)"
21
+ )
22
+
23
+ # Any dash/underscore-separated prefix, then that word, then `=`. Anchoring the
24
+ # end on `=` is what leaves innocent parameters alone: `author=` cannot match,
25
+ # because `auth` would have to consume the whole name and `or` is left over.
26
+ # `keywords=` survives for the same reason.
27
+ _PARAM_NAME = rf"(?:[A-Za-z0-9]+[-_])*{_SENSITIVE_WORD}"
28
+
29
+ # Matches ?key=..., &apiKey=..., &api-key=... in a URL and replaces the value.
30
+ # Stops at the next &, whitespace, quote, or closing bracket so we don't eat
31
+ # the rest of the message.
32
+ _KEY_QUERY_RE = re.compile(
33
+ rf"([?&]{_PARAM_NAME}=)[^&\s'\"<>)\]]+",
34
+ re.IGNORECASE,
35
+ )
36
+
37
+
38
+ def redact_url_keys(text):
39
+ """Replace key/token query-parameter values in any string with [REDACTED].
40
+
41
+ Provider-agnostic: works even when the key value isn't available to compare
42
+ against, because it matches on the parameter name.
43
+ """
44
+ return _KEY_QUERY_RE.sub(r"\1[REDACTED]", str(text))
45
+
46
+
47
+ def redact_value(text, secret):
48
+ """Replace a known secret value with [REDACTED] wherever it appears."""
49
+ if secret and secret in str(text):
50
+ return str(text).replace(secret, "[REDACTED]")
51
+ return str(text)