在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:piotrmurach/github开源软件地址:https://github.com/piotrmurach/github开源编程语言:Ruby 88.9%开源软件介绍:GithubAPIA Ruby client for the official GitHub API. Supports all the API methods. It's built in a modular way. You can either instantiate the whole API wrapper Github.new or use parts of it i.e. Github::Client::Repos.new if working solely with repositories is your main concern. Intuitive query methods allow you easily call API endpoints. Features
InstallationInstall the gem by running gem install github_api or put it in your Gemfile and run gem "github_api" Contents1 UsageTo start using the gem, you can either perform requests directly on Github.repos.list user: 'piotrmurach' or create a new client instance like so github = Github.new and then call api methods, for instance, to list a given user repositories do github.repos.list user: 'piotrmurach' 1.1 API NavigationThe github_api closely mirrors the GitHub API hierarchy. For example, if you want to create a new file in a repository, look up the GitHub API spec. In there you will find contents sub category underneath the repository category. This would translate to the request: github = Github.new
github.repos.contents.create 'piotrmurach', 'finite_machine', 'hello.rb',
path: 'hello.rb',
content: "puts 'hello ruby'" The whole library reflects the same api navigation. Therefore, if you need to list releases for a repository do: github.repos.releases.list 'piotrmurach', 'finite_machine' or to list a user's followers: github.users.followers.list 'piotrmurach' The code base has been extensively documented with examples of how to use each method. Please refer to the documentation under the Alternatively, you can find out which methods are supported by an api by calling Github::Client::Repos::Contents.actions
=> [:archive, :create, :delete, :find, :get, :readme, :update] 1.2 ModularityThe code base is modular. This means that you can work specifically with a given part of GitHub API. If you want to only work with activity starring API do the following: starring = Github::Client::Activity::Starring.new oauth_token: token
starring.star 'piotrmurach', 'github' Please refer to the documentation and look under 1.3 ArgumentsThe github_api library allows for flexible argument parsing. Arguments can be passed directly inside the method called. The issues = Github::Client::Issues.new
issues.milestones.list 'piotrmurach', 'github', state: 'open' In the previous example, the order of arguments is important. However, each method also allows you to specify issues = Github::Client::Issues.new
issues.milestones.list user: 'piotrmurach', repo: 'github', state: 'open' Furthermore, issues = Github::Client::Issues.new user: 'piotrmurach', repo: 'github'
issues.milestones.list state: 'open' Similarly, the issues = Github::Client::Issues.new
issues.milestones(user: 'piotrmurach', repo: 'github').list state: 'open' But why limit ourselves? You can mix and match arguments, for example: issues = Github::Client::Issues.new user: 'piotrmurach'
issues.milestones(repo: 'github').list
issues.milestones(repo: 'tty').list You can also use a bit of syntactic sugar whereby "username/repository" can be passed as well: issues = Github::Client::Issues.new
issues.milestones('piotrmurach/github').list
issues.milestones.list 'piotrmurach/github' Finally, use the issues = Github::Client::Issues.new
issues.milestones.with(user: 'piotrmurach', repo: 'github').list Please consult the method documentation or GitHub specification to see which arguments are required and what are the option parameters. 1.4 Response QueryingThe response is of type For example, when request is issued to list all the branches on a given repository, each branch will be yielded one by one: repos = Github::Client::Repos.new
repos.branches user: 'piotrmurach', repo: 'github' do |branch|
puts branch.name
end 1.4.1 Response BodyThe response = repos.branches user: 'piotrmurach', repo: 'github'
response.body # => Array of branches 1.4.2 Response HeadersEach response comes packaged with methods allowing for inspection of HTTP start line and headers. For example, to check for rate limits and status codes do: response = Github::Client::Repos.branches 'piotrmurach', 'github'
response.headers.ratelimit_limit # "5000"
response.headers.ratelimit_remaining # "4999"
response.headers.status # "200"
response.headers.content_type # "application/json; charset=utf-8"
response.headers.etag # "\"2c5dfc54b3fe498779ef3a9ada9a0af9\""
response.headers.cache_control # "public, max-age=60, s-maxage=60" 1.4.3 Response SuccessIf you want to verify if the response was success, namely, that the response = Github::Client::Repos.branches 'piotrmurach', 'github'
response.success? # => true 1.5 Request HeadersIt is possible to specify additional header information which will be added to the final request. For example, to set events = Github::Client::Activity::Events.new
events.public headers: {
'X-Poll-Interval': 60,
'ETag': "a18c3bded88eb5dbb5c849a489412bf3"
} 1.5.1 Media TypesIn order to set custom media types for a request use the accept header. By using the issues = Github::Client::Issues.new
issues.get 'piotrmurach', 'github', 108, accept: 'application/vnd.github.raw' 2 ConfigurationThe github_api provides ability to specify global configuration options. These options will be available to all api calls. 2.1 BasicThe configuration options can be set by using the Github.configure do |c|
c.basic_auth = "login:password"
c.adapter = :typheous
c.user = 'piotrmurach'
c.repo = 'finite_machine'
end Alternatively, you can configure the settings by passing a block to an instance like: Github.new do |c|
c.endpoint = 'https://github.company.com/api/v3'
c.site = 'https://github.company.com'
c.upload_endpoint = 'https://github.company.com/api/uploads'
end or simply by passing hash of options to an instance like so github = Github.new basic_auth: 'login:password',
adapter: :typheous,
user: 'piotrmurach',
repo: 'finite_machine' The following is the full list of available configuration options: adapter # Http client used for performing requests. Default :net_http
auto_pagination # Automatically traverse requests page links. Default false
basic_auth # Basic authentication in form login:password.
client_id # Oauth client id.
client_secret # Oauth client secret.
connection_options # Hash of connection options.
endpoint # Enterprise API endpoint. Default: 'https://api.github.com'
oauth_token # Oauth authorization token.
org # Global organization used in requests if none provided
per_page # Number of items per page. Max of 100. Default 30.
repo # Global repository used in requests in none provided
site # enterprise API web endpoint
ssl # SSL settings in hash form.
user # Global user used for requests if none provided
user_agent # Custom user agent name. Default 'Github API Ruby Gem' 2.2 AdvancedThe github_api will use the default middleware stack which is exposed by calling Github.configure do |c|
c.stack.insert_after Github::Response::Helpers, CustomMiddleware
end Furthermore, you can build your entire custom stack and specify other connection options such as Github.new do |c|
c.adapter :excon
c.stack do |builder|
builder.use Github::Response::Helpers
builder.use Github::Response::Jsonize
end
end 2.3 SSLBy default requests over SSL are set to OpenSSL::SSL::VERIFY_PEER. However, you can turn off peer verification by github = Github.new ssl: { verify: false } If your client fails to find CA certs, you can pass other SSL options to specify exactly how the information is sourced ssl: {
client_cert: "/usr/local/www.example.com/client_cert.pem"
client_key: "/user/local/www.example.com/client_key.pem"
ca_file: "example.com.cert"
ca_path: "/etc/ssl/"
} For instance, download CA root certificates from Mozilla cacert and point ca_file at your certificate bundle location. This will allow the client to verify the github.com ssl certificate as authentic. 2.4 CachingCaching is supported through the Add the gem to your Gemfile: gem 'faraday-http-cache' You can now configure cache parameters as follows Github.configure do |config|
config.stack = proc do |builder|
builder.use Faraday::HttpCache, store: Rails.cache
end
end More details on the available options can be found in the gem's own documentation: https://github.com/plataformatec/faraday-http-cache#faraday-http-cache 3 Authentication3.1 BasicTo start making requests as authenticated user you can use your GitHub username and password like so Github.new basic_auth: 'login:password' Though this method is convenient you should strongly consider using 3.2 Authorizations API3.2.1 For a UserTo create an access token through the GitHub Authorizations API, you are required to pass your basic credentials and scopes you wish to have for the authentication token. github = Github.new basic_auth: 'login:password'
github.auth.create scopes: ['repo'], note: 'admin script' You can add more than one scope from the 3.2.2 For an AppFurthermore, to create auth token for an application you need to pass github = Github.new basic_auth: 'login:password'
github.auth.app.create 'client-id', scopes: ['repo'] In order to revoke auth token(s) for an application you must use basic authentication with github = Github.new basic_auth: "client_id:client_secret"
github.auth.app.delete 'client-id' Revoke a specific app token. github.auth.app.delete 'client-id', 'access-token' 3.3 ScopesYou can check OAuth scopes you have by: github = Github.new oauth_token: 'token'
github.scopes.list # => ['repo'] or inidividually for a given user: github = Github.new
github.scopes.list 'token' To list the scopes that the particular GitHub API action checks for do: repos = Github::Client::Repos.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论