api-everything 0.5.0__py3-none-any.whl
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.
- api_everything/__init__.py +3 -0
- api_everything/__main__.py +8 -0
- api_everything/check.py +168 -0
- api_everything/chrome_cmd.py +131 -0
- api_everything/cli.py +803 -0
- api_everything/config.py +321 -0
- api_everything/delivery.py +518 -0
- api_everything/delivery_git.py +94 -0
- api_everything/driver/__init__.py +37 -0
- api_everything/driver/attach.py +95 -0
- api_everything/driver/base.py +80 -0
- api_everything/driver/chrome.py +127 -0
- api_everything/driver/profile.py +84 -0
- api_everything/engine.py +381 -0
- api_everything/errors.py +23 -0
- api_everything/hooks.py +52 -0
- api_everything/login.py +95 -0
- api_everything/mcp_bridge.py +249 -0
- api_everything/metrics.py +87 -0
- api_everything/mock_site/__init__.py +5 -0
- api_everything/mock_site/__main__.py +18 -0
- api_everything/mock_site/app.py +262 -0
- api_everything/pipeline.py +934 -0
- api_everything/pool.py +553 -0
- api_everything/ratelimit.py +67 -0
- api_everything/recipe/__init__.py +6 -0
- api_everything/recipe/actions.py +56 -0
- api_everything/recipe/jsonpath.py +22 -0
- api_everything/recipe/loader.py +142 -0
- api_everything/recipe/params.py +120 -0
- api_everything/recipe/schema.py +235 -0
- api_everything/recipes/alignmentforum/feed.yaml +15 -0
- api_everything/recipes/alpine_docs/search.yaml +32 -0
- api_everything/recipes/angular_docs/search.yaml +32 -0
- api_everything/recipes/anthropic_status/summary.yaml +17 -0
- api_everything/recipes/apis_guru/providers.yaml +15 -0
- api_everything/recipes/arbeitnow/jobs.yaml +20 -0
- api_everything/recipes/arin_rdap/ip.yaml +24 -0
- api_everything/recipes/artifacthub/browse.yaml +22 -0
- api_everything/recipes/artifacthub/search.yaml +24 -0
- api_everything/recipes/astro_docs/search.yaml +32 -0
- api_everything/recipes/atlassian_status/summary.yaml +17 -0
- api_everything/recipes/babel_docs/search.yaml +32 -0
- api_everything/recipes/binance/price.yaml +24 -0
- api_everything/recipes/bugzilla/bugs.yaml +24 -0
- api_everything/recipes/bundlephobia/size.yaml +24 -0
- api_everything/recipes/caddy_docs/search.yaml +32 -0
- api_everything/recipes/caniuse/search.yaml +24 -0
- api_everything/recipes/capacitor_docs/search.yaml +32 -0
- api_everything/recipes/cdnjs/library.yaml +22 -0
- api_everything/recipes/cdnjs/search.yaml +27 -0
- api_everything/recipes/certspotter/issuances.yaml +27 -0
- api_everything/recipes/cheerio_docs/search.yaml +32 -0
- api_everything/recipes/circleci_status/summary.yaml +17 -0
- api_everything/recipes/cisa_kev/catalog.yaml +17 -0
- api_everything/recipes/civitai/models.yaml +26 -0
- api_everything/recipes/cloudflare_status/summary.yaml +17 -0
- api_everything/recipes/cncf_landscape/projects.yaml +20 -0
- api_everything/recipes/cncf_landscape/ui.yaml +18 -0
- api_everything/recipes/coinbase/rates.yaml +24 -0
- api_everything/recipes/coingecko/trending.yaml +17 -0
- api_everything/recipes/conda_forge/package.yaml +25 -0
- api_everything/recipes/crates/categories.yaml +15 -0
- api_everything/recipes/crates/downloads.yaml +22 -0
- api_everything/recipes/crates/keywords.yaml +15 -0
- api_everything/recipes/crates/reverse_deps.yaml +22 -0
- api_everything/recipes/crates/search.yaml +24 -0
- api_everything/recipes/crates/summary.yaml +15 -0
- api_everything/recipes/crates/user.yaml +22 -0
- api_everything/recipes/crates/versions.yaml +22 -0
- api_everything/recipes/crossref/works.yaml +24 -0
- api_everything/recipes/cve_org/record.yaml +24 -0
- api_everything/recipes/debian_sources/search.yaml +24 -0
- api_everything/recipes/deps_dev/npm_spa.yaml +22 -0
- api_everything/recipes/deps_dev/package.yaml +23 -0
- api_everything/recipes/devdocs/index.yaml +18 -0
- api_everything/recipes/devto/articles.yaml +26 -0
- api_everything/recipes/devto/latest.yaml +18 -0
- api_everything/recipes/devto/tags.yaml +18 -0
- api_everything/recipes/devto/top.yaml +18 -0
- api_everything/recipes/digitalocean_status/summary.yaml +17 -0
- api_everything/recipes/digitalocean_status/ui.yaml +15 -0
- api_everything/recipes/discord_status/summary.yaml +17 -0
- api_everything/recipes/dns_google/resolve.yaml +24 -0
- api_everything/recipes/drizzle_docs/search.yaml +32 -0
- api_everything/recipes/echojs/latest.yaml +17 -0
- api_everything/recipes/ecosystems/package.yaml +25 -0
- api_everything/recipes/endoflife/product.yaml +24 -0
- api_everything/recipes/epss/score.yaml +24 -0
- api_everything/recipes/exchangerate/latest.yaml +24 -0
- api_everything/recipes/firefox_addons/search.yaml +26 -0
- api_everything/recipes/fly_status/summary.yaml +17 -0
- api_everything/recipes/formik_docs/search.yaml +32 -0
- api_everything/recipes/frankfurter/rates.yaml +24 -0
- api_everything/recipes/gcp_status/incidents.yaml +20 -0
- api_everything/recipes/github/explore.yaml +35 -0
- api_everything/recipes/github/gists.yaml +18 -0
- api_everything/recipes/github/issues.yaml +30 -0
- api_everything/recipes/github/license.yaml +23 -0
- api_everything/recipes/github/org.yaml +22 -0
- api_everything/recipes/github/releases.yaml +30 -0
- api_everything/recipes/github/repo.yaml +28 -0
- api_everything/recipes/github/user.yaml +22 -0
- api_everything/recipes/github_advisories/list.yaml +20 -0
- api_everything/recipes/github_status/summary.yaml +17 -0
- api_everything/recipes/github_status/ui.yaml +15 -0
- api_everything/recipes/gitlab/issues.yaml +24 -0
- api_everything/recipes/gitlab/repo.yaml +21 -0
- api_everything/recipes/gitlab/search.yaml +26 -0
- api_everything/recipes/go_proxy/latest.yaml +24 -0
- api_everything/recipes/grafana_status/summary.yaml +17 -0
- api_everything/recipes/hackage/versions.yaml +24 -0
- api_everything/recipes/hackernews/ask.yaml +18 -0
- api_everything/recipes/hackernews/front_page.yaml +18 -0
- api_everything/recipes/hackernews/item.yaml +21 -0
- api_everything/recipes/hackernews/jobs.yaml +18 -0
- api_everything/recipes/hackernews/latest.yaml +25 -0
- api_everything/recipes/hackernews/search.yaml +34 -0
- api_everything/recipes/hackernews/show.yaml +18 -0
- api_everything/recipes/hashicorp_releases/list.yaml +27 -0
- api_everything/recipes/hashicorp_status/summary.yaml +17 -0
- api_everything/recipes/helm_docs/search.yaml +32 -0
- api_everything/recipes/heroku_status/current.yaml +17 -0
- api_everything/recipes/hexpm/search.yaml +27 -0
- api_everything/recipes/hf_papers/daily.yaml +20 -0
- api_everything/recipes/himalayas/jobs.yaml +17 -0
- api_everything/recipes/homebrew/cask.yaml +22 -0
- api_everything/recipes/homebrew/formula.yaml +26 -0
- api_everything/recipes/hono_docs/search.yaml +32 -0
- api_everything/recipes/httpbin/json.yaml +17 -0
- api_everything/recipes/httpbin/uuid.yaml +15 -0
- api_everything/recipes/huggingface/datasets.yaml +25 -0
- api_everything/recipes/huggingface/model_info.yaml +27 -0
- api_everything/recipes/huggingface/models.yaml +28 -0
- api_everything/recipes/huggingface/spaces.yaml +25 -0
- api_everything/recipes/inspirehep/papers.yaml +24 -0
- api_everything/recipes/ionic_docs/search.yaml +32 -0
- api_everything/recipes/ipify/ipv4.yaml +17 -0
- api_everything/recipes/ipwho/lookup.yaml +24 -0
- api_everything/recipes/jest_docs/search.yaml +32 -0
- api_everything/recipes/jsdelivr/gh.yaml +27 -0
- api_everything/recipes/jsdelivr/npm.yaml +24 -0
- api_everything/recipes/jsr/search.yaml +27 -0
- api_everything/recipes/launchdarkly_status/summary.yaml +17 -0
- api_everything/recipes/lemmy/hot.yaml +17 -0
- api_everything/recipes/lesswrong/feed.yaml +17 -0
- api_everything/recipes/llamaindex_docs/search.yaml +32 -0
- api_everything/recipes/lobsters/hottest.yaml +20 -0
- api_everything/recipes/luma/category.yaml +22 -0
- api_everything/recipes/luma/events.yaml +17 -0
- api_everything/recipes/mastodon/explore.yaml +26 -0
- api_everything/recipes/mastodon/tag.yaml +23 -0
- api_everything/recipes/mastodon/trends_tags.yaml +18 -0
- api_everything/recipes/mdn/search.yaml +24 -0
- api_everything/recipes/metacpan/search.yaml +24 -0
- api_everything/recipes/microlink/preview.yaml +24 -0
- api_everything/recipes/mikro_orm_docs/search.yaml +32 -0
- api_everything/recipes/mock/items.yaml +35 -0
- api_everything/recipes/mongodb_status/summary.yaml +17 -0
- api_everything/recipes/nager_date/holidays.yaml +32 -0
- api_everything/recipes/neon_docs/search.yaml +32 -0
- api_everything/recipes/nestjs_docs/search.yaml +32 -0
- api_everything/recipes/netlify_status/summary.yaml +17 -0
- api_everything/recipes/nextjs_docs/search.yaml +32 -0
- api_everything/recipes/nodejs_dist/releases.yaml +17 -0
- api_everything/recipes/npm/dist_tags.yaml +22 -0
- api_everything/recipes/npm/package.yaml +25 -0
- api_everything/recipes/npm/search.yaml +25 -0
- api_everything/recipes/npm_downloads/weekly.yaml +24 -0
- api_everything/recipes/npm_status/summary.yaml +17 -0
- api_everything/recipes/npmtrends/compare.yaml +22 -0
- api_everything/recipes/nuget/search.yaml +27 -0
- api_everything/recipes/nuxt_docs/search.yaml +32 -0
- api_everything/recipes/nvd/cve.yaml +22 -0
- api_everything/recipes/nvd/cves.yaml +24 -0
- api_everything/recipes/open_elevation/lookup.yaml +29 -0
- api_everything/recipes/openai_status/summary.yaml +17 -0
- api_everything/recipes/opencollective/search.yaml +22 -0
- api_everything/recipes/openfda/events.yaml +17 -0
- api_everything/recipes/openlibrary/search.yaml +24 -0
- api_everything/recipes/openmeteo/forecast.yaml +17 -0
- api_everything/recipes/openrouter/models.yaml +20 -0
- api_everything/recipes/openstreetmap/geocode.yaml +27 -0
- api_everything/recipes/openstreetmap/reverse.yaml +27 -0
- api_everything/recipes/osv/vuln.yaml +24 -0
- api_everything/recipes/packagist/search.yaml +27 -0
- api_everything/recipes/peps/list.yaml +17 -0
- api_everything/recipes/pinia_docs/search.yaml +32 -0
- api_everything/recipes/pkg_go_dev/browse.yaml +15 -0
- api_everything/recipes/pkg_go_dev/package.yaml +25 -0
- api_everything/recipes/playwright_docs/search.yaml +32 -0
- api_everything/recipes/pnpm_docs/search.yaml +32 -0
- api_everything/recipes/pokeapi/pokemon.yaml +24 -0
- api_everything/recipes/posthog_docs/search.yaml +32 -0
- api_everything/recipes/postman_echo/get.yaml +24 -0
- api_everything/recipes/postman_status/summary.yaml +17 -0
- api_everything/recipes/preact_docs/search.yaml +32 -0
- api_everything/recipes/pubdev/search.yaml +24 -0
- api_everything/recipes/pubmed/search.yaml +24 -0
- api_everything/recipes/puppeteer_docs/search.yaml +32 -0
- api_everything/recipes/pypi/package.yaml +24 -0
- api_everything/recipes/pypi/version.yaml +27 -0
- api_everything/recipes/quay/search.yaml +27 -0
- api_everything/recipes/quora/answers.yaml +46 -0
- api_everything/recipes/qwik_docs/search.yaml +32 -0
- api_everything/recipes/ray_docs/search.yaml +32 -0
- api_everything/recipes/rdap/domain.yaml +24 -0
- api_everything/recipes/react_docs/search.yaml +32 -0
- api_everything/recipes/react_hook_form_docs/search.yaml +32 -0
- api_everything/recipes/reactrouter_docs/search.yaml +32 -0
- api_everything/recipes/reddit/search.yaml +48 -0
- api_everything/recipes/reddit_status/summary.yaml +17 -0
- api_everything/recipes/remoteok/jobs.yaml +17 -0
- api_everything/recipes/render_status/summary.yaml +17 -0
- api_everything/recipes/repology/project.yaml +24 -0
- api_everything/recipes/ripe_stat/prefix.yaml +24 -0
- api_everything/recipes/rollup_docs/search.yaml +32 -0
- api_everything/recipes/rsbuild_docs/search.yaml +32 -0
- api_everything/recipes/rubygems/gem.yaml +22 -0
- api_everything/recipes/rubygems/search.yaml +27 -0
- api_everything/recipes/sendgrid_status/summary.yaml +17 -0
- api_everything/recipes/sequelize_docs/search.yaml +32 -0
- api_everything/recipes/shodan_internetdb/host.yaml +24 -0
- api_everything/recipes/shopify_status/summary.yaml +17 -0
- api_everything/recipes/sigstore_rekor/log.yaml +17 -0
- api_everything/recipes/slack_status/current.yaml +17 -0
- api_everything/recipes/spdx/licenses.yaml +17 -0
- api_everything/recipes/stackexchange/sites.yaml +20 -0
- api_everything/recipes/stackoverflow/answers.yaml +15 -0
- api_everything/recipes/stackoverflow/questions.yaml +18 -0
- api_everything/recipes/stackoverflow/search.yaml +27 -0
- api_everything/recipes/stackoverflow/tags.yaml +18 -0
- api_everything/recipes/strapi_docs/search.yaml +32 -0
- api_everything/recipes/strawberry_docs/search.yaml +32 -0
- api_everything/recipes/stripe_status/current.yaml +17 -0
- api_everything/recipes/substack/explore.yaml +22 -0
- api_everything/recipes/substack/search.yaml +31 -0
- api_everything/recipes/sunrise_sunset/times.yaml +29 -0
- api_everything/recipes/supabase_status/summary.yaml +17 -0
- api_everything/recipes/swr_docs/search.yaml +32 -0
- api_everything/recipes/tanstack_docs/search.yaml +32 -0
- api_everything/recipes/tc39/proposals.yaml +20 -0
- api_everything/recipes/techempower/results.yaml +15 -0
- api_everything/recipes/temporal_docs/search.yaml +32 -0
- api_everything/recipes/terraform_registry/browse.yaml +25 -0
- api_everything/recipes/terraform_registry/search.yaml +24 -0
- api_everything/recipes/testing_library_docs/search.yaml +32 -0
- api_everything/recipes/tvmaze/shows.yaml +24 -0
- api_everything/recipes/twilio_status/summary.yaml +17 -0
- api_everything/recipes/upstash_docs/search.yaml +32 -0
- api_everything/recipes/valibot_docs/search.yaml +32 -0
- api_everything/recipes/vercel_status/summary.yaml +17 -0
- api_everything/recipes/vercel_status/ui.yaml +15 -0
- api_everything/recipes/vue_docs/search.yaml +32 -0
- api_everything/recipes/w3c/specs.yaml +17 -0
- api_everything/recipes/wayback/available.yaml +24 -0
- api_everything/recipes/webdriverio_docs/search.yaml +32 -0
- api_everything/recipes/webpack_docs/search.yaml +32 -0
- api_everything/recipes/wikidata/entity.yaml +22 -0
- api_everything/recipes/wikidata/search.yaml +24 -0
- api_everything/recipes/wikimedia_commons/search.yaml +25 -0
- api_everything/recipes/wikipedia/search.yaml +25 -0
- api_everything/recipes/wikipedia/summary.yaml +33 -0
- api_everything/recipes/wordpress_plugins/search.yaml +24 -0
- api_everything/recipes/wordpress_themes/search.yaml +24 -0
- api_everything/recipes/worldbank/indicator.yaml +17 -0
- api_everything/recipes/zenodo/records.yaml +24 -0
- api_everything/recipes/zippopotam/in.yaml +22 -0
- api_everything/recipes/zippopotam/lookup.yaml +29 -0
- api_everything/recipes/zoom_status/summary.yaml +17 -0
- api_everything/record.py +327 -0
- api_everything/record_session.py +364 -0
- api_everything/repair.py +717 -0
- api_everything/repair_ai.py +194 -0
- api_everything/runlog.py +61 -0
- api_everything/server.py +323 -0
- api_everything-0.5.0.dist-info/METADATA +363 -0
- api_everything-0.5.0.dist-info/RECORD +281 -0
- api_everything-0.5.0.dist-info/WHEEL +4 -0
- api_everything-0.5.0.dist-info/entry_points.txt +2 -0
- api_everything-0.5.0.dist-info/licenses/LICENSE +202 -0
api_everything/config.py
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
"""Daemon configuration: defaults, a YAML file, environment overrides and the API token.
|
|
2
|
+
|
|
3
|
+
Precedence, highest first: environment variables, then the YAML file, then the built-in
|
|
4
|
+
defaults. A first run works with no config at all.
|
|
5
|
+
|
|
6
|
+
The API token is deliberately different: it is read only from the environment
|
|
7
|
+
(`API_EVERYTHING_TOKEN`, or a file named by `API_EVERYTHING_TOKEN_FILE`) and is rejected if
|
|
8
|
+
it appears in the YAML file, so it cannot be committed by accident.
|
|
9
|
+
|
|
10
|
+
Any single value can be overridden with `API_EVERYTHING__<SECTION>__<KEY>`, for example
|
|
11
|
+
`API_EVERYTHING__POOL__NONE_SIZE=20`. Per-domain rate limits contain dots in their keys, so
|
|
12
|
+
they are set in the YAML file only.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import ipaddress
|
|
18
|
+
import os
|
|
19
|
+
import re
|
|
20
|
+
from collections.abc import Mapping
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
from typing import Any, Literal
|
|
23
|
+
from urllib.parse import urlsplit
|
|
24
|
+
|
|
25
|
+
import yaml
|
|
26
|
+
from pydantic import BaseModel, ConfigDict, Field, SecretStr, ValidationError, model_validator
|
|
27
|
+
|
|
28
|
+
ENV_PREFIX = "API_EVERYTHING__"
|
|
29
|
+
TOKEN_ENV = "API_EVERYTHING_TOKEN"
|
|
30
|
+
TOKEN_FILE_ENV = "API_EVERYTHING_TOKEN_FILE"
|
|
31
|
+
|
|
32
|
+
# Profile names become directory names, so they must not be able to escape the profiles dir.
|
|
33
|
+
_PROFILE_NAME = re.compile(r"^[a-z0-9][a-z0-9_-]{0,63}$")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class ConfigError(Exception):
|
|
37
|
+
"""The configuration is invalid. The message says what to fix."""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class _Section(BaseModel):
|
|
41
|
+
# Unknown keys are errors, so a typo like `queue_dept` fails loudly instead of being ignored.
|
|
42
|
+
model_config = ConfigDict(extra="forbid")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class ServerConfig(_Section):
|
|
46
|
+
host: str = "127.0.0.1"
|
|
47
|
+
port: int = Field(default=8750, ge=1, le=65535)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
DEFAULT_RECIPE_SOURCE = "https://github.com/Abhinay-kal/api-everything-recipes.git"
|
|
51
|
+
|
|
52
|
+
# `git clone` URL forms we accept. Anything else (notably `ext::` and `fd::`, which make git run
|
|
53
|
+
# a command) is refused, and a leading `-` could be read as an option.
|
|
54
|
+
_GIT_URL = re.compile(
|
|
55
|
+
r"^(https://[^\s]+|ssh://[^\s]+|git@[A-Za-z0-9._-]+:[^\s]+|file:///[^\s]+|/[^\s]+|~/[^\s]+)$"
|
|
56
|
+
)
|
|
57
|
+
_GIT_REF = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._/-]{0,100}$")
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
class RecipeSource(_Section):
|
|
61
|
+
"""A git repository `api-everything recipes pull` fetches shared recipes from.
|
|
62
|
+
|
|
63
|
+
The repository holds `recipes/<site>/<endpoint>.yaml`. `ref` is a branch or tag.
|
|
64
|
+
"""
|
|
65
|
+
|
|
66
|
+
git: str
|
|
67
|
+
ref: str = "main"
|
|
68
|
+
|
|
69
|
+
@model_validator(mode="after")
|
|
70
|
+
def _safe(self) -> RecipeSource:
|
|
71
|
+
if not _GIT_URL.match(self.git) or self.git.startswith("-"):
|
|
72
|
+
raise ValueError(
|
|
73
|
+
f"git must be an https://, ssh://, git@host:path, file:// or absolute path "
|
|
74
|
+
f"address, got {self.git!r}"
|
|
75
|
+
)
|
|
76
|
+
if not _GIT_REF.match(self.ref) or ".." in self.ref:
|
|
77
|
+
raise ValueError(f"ref must be a branch or tag name, got {self.ref!r}")
|
|
78
|
+
return self
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class AttachProfile(_Section):
|
|
82
|
+
"""A profile served by a Chrome you started yourself (`api-everything chrome <name>`).
|
|
83
|
+
|
|
84
|
+
The daemon attaches to it over the Chrome DevTools Protocol instead of launching Chrome.
|
|
85
|
+
Anything that can reach that port can control the browser, so only this machine is allowed.
|
|
86
|
+
"""
|
|
87
|
+
|
|
88
|
+
attach: str
|
|
89
|
+
|
|
90
|
+
@model_validator(mode="after")
|
|
91
|
+
def _loopback_http_with_port(self) -> AttachProfile:
|
|
92
|
+
parts = urlsplit(self.attach)
|
|
93
|
+
try:
|
|
94
|
+
port = parts.port
|
|
95
|
+
except ValueError:
|
|
96
|
+
port = None
|
|
97
|
+
ok = (
|
|
98
|
+
parts.scheme == "http"
|
|
99
|
+
and parts.hostname is not None
|
|
100
|
+
and _is_loopback(parts.hostname)
|
|
101
|
+
and port is not None
|
|
102
|
+
and parts.path in ("", "/")
|
|
103
|
+
and not parts.query
|
|
104
|
+
and not parts.fragment
|
|
105
|
+
)
|
|
106
|
+
if not ok:
|
|
107
|
+
raise ValueError(
|
|
108
|
+
f"attach must look like http://127.0.0.1:9222 (this machine only, with a "
|
|
109
|
+
f"port), got {self.attach!r}"
|
|
110
|
+
)
|
|
111
|
+
return self
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def port(self) -> int:
|
|
115
|
+
return urlsplit(self.attach).port or 0
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def url(self) -> str:
|
|
119
|
+
return self.attach.rstrip("/")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
class PoolConfig(_Section):
|
|
123
|
+
# `none` session mode: isolated contexts in one non-persistent Chrome.
|
|
124
|
+
none_size: int = Field(default=10, ge=1)
|
|
125
|
+
queue_depth: int = Field(default=20, ge=0)
|
|
126
|
+
# `profile:<name>` session mode: tabs in one persistent-profile Chrome, one at a time.
|
|
127
|
+
profile_concurrency: int = Field(default=1, ge=1)
|
|
128
|
+
# Isolated contexts, each with a blank page already open, kept ready so a run does not pay
|
|
129
|
+
# to create one (about 200 to 700 ms). Each is used once and discarded, so isolation is
|
|
130
|
+
# unchanged. Each costs memory (roughly a browser tab); 0 turns pre-warming off. They are
|
|
131
|
+
# opened only while a Chrome is idle, never while its runs are in flight.
|
|
132
|
+
warm_leases: int = Field(default=2, ge=0)
|
|
133
|
+
# Each Chrome is replaced after this many runs or minutes (drained first).
|
|
134
|
+
recycle_after_runs: int = Field(default=200, ge=1)
|
|
135
|
+
recycle_after_minutes: int = Field(default=60, ge=1)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class DeadlineConfig(_Section):
|
|
139
|
+
default_seconds: float = Field(default=30.0, gt=0)
|
|
140
|
+
max_seconds: float = Field(default=60.0, gt=0)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
class LimitsConfig(_Section):
|
|
144
|
+
max_body_bytes: int = Field(default=5 * 1024 * 1024, ge=1)
|
|
145
|
+
max_collect_count: int = Field(default=50, ge=1)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class RateLimitConfig(_Section):
|
|
149
|
+
"""Token bucket: `rate_per_second` refills it, `burst` is its size."""
|
|
150
|
+
|
|
151
|
+
rate_per_second: float = Field(default=1.0, gt=0)
|
|
152
|
+
burst: int = Field(default=2, ge=1)
|
|
153
|
+
# A run waits for a slot at most this long, then is rejected with 429.
|
|
154
|
+
max_wait_seconds: float = Field(default=5.0, ge=0)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
class Config(BaseModel):
|
|
158
|
+
model_config = ConfigDict(extra="forbid")
|
|
159
|
+
|
|
160
|
+
server: ServerConfig = Field(default_factory=ServerConfig)
|
|
161
|
+
pool: PoolConfig = Field(default_factory=PoolConfig)
|
|
162
|
+
deadline: DeadlineConfig = Field(default_factory=DeadlineConfig)
|
|
163
|
+
limits: LimitsConfig = Field(default_factory=LimitsConfig)
|
|
164
|
+
rate_limit: RateLimitConfig = Field(default_factory=RateLimitConfig)
|
|
165
|
+
# Per-domain overrides of `rate_limit`, keyed by host name, e.g. "www.reddit.com".
|
|
166
|
+
rate_limits: dict[str, RateLimitConfig] = Field(default_factory=dict)
|
|
167
|
+
|
|
168
|
+
# Which browser the shared (`session: none`) lane launches: your installed Google Chrome,
|
|
169
|
+
# or Playwright's managed Chromium (needs `playwright install --only-shell chromium`).
|
|
170
|
+
# Logged-in `profile:<name>` recipes always use real Chrome: Chrome and Chromium keep
|
|
171
|
+
# their cookies in incompatible ways, so one profile cannot be shared between them.
|
|
172
|
+
browser: Literal["chrome", "chromium"] = "chrome"
|
|
173
|
+
|
|
174
|
+
recipes_dir: Path | None = None
|
|
175
|
+
# Local Python hooks (`<name>.py` with `transform(data, params)`). Code you wrote and keep.
|
|
176
|
+
hooks_dir: Path | None = None
|
|
177
|
+
profiles_dir: Path = Path("~/.api-everything/profiles")
|
|
178
|
+
# Per profile name: a directory (Chrome launched by the daemon on it), or
|
|
179
|
+
# `{attach: "http://127.0.0.1:9222"}` (a Chrome you started with `api-everything chrome`).
|
|
180
|
+
profiles: dict[str, Path | AttachProfile] = Field(default_factory=dict)
|
|
181
|
+
|
|
182
|
+
# Where `api-everything recipes pull` fetches shared recipes from, and where applied ones
|
|
183
|
+
# live. Applied recipes sit between the bundled ones and yours: yours always win by name.
|
|
184
|
+
# Set `recipe_sources: []` to turn delivery off.
|
|
185
|
+
recipe_sources: list[RecipeSource] = Field(
|
|
186
|
+
default_factory=lambda: [RecipeSource(git=DEFAULT_RECIPE_SOURCE)]
|
|
187
|
+
)
|
|
188
|
+
community_dir: Path = Path("~/.api-everything/community")
|
|
189
|
+
sources_dir: Path = Path("~/.api-everything/sources")
|
|
190
|
+
# Where `api-everything repair` keeps drafted fixes until you apply or discard them.
|
|
191
|
+
repairs_dir: Path = Path("~/.api-everything/repairs")
|
|
192
|
+
|
|
193
|
+
# Recipes using the `eval` action are refused unless this is true.
|
|
194
|
+
allow_eval: bool = False
|
|
195
|
+
# Extra safety for logged-in sessions: a `profile:<name>` recipe that uses `eval` also
|
|
196
|
+
# needs its own name listed here, since it would run custom JavaScript as you.
|
|
197
|
+
allow_eval_in_profiles: list[str] = Field(default_factory=list)
|
|
198
|
+
# Optional shell command run by `check --all` when a check fails.
|
|
199
|
+
notify_command: str | None = None
|
|
200
|
+
|
|
201
|
+
# Set from the environment only; see module docstring.
|
|
202
|
+
token: SecretStr | None = None
|
|
203
|
+
|
|
204
|
+
@model_validator(mode="after")
|
|
205
|
+
def _check_consistency(self) -> Config:
|
|
206
|
+
if self.deadline.default_seconds > self.deadline.max_seconds:
|
|
207
|
+
raise ValueError("deadline.default_seconds must not exceed deadline.max_seconds")
|
|
208
|
+
if not _is_loopback(self.server.host) and self.token is None:
|
|
209
|
+
raise ValueError(
|
|
210
|
+
f"refusing to bind {self.server.host!r} without an API token: set "
|
|
211
|
+
f"{TOKEN_ENV} or {TOKEN_FILE_ENV}, or bind to 127.0.0.1"
|
|
212
|
+
)
|
|
213
|
+
return self
|
|
214
|
+
|
|
215
|
+
def rate_limit_for(self, domain: str) -> RateLimitConfig:
|
|
216
|
+
"""The rate limit for a domain: its override if present, else the default."""
|
|
217
|
+
return self.rate_limits.get(domain, self.rate_limit)
|
|
218
|
+
|
|
219
|
+
def profile_dir(self, name: str) -> Path:
|
|
220
|
+
"""Where a named profile lives: its explicit mapping, else under `profiles_dir`."""
|
|
221
|
+
if not _PROFILE_NAME.match(name):
|
|
222
|
+
raise ConfigError(
|
|
223
|
+
f"invalid profile name {name!r}: use lowercase letters, digits, '-' and '_'"
|
|
224
|
+
)
|
|
225
|
+
entry = self.profiles.get(name)
|
|
226
|
+
if isinstance(entry, Path):
|
|
227
|
+
return entry.expanduser()
|
|
228
|
+
# An attached profile still has a folder: where `api-everything chrome` keeps it.
|
|
229
|
+
return (self.profiles_dir / name).expanduser()
|
|
230
|
+
|
|
231
|
+
def profile_attach(self, name: str) -> AttachProfile | None:
|
|
232
|
+
"""The attach setting of a profile, or None when the daemon launches its own Chrome."""
|
|
233
|
+
if not _PROFILE_NAME.match(name):
|
|
234
|
+
raise ConfigError(
|
|
235
|
+
f"invalid profile name {name!r}: use lowercase letters, digits, '-' and '_'"
|
|
236
|
+
)
|
|
237
|
+
entry = self.profiles.get(name)
|
|
238
|
+
return entry if isinstance(entry, AttachProfile) else None
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _is_loopback(host: str) -> bool:
|
|
242
|
+
if host == "localhost":
|
|
243
|
+
return True
|
|
244
|
+
try:
|
|
245
|
+
return ipaddress.ip_address(host).is_loopback
|
|
246
|
+
except ValueError: # a host name we cannot prove is loopback
|
|
247
|
+
return False
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _set_nested(target: dict[str, Any], path: list[str], value: str, source: str) -> None:
|
|
251
|
+
node = target
|
|
252
|
+
for part in path[:-1]:
|
|
253
|
+
child = node.setdefault(part, {})
|
|
254
|
+
if not isinstance(child, dict):
|
|
255
|
+
raise ConfigError(f"{source}: {part!r} is a value in the config, not a section")
|
|
256
|
+
node = child
|
|
257
|
+
if isinstance(node.get(path[-1]), dict):
|
|
258
|
+
raise ConfigError(f"{source}: {path[-1]!r} is a section, not a single value")
|
|
259
|
+
node[path[-1]] = value
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _read_yaml(path: Path) -> dict[str, Any]:
|
|
263
|
+
try:
|
|
264
|
+
text = path.read_text()
|
|
265
|
+
except OSError as exc:
|
|
266
|
+
raise ConfigError(f"cannot read config file {path}: {exc.strerror}") from exc
|
|
267
|
+
try:
|
|
268
|
+
data = yaml.safe_load(text) or {}
|
|
269
|
+
except yaml.YAMLError as exc:
|
|
270
|
+
raise ConfigError(f"config file {path} is not valid YAML: {exc}") from exc
|
|
271
|
+
if not isinstance(data, dict):
|
|
272
|
+
raise ConfigError(f"config file {path} must contain a mapping at the top level")
|
|
273
|
+
if "token" in data:
|
|
274
|
+
raise ConfigError(
|
|
275
|
+
f"config file {path} contains 'token'. The token is read only from "
|
|
276
|
+
f"{TOKEN_ENV} or {TOKEN_FILE_ENV}, never from YAML, so it cannot be committed"
|
|
277
|
+
)
|
|
278
|
+
return data
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
def _read_token(env: Mapping[str, str]) -> str | None:
|
|
282
|
+
direct, file_path = env.get(TOKEN_ENV), env.get(TOKEN_FILE_ENV)
|
|
283
|
+
if direct and file_path:
|
|
284
|
+
raise ConfigError(f"set only one of {TOKEN_ENV} and {TOKEN_FILE_ENV}, not both")
|
|
285
|
+
if file_path:
|
|
286
|
+
try:
|
|
287
|
+
token = Path(file_path).expanduser().read_text().strip()
|
|
288
|
+
except OSError as exc:
|
|
289
|
+
raise ConfigError(
|
|
290
|
+
f"cannot read {TOKEN_FILE_ENV} file {file_path}: {exc.strerror}"
|
|
291
|
+
) from exc
|
|
292
|
+
else:
|
|
293
|
+
token = (direct or "").strip()
|
|
294
|
+
if (direct is not None or file_path) and not token:
|
|
295
|
+
raise ConfigError("the API token is set but empty")
|
|
296
|
+
return token or None
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def load_config(path: Path | None = None, env: Mapping[str, str] | None = None) -> Config:
|
|
300
|
+
"""Build the configuration from an optional YAML file plus the environment."""
|
|
301
|
+
env = os.environ if env is None else env
|
|
302
|
+
data: dict[str, Any] = _read_yaml(path) if path else {}
|
|
303
|
+
|
|
304
|
+
for key, value in env.items():
|
|
305
|
+
if key.startswith(ENV_PREFIX):
|
|
306
|
+
parts = [p.lower() for p in key[len(ENV_PREFIX) :].split("__") if p]
|
|
307
|
+
if parts:
|
|
308
|
+
_set_nested(data, parts, value, source=key)
|
|
309
|
+
|
|
310
|
+
token = _read_token(env)
|
|
311
|
+
if token:
|
|
312
|
+
data["token"] = token
|
|
313
|
+
|
|
314
|
+
try:
|
|
315
|
+
return Config.model_validate(data)
|
|
316
|
+
except ValidationError as exc:
|
|
317
|
+
lines = []
|
|
318
|
+
for err in exc.errors():
|
|
319
|
+
where = ".".join(str(p) for p in err["loc"]) or "config"
|
|
320
|
+
lines.append(f" {where}: {err['msg']}")
|
|
321
|
+
raise ConfigError("invalid configuration:\n" + "\n".join(lines)) from exc
|