sqlite-http 作者 asg017

标星

README 源代码

sqlite-http

一个用于在 SQL 中纯粹发起 HTTP 请求的 SQLite 扩展。

  • 创建 GET、POST 和其他 HTTP 请求,类似于 curlwgetfetch
  • 下载响应体、头部、状态码、计时信息
  • 设置速率限制、超时

用法

NPR:美国国家公共广播电台 - Datasette 工具 .... */">
.load ./http0
select http_get_body('https://text.npr.org/');
/*



  NPR : National Public Radio - Datasette 工具
  ....
*/

查询端点中的所有自定义头部。

select name, value
from http_headers_each(
  http_get_headers('https://api.github.com/')
)
where name like 'X-%';
/*
┌────────────────────────┬────────────────────────────────────┐
│          name          │               value                │
├────────────────────────┼────────────────────────────────────┤
│ X-Ratelimit-Limit      │ 60                                 │
│ X-Ratelimit-Used       │ 8                                  │
│ X-Content-Type-Options │ nosniff                            │
│ X-Github-Media-Type    │ github.v3; format=json             │
│ X-Github-Request-Id    │ CCCA:5FDF:1014BC2:10965F9:62F3DE4E │
│ X-Ratelimit-Remaining  │ 52                                 │
│ X-Ratelimit-Resource   │ core                               │
│ X-Frame-Options        │ deny                               │
│ X-Ratelimit-Reset      │ 1660152798                         │
│ X-Xss-Protection       │ 0                                  │
└────────────────────────┴────────────────────────────────────┘
*/

从 JSON 端点抓取数据。

select http_get_body('https://api.github.com/repos/sqlite/sqlite')
  ->> '$.description' as description;
/*
┌───────────────────────────────────────────────┐
│                  description                  │
├───────────────────────────────────────────────┤
│ Official Git mirror of the SQLite source tree │
└───────────────────────────────────────────────┘
*/

在请求中传入特定头部。

select
  value
from json_each(
  http_get_body(
    'https://api.github.com/issues',
    http_headers(
      'Authorization', 'token ghp_16C7e42F292c6912E7710c8'
    )
  )
);

文档

请参阅 docs.md 获取完整的 API 参考。

安装

语言 安装
Python pip install sqlite-http PyPI
Datasette datasette install datasette-sqlite-http Datasette
Node.js npm install sqlite-http npm
Deno deno.land/x/sqlite_http deno.land/x release
Ruby gem install sqlite-http Gem
Github 发布 GitHub tag (latest SemVer pre-release)

发布页面包含适用于 Linux amd64、MacOS amd64(不支持 arm)和 Windows 的预构建二进制文件。

作为可加载扩展

如果您想将 sqlite-http 用作运行时可加载扩展,请从发布版本中下载 http0.dylib(适用于 MacOS)、http0.so(适用于 Linux)或 http0.dll(适用于 Windows)文件,并将其加载到您的 SQLite 环境中。

注意:文件名中的 0 (http0.dylib/ http0.so/http0.dll) 表示 sqlite-http 的主版本。目前 sqlite-http 处于 v1 之前,因此未来版本可能会有重大更改。

例如,如果您正在使用SQLite CLI,您可以像这样加载库

.load ./http0
select http_version();
-- v0.0.1

或者在 Python 中,使用内置的sqlite3 模块

import sqlite3

con = sqlite3.connect(":memory:")

con.enable_load_extension(True)
con.load_extension("./http0")

print(con.execute("select http_version()").fetchone())
# ('v0.0.1',)

或者在 Node.js 中,使用better-sqlite3

const Database = require("better-sqlite3");
const db = new Database(":memory:");

db.loadExtension("./http0");

console.log(db.prepare("select http_version()").get());
// { 'http_version()': 'v0.0.1' }

或者使用Datasette,并使用“无网络”选项来限制 DDoS 攻击

datasette data.db --load-extension ./http0-no-net

另请参阅

  • sqlite-html,用于在 SQLite 中使用 CSS 选择器解析和查询 HTML(与本工具搭配使用效果极佳)
  • pgsql-http,一个与 POstgreSQL 类似但又非常不同的 HTTP 库(我在开始之前不知道这个,但有趣的是,我设计出了一个非常相似的 API)
  • riyaz-ali/sqlite,本库所依赖的优秀的 Go 库
  • nalgeon/sqlean,几个预编译的实用的 SQLite 函数,用 C 语言编写