Skip to content

Commit

Permalink
DEVX-9024: Fixing bug with HTTP Options (#319)
Browse files Browse the repository at this point in the history
* Fixing bug with HTTP Options
  • Loading branch information
superchilled authored Oct 23, 2024
1 parent 6e799a0 commit 68ff52b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 7.27.1

* Fixes a bug with setting options on the HTTP client. [#319](https://github.com/Vonage/vonage-ruby-sdk/pull/319)

# 7.27.0

* Updates Messages API implementation to add RCS channel as well as a new `PATCH` endpoint for RCS message revocation and WhatsApp Mark as Read features. [#316](https://github.com/Vonage/vonage-ruby-sdk/pull/316)
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ need a Vonage account. Sign up [for free at vonage.com][signup].
* [Logging](#logging)
* [Exceptions](#exceptions)
* [Overriding the default hosts](#overriding-the-default-hosts)
* [HTTP Client Configuration](#http-client-configuration)
* [JWT authentication](#jwt-authentication)
* [Webhook signatures](#webhook-signatures)
* [Pagination](#pagination)
Expand Down Expand Up @@ -179,6 +180,33 @@ client = Vonage::Client.new(

By default the hosts are set to `api.nexmo.com` and `rest.nexmo.com`, respectively.

### HTTP Client Configuration

It is possible to set configuration options on the HTTP client. This can be don in a couple of ways.

1. Using an `:http` key during `Vonage::Client` instantiation, for example:
```ruby
client = Vonage::Client.new(
api_key: 'YOUR-API-KEY',
api_secret: 'YOUR-API-SECRET',
http: {
max_retries: 1
}
)
```

2. By using the `http=` setter on the `Vonage::Config` object, for example:
```ruby
client = Vonage::Client.new(
api_key: 'YOUR-API-KEY',
api_secret: 'YOUR-API-SECRET'
)
client.config.http = { max_retries: 1 }
```

The Vonage Ruby SDK uses the [`Net::HTTP::Persistent` library](https://github.com/drbrain/net-http-persistent) as an HTTP client. For available configuration options see [the documentation for that library](https://www.rubydoc.info/gems/net-http-persistent/3.0.0/Net/HTTP/Persistent).

### Webhook signatures

Certain Vonage APIs provide signed [webhooks](https://developer.vonage.com/en/getting-started/concepts/webhooks) as a means of verifying the origin of the webhooks. The exact signing mechanism varies depending on the API.
Expand Down
6 changes: 3 additions & 3 deletions lib/vonage/http.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# typed: strict
# frozen_string_literal: true
require 'net/http'
require 'net/http/persistent'

module Vonage
module HTTP
Expand All @@ -21,7 +21,7 @@ def initialize(hash)
end
end

sig { params(http: Net::HTTP).returns(T::Hash[Symbol, T.untyped]) }
sig { params(http: Net::HTTP::Persistent).returns(T::Hash[Symbol, T.untyped]) }
def set(http)
@hash.each do |name, value|
http.public_send(defined_options.fetch(name), value)
Expand All @@ -34,7 +34,7 @@ def set(http)
def defined_options
@defined_options = T.let(@defined_options, T.nilable(T::Hash[Symbol, T.untyped]))

@defined_options ||= Net::HTTP.instance_methods.grep(/\w=\z/).each_with_object({}) do |name, hash|
@defined_options ||= Net::HTTP::Persistent.instance_methods.grep(/\w=\z/).each_with_object({}) do |name, hash|
hash[name.to_s.chomp('=').to_sym] = name
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vonage/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# typed: strong

module Vonage
VERSION = '7.27.0'
VERSION = '7.27.1'
end
17 changes: 12 additions & 5 deletions test/vonage/http_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ def read_timeout
5
end

def proxy_address
'localhost'
def proxy_host
'example.com'
end

def proxy_port
4567
end

def ca_file
Expand All @@ -23,13 +27,16 @@ def test_invalid_option_raises_argument_error
end

def test_set_method
http = Net::HTTP.new('example.com', Net::HTTP.https_default_port, p_addr = nil)
http = Net::HTTP::Persistent.new

proxy = URI::HTTP.build(host: proxy_host, port: proxy_port)

options = Options.new(read_timeout: read_timeout, proxy_address: proxy_address, ca_file: ca_file)
options = Options.new(read_timeout: read_timeout, proxy: proxy, ca_file: ca_file)
options.set(http)

assert_equal read_timeout, http.read_timeout
assert_equal proxy_address, http.proxy_address
assert_equal proxy_host, http.proxy_uri.hostname
assert_equal proxy_port, http.proxy_uri.port
assert_equal ca_file, http.ca_file
end
end

0 comments on commit 68ff52b

Please sign in to comment.