GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sdk-python/scripts/v2_subset.py at main · internetdata/sdk-python

Expand file tree

/

v2_subset.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
executable file
·
162 lines (138 loc) · 6.04 KB
/

v2_subset.py

Copy path

File metadata and controls

executable file
·
162 lines (138 loc) · 6.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
"""Narrows the pinned spec to what this client generates from, for codegen only.
python3 scripts/v2_subset.py spec/openapi.yaml /tmp/v2.yaml
`spec/openapi.yaml` is pinned VERBATIM so a refresh diffs against what the API actually
publishes, and that document carries `/api/v1/database/*` as well. v1 is a frozen legacy
contract held for a handful of named customers: a different credential (`?apikey=<uuid>`
rather than a bearer token), a different error vocabulary, and no `list`. Generating it
would ship dead modules and, worse, a second credential spelling that looks usable from
this client and is not.
Everything else is kept: the lookups, the entitlement, the v2 database, IAM and OAuth, as
the VPNDetection SDK generates them. IAM is generated and left unwrapped; the `oauth`
accessor sends its own requests and never calls the generated authorization endpoints.
So the narrowing happens here rather than in the pinned copy. Unreferenced components go
with the paths, or the generator emits models for v1 envelopes nothing can reach.
Uses ruamel.yaml, which openapi-python-client already depends on, so the codegen
virtualenv needs nothing extra.
"""
from __future__ import annotations
import sys
from typing import Any
from ruamel.yaml import YAML
KEEP_PATH_PREFIXES = (
"/api/v2/",
"/{ip}",
"/myip",
"/asn/",
"/domain/",
"/batch",
"/api/v1/entitlement",
"/api/v1/iam/",
"/oauth/",
"/.well-known/oauth-authorization-server",
)
# v1's `?apikey=` credential. Dropped with v1 itself: leaving it in the spec would put a
# second, unusable auth spelling in the generated client.
DROP_SECURITY_SCHEMES = ("ApiKeyAuth",)
# Every components section that a `$ref` can point at in this spec.
REF_SECTIONS = ("schemas", "parameters", "responses")
# The seen days spec 2026.11.23 gave `format: date`. The answer types hand them back as the
# served string, as they did before (a minor never changes what a member returns), and
# `models` checks each is an RFC 3339 full-date; generated as `date`, the decoder would read
# them with `isoparse`, which takes far more than a full-date. Each edit fails once the spec
# stops matching it, so a moved member stops generation.
SEEN_DAYS = (
("ClassDetail", "last_seen"),
("ProxyDetail", "first_seen"),
("ProxyDetail", "last_seen"),
("PubproxyDetail", "last_seen"),
("BotDetail", "first_seen"),
("BotDetail", "last_seen"),
("SshDetail", "first_seen"),
("SshDetail", "last_seen"),
("SmtpDetail", "first_seen"),
("SmtpDetail", "last_seen"),
("WebDetail", "first_seen"),
("WebDetail", "last_seen"),
("DnsResolverDetail", "first_seen"),
("DnsResolverDetail", "last_seen"),
("BitTorrentDetail", "first_seen"),
("BitTorrentDetail", "last_seen"),
("AsnPrefix", "first_seen"),
("DnsAddressRecord", "last_seen"),
("DnsMxRecord", "last_seen"),
("DnsNsRecord", "last_seen"),
("DnsCnameRecord", "last_seen"),
("DisposableDetail", "first_seen"),
("DisposableDetail", "last_seen"),
)
def main(source: str, destination: str) -> int:
yaml = YAML()
yaml.preserve_quotes = True
with open(source) as handle:
spec: dict[str, Any] = yaml.load(handle)
paths = spec.get("paths", {})
dropped = [path for path in paths if not path.startswith(KEEP_PATH_PREFIXES)]
for path in dropped:
del paths[path]
if not any(path.startswith(KEEP_PATH_PREFIXES[0]) for path in paths):
raise SystemExit(f"no {KEEP_PATH_PREFIXES[0]} paths survived, so the spec moved under us")
components = spec.get("components", {})
for scheme in DROP_SECURITY_SCHEMES:
components.get("securitySchemes", {}).pop(scheme, None)
# Every kept operation names its own security, so a document-level default could only
# name the dropped v1 scheme, which would leave the spec invalid.
spec.pop("security", None)
schemas = components.get("schemas", {})
for schema, member in SEEN_DAYS:
prop = schemas.get(schema, {}).get("properties", {}).get(member, {})
if prop.get("format") != "date":
raise SystemExit(f"{schema}.{member} is no `format: date` member, so the spec moved")
del prop["format"]
reachable = resolve_refs(spec, components)
for section in REF_SECTIONS:
entries = components.get(section)
if entries is None:
continue
for name in [name for name in entries if f"#/components/{section}/{name}" not in reachable]:
del entries[name]
if not entries:
del components[section]
with open(destination, "w") as handle:
yaml.dump(spec, handle)
print(f"{destination} <- {source} ({len(paths)} paths kept, dropped {len(dropped)})")
return 0
def resolve_refs(spec: dict[str, Any], components: dict[str, Any]) -> set[str]:
"""Every `#/components/...` a kept path can reach, following refs through refs.
A fixed point rather than one pass: `V2Unauthorized` is a response holding a ref to
the `Error` schema, so a single sweep would keep the response and prune the schema it
needs.
"""
reachable = refs_in(spec.get("paths", {}))
while True:
found = set(reachable)
for ref in reachable:
_, _, section, name = ref.split("/")
found |= refs_in(components.get(section, {}).get(name, {}))
if found == reachable:
return reachable
reachable = found
def refs_in(node: Any) -> set[str]:
if isinstance(node, dict):
found: set[str] = set()
for key, value in node.items():
if key == "$ref" and isinstance(value, str) and value.startswith("#/components/"):
found.add(value)
else:
found |= refs_in(value)
return found
if isinstance(node, list):
found = set()
for item in node:
found |= refs_in(item)
return found
return set()
if __name__ == "__main__":
if len(sys.argv) != 3:
raise SystemExit(f"usage: {sys.argv[0]} <source spec> <destination spec>")
sys.exit(main(sys.argv[1], sys.argv[2]))
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.