| from __future__ import annotations |
|
|
| import json |
| import urllib.parse |
| import urllib.request |
| from typing import Any |
|
|
|
|
| def get_json(url: str, params: dict[str, Any] | None = None, timeout: int = 20) -> dict[str, Any]: |
| if params: |
| encoded = urllib.parse.urlencode(params, doseq=True) |
| separator = "&" if "?" in url else "?" |
| url = f"{url}{separator}{encoded}" |
| request = urllib.request.Request( |
| url, |
| headers={ |
| "User-Agent": "SiteIntelligenceStudio/0.1 (Build Small Hackathon)", |
| "Accept": "application/json", |
| }, |
| ) |
| with urllib.request.urlopen(request, timeout=timeout) as response: |
| return json.loads(response.read().decode("utf-8")) |
|
|
|
|
| def post_text(url: str, body: str, timeout: int = 25) -> dict[str, Any]: |
| data = body.encode("utf-8") |
| request = urllib.request.Request( |
| url, |
| data=data, |
| method="POST", |
| headers={ |
| "User-Agent": "SiteIntelligenceStudio/0.1 (Build Small Hackathon)", |
| "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", |
| "Accept": "application/json", |
| }, |
| ) |
| with urllib.request.urlopen(request, timeout=timeout) as response: |
| return json.loads(response.read().decode("utf-8")) |
|
|