算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
serpapi-sdk-patterns【设计与创意】Wrap the official SerpAPI Python or JavaScript client behind typed, testable boundaries with safe errors, pagination, and output selection. Use when production code needs a stable search adapter. Trigger with "design a SerpAPI client wrapper".
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Create a narrow application-owned gateway instead of spreading vendor parameters, credentials, and variable result schemas through business code.
Use Read, Glob, and Grep to map current call sites and types, WebFetch to verify official client interfaces, and Write or Edit for the gateway, schemas, fixtures, and tests.
The Python client returns a SerpResults mapping and provides next_page() and yield_pages(). The JavaScript client exposes promise and callback forms including getJson, getJsonBySearchId, and getAccount, but does not provide built-in pagination. Search output can be JSON, HTML, or Markdown; json_restrictor can reduce JSON payload fields.
Inject SERPAPI_KEY at the outer server-side composition root. Do not include it in domain types, cache keys, error objects, telemetry, or serialized request parameters.
yield_pages() only with a page/search budget; for JavaScript, implement engine-specific manual pagination from documented tokens or offsets.json_restrictor when supported fields are known.Return request and response types, gateway code, client-version pin, pagination and retry budgets, output-selection rationale, fixture tests, and redaction evidence.
| Condition | Response |
|---|---|
| Unknown engine or parameter | Reject locally before making a request. |
Python HTTPError or TimeoutError | Map to a typed application error and preserve safe status/search metadata. |
| JavaScript rejection | Normalize the status and message without serializing request credentials. |
| Pagination budget reached | Return an explicit partial result and continuation state. |
import { getJson } from "serpapi";
export async function searchGoogle(query: string) {
const result = await getJson({
engine: "google",
q: query,
api_key: process.env.SERPAPI_KEY,
json_restrictor: "organic_results[].{position,title,link}",
});
return (result.organic_results ?? []).map(({ position, title, link }) => ({
position, title, link,
}));
}
Exercise the gateway against sanitized fixtures, then canary one approved live request per supported engine.