Skip to content
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
2 changes: 1 addition & 1 deletion lib/async/http/internet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Async
module HTTP
class Internet
def initialize(**options)
@clients = Hash.new
@clients = options.delete(:clients) || Hash.new
@options = options
end

Expand Down
39 changes: 39 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,45 @@ Async do
end
```

### Private Certificate Authentication

You provide private certificates for authentication.

``` ruby
require 'async'
require 'async/http'

# These are generated from a signed certificate request.
ssl_private_key = OpenSSL::PKey::RSA.new(ENV['PRIVATE_SSL_KEY_AS_STRING']))
ssl_certificate = OpenSSL::X509::Certificate.new(OpenSSL::X509::Certificate.new(ENV['PUBLIC_SSL_CERTIFICATE_AS_STRING'])

ssl_context = OpenSSL::SSL::SSLContext.new.tap do |context|
context.cert = ssl_certificate
context.key = ssl_private_key
end
api_url = 'https://www.example-api.com'
auth_url = 'https://www.example-auth.com'

api_endpoint = Async::HTTP::Endpoint.parse(api_url, ssl_context: ssl_context)
auth_endpoint = Async::HTTP::Endpoint.parse(auth_url, ssl_context: ssl_context)

clients = {
api_url => Async::HTTP::Client.new(api_endpoint),
auth_url => Async::HTTP::Client.new(auth_endpoint)
}


Async do
internet = Async::HTTP::Internet.new(clients: clients)
token_response = internet.get("#{internet}/auth/token")
token = JSON.parse(token_response.read).dig("token")
headers = [['Authorization', "Bearer #{token}"]]
widgets_response = internet.get("#{api_url}/widgets", headers )

pp widgets_response.status, widgets_response.headers.fields, widgets_response.read
end
```

### Timeouts

Here's a basic example with a timeout:
Expand Down