Skip to content

Commit

Permalink
Add Accept-Encoding header to HTTP requests (closes #80)
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Jul 28, 2024
1 parent d29de93 commit 77643f0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Add deprecation warning for `Pricing#traffic`
- Add deprecation warning for `ServerType#included_traffic`
- Fix crash on prices when using `Pricing` API
- Add support for (de-)compression of HTTP requests

## HCloud v2.0.0 (2024-01-10)

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ ssh_keys = HCloud::SSHKey.all # Will block until remaining requests have regener
Since rate limits are per hour and per project, using multiple clients at the same time will interfere with the rate limiting mechanism.
To prevent this, wrap client calls in a loop that retries the call after it fails with a `HCloud::RateLimitExceeded` error.

### Compression

Enable compression by passing an appropriate `encoding` option to `HCloud::Client.new`.
Current supported options are `nil`, `gzip` and `brotli`.
Compression is disabled by default.

```ruby
client = HCloud::Client.new(access_token: "my_access_token", encoding: "gzip")
```

## Testing

```ssh
Expand Down
8 changes: 5 additions & 3 deletions lib/hcloud/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ class Client

self.connection = NilConnection.new

attr_reader :access_token, :endpoint, :logger, :rate_limit
attr_reader :access_token, :endpoint, :logger, :rate_limit, :timeout, :encoding

def initialize(access_token:, endpoint: "https://api.hetzner.cloud/v1", logger: Logger.new("/dev/null"), rate_limit: false)
def initialize(access_token:, endpoint: "https://api.hetzner.cloud/v1", logger: Logger.new("/dev/null"), rate_limit: false, timeout: 10, encoding: nil)
@access_token = access_token
@endpoint = endpoint
@logger = logger
@rate_limit = rate_limit
@timeout = timeout
@encoding = encoding
end

delegate :get, :put, :post, :delete, to: :http

private

def http
@http ||= HTTP.new(access_token, endpoint, logger, rate_limit)
@http ||= HTTP.new(access_token, endpoint, logger, rate_limit, timeout, encoding)
end
end
end
8 changes: 5 additions & 3 deletions lib/hcloud/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

module HCloud
class HTTP
attr_reader :access_token, :endpoint, :logger, :rate_limit, :timeout
attr_reader :access_token, :endpoint, :logger, :rate_limit, :timeout, :encoding

def initialize(access_token, endpoint, logger, rate_limit = false, timeout = 10)
def initialize(access_token, endpoint, logger, rate_limit = false, timeout = 10, encoding = nil)
@access_token = access_token
@endpoint = endpoint
@logger = logger
@rate_limit = rate_limit
@timeout = timeout
@encoding = encoding
end

def get(path, params = {})
Expand Down Expand Up @@ -78,7 +79,8 @@ def delete(path)

def http
@http ||= ::HTTP
.headers(accept: "application/json", user_agent: "#{HCloud::NAME}/#{HCloud::VERSION}")
.headers(user_agent: "#{HCloud::NAME}/#{HCloud::VERSION}", "Accept-Encoding" => encoding)
.accept("application/json")
.timeout(timeout)
.use(logging: { logger: logger })
.then { |h| rate_limit ? h.use(:rate_limiter) : h }
Expand Down

0 comments on commit 77643f0

Please sign in to comment.