Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional Requests #142

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,34 @@ client = Octokit::Client.new(:login => "me", :oauth_token => "oauth2token")
client.follow("sferik")
```

## Rate Limiting and Conditional Requests
GitHub limits API requests to 5000 per hour.

```ruby
client = Octokit::Client.new(:login => "me", :oauth_token => "oauth2token")
client.ratelimit_remaining # 5000
client.repositories
client.ratelimit_remaining # 4999
```

You can make conditional requests which will only return data to you if there
have been changes since your last request. Pass either the `since` or `etag`
option to any API call to perform a conditional request, and immediately after
the request is complete the client will have the attributes `last_modified` and
`etag` populated to be used for the next identical API call.

```ruby
client = Octokit::Client(:login => "me", :oauth_token => "oauth2token")
client.ratelimit_remaining # 5000
client.repositories # (Returns an array of repositories)
client.ratelimit_remaining # 4999
repos_last_modified = client.last_modified

client.repositories(nil,
:since => repos_last_modified) # nil
client.ratelimit_remaining # 4999
```

## Using with GitHub Enterprise

To use with [GitHub Enterprise](https://enterprise.github.com/), you'll need to
Expand Down
1 change: 1 addition & 0 deletions lib/octokit/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
module Octokit
class Client
attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
attr_accessor(:last_modified, :etag)

def initialize(options={})
options = Octokit.options.merge(options)
Expand Down
6 changes: 6 additions & 0 deletions lib/octokit/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def ratelimit_remaining
def request(method, path, options, version, authenticate, raw, force_urlencoded)
path.sub(%r{^/}, '') #leading slash in path fails in github:enterprise
response = connection(authenticate, raw, version, force_urlencoded).send(method) do |request|
request.headers['If-Modified-Since'] = options.delete(:since) unless options[:since].nil?
request.headers['If-None-Match'] = options.delete(:etag) unless options[:etag].nil?

case method
when :delete, :get
if auto_traversal && per_page.nil?
Expand All @@ -58,6 +61,9 @@ def request(method, path, options, version, authenticate, raw, force_urlencoded)
request.headers['Host'] = Octokit.request_host if Octokit.request_host
end

self.last_modified = response.headers['Last-Modified']
self.etag = response.headers['ETag'].gsub('"', '') unless response.headers['ETag'].nil?

if raw
response
elsif auto_traversal && ( next_url = links(response)["next"] )
Expand Down