SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
1.6 KB · 48 lines python
Raw Blame History
1"""Helpers for the Gemini core-generation smoke tests: free-tier pacing (15 RPM/model) and one 429 retry."""2from __future__ import annotations34import base645import re6import struct7import time8import zlib910_LAST_CALL = [0.0]11MIN_GAP_S = 4.2121314def call(gemini, method: str, path: str, body=None, retries: int = 2, **kw):15    """gemini fixture wrapper: keeps >= MIN_GAP_S between calls; retries on 429 (RetryInfo delay) and transient 503."""16    for attempt in range(retries + 1):17        gap = MIN_GAP_S - (time.time() - _LAST_CALL[0])18        if gap > 0:19            time.sleep(gap)20        st, resp, hdrs = gemini(method, path, body, **kw)21        _LAST_CALL[0] = time.time()22        if st not in (429, 503) or attempt == retries:23            return st, resp, hdrs24        msg = resp.get("error", {}).get("message", "") if isinstance(resp, dict) else ""25        m = re.search(r"retry in ([0-9.]+)s", msg)26        time.sleep(float(m.group(1)) + 1 if m else 20)27    return st, resp, hdrs282930def png_1x1_b64() -> str:31    def ch(t, d):32        c = struct.pack(">I", len(d)) + t + d33        return c + struct.pack(">I", zlib.crc32(t + d) & 0xFFFFFFFF)34    png = b"\x89PNG\r\n\x1a\n" + ch(b"IHDR", struct.pack(">IIBBBBB", 1, 1, 8, 2, 0, 0, 0)) + ch(b"IDAT", zlib.compress(b"\x00\xff\x00\x00")) + ch(b"IEND", b"")35    return base64.b64encode(png).decode()363738def user(text: str) -> list[dict]:39    return [{"role": "user", "parts": [{"text": text}]}]404142def text_of(body: dict) -> str:43    return "".join(p.get("text", "") for p in body["candidates"][0]["content"]["parts"] if not p.get("thought"))444546def gc(model: str) -> str:47    return f"/v1beta/models/{model}:generateContent"48