示例代码
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" } }