範例程式碼
rpicx API 使用範例
GET
/v1/health健康檢查服務健康檢查,返回當前圖片總數和運行狀態。
curl 'https://api.tor.hk/v1/v1/health'<script>
fetch('https://api.tor.hk/v1/v1/health')
.then(res => res.json())
.then(data => console.log(data))
</script>fetch('https://api.tor.hk/v1/v1/health')
.then(res => res.json())
.then(data => console.log(data))use reqwest;
use serde_json::Value;
#[tokio::main]
async fn main() -> Result<()> {
let resp: Value = reqwest::Client::new()
.get("https://api.tor.hk/v1/v1/health")
.send().await?
.json().await?;
println!("{:?}", resp);
Ok(())
}響應 200:
{
"status": "ok",
"image_count": 100000
}GET
/v1/random獲取隨機圖片從記憶體索引中隨機選取一張圖片,返回 302 重定向。
curl 'https://api.tor.hk/v1/v1/random'<img src="https://api.tor.hk/v1/v1/random" alt="Random image" />fetch('https://api.tor.hk/v1/random')
.then(res => res.blob())
.then(blob => {
const img = document.createElement('img')
img.src = URL.createObjectURL(blob)
document.body.appendChild(img)
})use reqwest;
#[tokio::main]
async fn main() -> Result<()> {
let resp = reqwest::get("https://api.tor.hk/v1/random").await?;
let bytes = resp.bytes().await?;
println!("Downloaded {} bytes", bytes.len());
Ok(())
}成功 (302):
Location: {siteConfig.image_url}/xxx.webpGET
/v1/categories獲取分類列表返回圖片總數。當前為平面索引,categories 固定為空陣列。
curl 'https://api.tor.hk/v1/v1/categories'<script>
fetch('https://api.tor.hk/v1/v1/categories')
.then(res => res.json())
.then(data => console.log(data))
</script>fetch('https://api.tor.hk/v1/v1/categories')
.then(res => res.json())
.then(data => console.log(data))use reqwest;
use serde_json::Value;
#[tokio::main]
async fn main() -> Result<()> {
let resp: Value = reqwest::Client::new()
.get("https://api.tor.hk/v1/v1/categories")
.send().await?
.json().await?;
println!("{:?}", resp);
Ok(())
}響應 200:
{
"total_images": 100000,
"categories": []
}GET
/v1/stats獲取統計資訊返回服務聚合統計資訊。(IP 限流 10 QPS,響應緩存 3 小時)
curl 'https://api.tor.hk/v1/v1/stats'<script>
fetch('https://api.tor.hk/v1/v1/stats')
.then(res => res.json())
.then(data => console.log(data))
</script>fetch('https://api.tor.hk/v1/v1/stats')
.then(res => res.json())
.then(data => console.log(data))use reqwest;
use serde_json::Value;
#[tokio::main]
async fn main() -> Result<()> {
let resp: Value = reqwest::Client::new()
.get("https://api.tor.hk/v1/v1/stats")
.send().await?
.json().await?;
println!("{:?}", resp);
Ok(())
}響應 200:
{
"requests_total": 1000000,
"requests_today": 5000,
"avg_hour_requests": 200.0,
"uv_today": 1200,
"top_referer": [{ "name": "github.com", "count": 300 }],
"top_ua": [{ "name": "Chrome", "count": 500 }],
"status": { "code_200": 900000, "code_302": 50000, "code_404": 20, "code_429": 10, "code_500": 0 },
"sync_count": 30
}GET
/metricsPrometheus 指標Prometheus 指標暴露端點,返回 text/plain 格式指標數據。
curl 'https://api.tor.hk/v1/metrics'<script>
fetch('https://api.tor.hk/v1/metrics')
.then(res => res.json())
.then(data => console.log(data))
</script>fetch('https://api.tor.hk/v1/metrics')
.then(res => res.text())
.then(data => console.log(data))use reqwest;
#[tokio::main]
async fn main() -> Result<()> {
let resp = reqwest::get("https://api.tor.hk/v1/metrics").await?;
let text = resp.text().await?;
println!("{}", text);
Ok(())
}響應 200:
# HELP rpicx_requests_total Total requests
# TYPE rpicx_requests_total counter
rpicx_requests_total 1000000
# HELP rpicx_images_total Total images
# TYPE rpicx_images_total gauge
rpicx_images_total 100000GET
/openapi.jsonOpenAPI 規範返回 OpenAPI 3.0 規範文檔(JSON 格式)。
curl 'https://api.tor.hk/v1/openapi.json'<script>
fetch('https://api.tor.hk/v1/openapi.json')
.then(res => res.json())
.then(data => console.log(data))
</script>fetch('https://api.tor.hk/v1/openapi.json')
.then(res => res.json())
.then(data => console.log(data))use reqwest;
use serde_json::Value;
#[tokio::main]
async fn main() -> Result<()> {
let resp: Value = reqwest::Client::new()
.get("https://api.tor.hk/v1/openapi.json")
.send().await?
.json().await?;
println!("{:?}", resp);
Ok(())
}響應 200:
{ "openapi": "3.0.0", "info": { "title": "rpicx API", "version": "1.0.0" } }