pip install pingera-sdkimport os
import time
import json
from pingera.api import OnDemandChecksApi
from pingera.models import ExecuteCustomCheckRequest
from pingera import ApiClient, Configuration, ApiException
# Настройка API-клиента (можно также использовать переменные окружения)
configuration = Configuration(host="https://api.pingera.ru")
configuration.api_key['apiKeyAuth'] = os.getenv("PINGERA_API_KEY")
# Наш Playwright-сценарий
playwright_script = """
// Простой Playwright-тест для pingera.ru
const { test, expect } = require('@playwright/test');
test('Basic Screenshot', async ({ page }) => {
await page.goto('https://pingera.ru/');
await page.waitForLoadState('networkidle');
await page.screenshot({ path: 'screenshot.png', fullPage: true });
console.log('Скриншот сделан!');
});
"""
# Создаем запрос
request_body = ExecuteCustomCheckRequest(
url="https://pingera.ru",
type="synthetic",
parameters={
"pw_script": playwright_script
},
timeout=30,
name="On-Demand Check from SDK"
)
with ApiClient(configuration) as api_client:
api = OnDemandChecksApi(api_client)
try:
# Запускаем проверку
print("🚀 Запуск on-demand синтетической проверки...")
job_result = api.v1_checks_on_demand_post(request_body)
job_id = job_result.job_id
print(f"✅ Проверка отправлена! ID задачи: {job_id}")
# Ожидаем завершения
max_wait_time = 30 # 30 секунд
start_time = time.time()
while True:
if time.time() - start_time > max_wait_time:
print("❌ Превышено время ожидания.")
break
job_status = api.v1_checks_on_demand_get(job_id)
if job_status.status in ["completed", "failed"]:
print("✅ Задача выполнена.")
# Выводим детальный результат
print("=== Детальный результат ===")
print(json.dumps(job_status.result.to_dict(), indent=2))
break
print("⏳ Ожидание завершения...")
time.sleep(5)
except ApiException as e:
print(f"❌ Ошибка API: {e.status} - {e.reason}")