Tentacat
GitHub API的简单Elixir封装。
Tentacool + Cat = Tentacat
功能
- Gitignore
- 内容
- 树
- 项目
- 问题
- 评论
- 事件
- 标签
- 拉取请求
- 评论
- 提交
- 文件
- 审查
- 审查请求
- 组织
- 成员
- 团队
- Webhooks
- 项目
- 团队
- 成员
- 用户
- 邮箱
- 密钥
- 项目
- 仓库
- Webhooks
- 分支
- 状态
- 部署
- 协作者
- 贡献者
- 项目
文档可以在这里找到
快速开始
首先,将Tentacat添加到你的mix.exs
依赖中:
def deps do
[
{:tentacat, "~> 2.0"}
]
end
获取依赖并在elixir控制台中运行:
mix deps.get
iex -S mix
你会看到类似这样的内容:
Erlang/OTP 17 [erts-6.0] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (0.13.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)>
现在你可以运行示例了!
示例
每个对GitHub的调用都需要一个客户端,但如果你想使用未经身份验证的请求,我们会为你提供一个未经身份验证的客户端。请记住,GitHub对经过身份验证和未经身份验证的请求有不同的速率限制。
使用客户端获取用户信息:
iex> client = Tentacat.Client.new
%Tentacat.Client{auth: nil, endpoint: "https://api.github.com/"}
iex> Tentacat.Users.find client, "edgurgel"
{200,
%{"avatar_url" => "https://avatars0.githubusercontent.com/u/30873?v=4",
"bio" => "无法得出有意义的结论的数据不足",
"blog" => "http://gurgel.me", "company" => nil,
"created_at" => "2008-10-24T17:05:04Z", "email" => nil,
"events_url" => "https://api.github.com/users/edgurgel/events{/privacy}",
"followers" => 220,
"followers_url" => "https://api.github.com/users/edgurgel/followers",
"following" => 75,
"following_url" => "https://api.github.com/users/edgurgel/following{/other_user}",
"gists_url" => "https://api.github.com/users/edgurgel/gists{/gist_id}",
"gravatar_id" => "", "hireable" => nil,
"html_url" => "https://github.com/edgurgel", "id" => 30873,
"location" => "惠灵顿, 新西兰", "login" => "edgurgel",
"name" => "Eduardo Gurgel",
"organizations_url" => "https://api.github.com/users/edgurgel/orgs",
"public_gists" => 13, "public_repos" => 59,
"received_events_url" => "https://api.github.com/users/edgurgel/received_events",
"repos_url" => "https://api.github.com/users/edgurgel/repos",
"site_admin" => false,
"starred_url" => "https://api.github.com/users/edgurgel/starred{/owner}{/repo}",
"subscriptions_url" => "https://api.github.com/users/edgurgel/subscriptions",
"type" => "User", "updated_at" => "2018-02-05T23:24:42Z",
"url" => "https://api.github.com/users/edgurgel"},
%HTTPoison.Response{body: %{"avatar_url" => "https://avatars0.githubusercontent.com/u/30873?v=4",
"bio" => "无法得出有意义的结论的数据不足",
"blog" => "http://gurgel.me", "company" => nil,
"created_at" => "2008-10-24T17:05:04Z", "email" => nil,
"events_url" => "https://api.github.com/users/edgurgel/events{/privacy}",
"followers" => 220,
"followers_url" => "https://api.github.com/users/edgurgel/followers",
"following" => 75,
"following_url" => "https://api.github.com/users/edgurgel/following{/other_user}",
"gists_url" => "https://api.github.com/users/edgurgel/gists{/gist_id}",
"gravatar_id" => "", "hireable" => nil,
"html_url" => "https://github.com/edgurgel", "id" => 30873,
"location" => "惠灵顿, 新西兰", "login" => "edgurgel",
"name" => "Eduardo Gurgel",
"organizations_url" => "https://api.github.com/users/edgurgel/orgs",
"public_gists" => 13, "public_repos" => 59,
"received_events_url" => "https://api.github.com/users/edgurgel/received_events",
"repos_url" => "https://api.github.com/users/edgurgel/repos",
"site_admin" => false,
"starred_url" => "https://api.github.com/users/edgurgel/starred{/owner}{/repo}",
"subscriptions_url" => "https://api.github.com/users/edgurgel/subscriptions",
"type" => "User", "updated_at" => "2018-02-05T23:24:42Z",
"url" => "https://api.github.com/users/edgurgel"},
headers: [{"Date", "Mon, 05 Feb 2018 23:25:36 GMT"},
{"Content-Type", "application/json; charset=utf-8"},
{"Content-Length", "1187"}, {"Server", "GitHub.com"}, {"Status", "200 OK"},
{"X-RateLimit-Limit", "60"}, {"X-RateLimit-Remaining", "59"},
{"X-RateLimit-Reset", "1517876736"},
{"Cache-Control", "public, max-age=60, s-maxage=60"}, {"Vary", "Accept"},
{"ETag", "\"ec2653a252e614a96afacfaeb88d0c39\""},
{"Last-Modified", "Mon, 05 Feb 2018 23:24:42 GMT"},
{"X-GitHub-Media-Type", "github.v3; format=json"},
{"Access-Control-Expose-Headers",
"ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval"},
{"Access-Control-Allow-Origin", "*"},
{"Content-Security-Policy", "default-src 'none'"},
{"Strict-Transport-Security",
"max-age=31536000; includeSubdomains; preload"},
{"X-Content-Type-Options", "nosniff"}, {"X-Frame-Options", "deny"},
{"X-XSS-Protection", "1; mode=block"}, {"X-Runtime-rack", "0.030182"},
{"Vary", "Accept-Encoding"},
{"X-GitHub-Request-Id", "054D:2BC4A:82C2C:A4560:5A78E7EF"}],
request_url: "https://api.github.com/users/edgurgel", status_code: 200}}
不使用定义的客户端获取用户信息:
iex> {200, data, _response} = Tentacat.Users.find("edgurgel")
iex> get_in(data, ["name"])
"Eduardo Gurgel"
获取已认证用户的信息:
- 使用用户名和密码:
iex> client = Tentacat.Client.new(%{user: "user", password: "password"})
%Tentacat.Client{auth: %{user: "user", password: "password"}, endpoint: "https://api.github.com/"}
iex> Tentacat.Users.me(client)
- 使用个人访问令牌 Github个人API令牌:
iex> client = Tentacat.Client.new(%{access_token: "928392873982932"})
%Tentacat.Client{auth: %{access_token: "928392873982932"}, endpoint: "https://api.github.com/"}
iex> Tentacat.Users.me(client)
访问其他端点:
iex> client = Tentacat.Client.new(%{access_token: "928392873982932"}, "https://ghe.example.com/api/v3/")
%Tentacat.Client{auth: %{access_token: "928392873982932"}, endpoint: "https://ghe.example.com/api/v3/"}
iex> Tentacat.Users.me(client)
其他
由于Github Reviews API仍处于预发布状态, 您需要在配置中设置一个额外的头部。
config :tentacat, :extra_headers, [{"Accept", "application/vnd.github.black-cat-preview+json"}]
反序列化选项
您可以通过以下方式向用于解码JSON的库传递反序列化选项:
# 使用原子键
config :tentacat, :deserialization_options, [keys: :atoms]
请参阅: https://hexdocs.pm/jason/Jason.html#decode/2-options 了解可用选项。
贡献
首先fork这个仓库。
然后运行以下命令获取依赖并运行测试:
MIX_ENV=test mix do deps.get, test
如果你正在使用Intellij并调试测试套件,请确保在Elixir Mix Eunit配置中设置'INTELLIJ_ELIXIR_DEBUG_BLACKLIST=hackney'
。如果你没有这样做,模拟的hackney模块将从磁盘重新加载,测试将会表现异常。
非常感谢您提交拉取请求。
版权和许可
版权所有 (c) 2013 Eduardo Gurgel Pinho
根据MIT许可证发布,可以在仓库中的LICENSE.md文件中找到。