• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

lusis/lua-github: Lua wrapper for talking to the github api

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

lusis/lua-github

开源软件地址:

https://github.com/lusis/lua-github

开源编程语言:

Lua 96.9%

开源软件介绍:

lua-github

lua-github is a small wrapper around the github API. It's currently primarily focused on user, org and team read-operations but the hope is to extend it to as much of the github api as possible.

Install

Easiest option is probably to install from luarocks:

luarocks install github

Alternately, you can install via the included Makefile. You'll probably want to override the LUA_SHAREDIR environment variable. You'll also need to make sure you install my httpclient library. Since github uses SSL, if you're using the luasocket driver you'll need luasec as well. If you're only using it via ngx-lua/openresty, you shouldn't need luasec.

Requirements

The following versions were tested

  • lua 5.2 (the version that shipped with trusty)
  • httpclient 0.1.0-6
  • openresty 1.7.4.1

Usage

Below are a few examples of usage both with nginx and luasocket/luasec. Results of most api calls return either

  • a lua table of the json (via cjson.decode) and nil error or
  • a nil result and an error message

Some api calls return true/false and any relevant error.

straight lua

local inspect = require 'inspect'
local gh = require('github').new()
gh:set_access_token('XXXXXXXX')
-- alternately: local gh = require('github').new({access_token = 'XXXXXXXXX'})
local r, err = gh:get_authenticated_user()
print(err)
-- no access token

gh:set_access_token('XXXXXXXX')
-- alternately: local gh = require('github').new({access_token = 'XXXXXXXXX'})

local r, err = gh:get_authenticated_user()

if err then
  print(err)
else
  inspect(r)
end
-- {
--   avatar_url = "https://avatars.githubusercontent.com/u/228958?v=3",
--   bio = <userdata 1>,
--   blog = "http://about.me/lusis",
--   company = "The Lusis Group",
--   created_at = "2010-03-23T20:28:44Z",
--   email = "[email protected]",
--   events_url = "https://api.github.com/users/lusis/events{/privacy}",
--   followers = 231,
--   followers_url = "https://api.github.com/users/lusis/followers",
--   following = 97,
--   following_url = "https://api.github.com/users/lusis/following{/other_user}",
--   gists_url = "https://api.github.com/users/lusis/gists{/gist_id}",
--   gravatar_id = "",
--   hireable = false,
--   html_url = "https://github.com/lusis",
--   id = 228958,
--   location = "Roswell, GA.",
--   login = "lusis",
--   name = "John E. Vincent",
--   organizations_url = "https://api.github.com/users/lusis/orgs",
--   public_gists = 231,
--   public_repos = 137,
--   received_events_url = "https://api.github.com/users/lusis/received_events",
--   repos_url = "https://api.github.com/users/lusis/repos",
--   site_admin = false,
--   starred_url = "https://api.github.com/users/lusis/starred{/owner}{/repo}",
--   subscriptions_url = "https://api.github.com/users/lusis/subscriptions",
--   type = "User",
--   updated_at = "2014-12-03T20:56:53Z",
--   url = "https://api.github.com/users/lusis"
-- }

print(r.login)
-- lusis
local r, err = gh:get_authed_user_org_membership('github', 'lusis')
print(r)
-- false
local r, err = gh:get_authed_user_org_membership('logstash', 'lusis')
print(r)
-- true

nginx usage

The nginx usage is largely similar but you'll need to follow the httpclient instructions to set up the internal redirect in nginx. Then you'll use this library specifying the alternate driver:

ngx.var.access_token = 'XXXXXXX'
local gh = require('github').new({access_token = ngx.var.access_token, httpclient_driver = 'httpclient.ngx_driver'})
local r, err = gh:get_authenticated_user()
if err or r.login ~= 'lusis' then
  ngx.exit(ngx.HTTP_UNAUTHORIZED)
else
  ngx.say("welcome "..r.login)
end

oauth usage

Alternately you can make all calls with oauth credentials but it's up to you to store those somewhere inside nginx each authenticated user. The library provides a few helpers you can use:

  • Generate a redirect url for oauth requests: gh:get_authorize_url(client_id, scope)
  • Process an oauth callback code to generate a usable auth token: gh:request_token(callback_code, args)

In the second example, args is a lua table like so:

args = {
  client_id = 'XXXXXXX',
  client_secret = 'YYYYYYY',
  redirect_uri = 'http://hostname/my_oauth_callback'
}

Authenticated vs Unauthenticated calls

Note that the library can be used out of the box without having a github access token. Most calls provide authenticated vs unauthenticated versions. For example:

  • get_user(username) vs
  • get_authenticated_user()

The first option makes a call to https://api.github.com/users/<username> while the second makes a call to https://api.github.com/user?access_token=XXXXXXX. This allows you to use public calls for some operations where the token isn't neccessary.

Debugging the underlying httpclient

A feature of httpclient is the ability to get details about the last request made. The github library exposes the raw httpclient via gh.hc. Any functions available to straight httpclient can be used here as well like res = gh.hc:get('https://httpbin.org/get').

With every action in httpclient you can always call get_last_request() which will return some information about the driver as well as the data passed in to driver to make the request. For example while writing this library, I ran into some header related bugs with nginx. I was able to debug this information like so:

local inspect = require 'inspect'
ngx.log(ngx.ERR, inspect(gh.hc:get_last_request().get_headers()))

Which is where I realized that my accept header wasn't being passed to the github api properly.

TODO

  • tests
  • environment variable + bin script for a quick cli client
  • more docs
  • docker/rocket for quick openresty environment for testing



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap