-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Add httpclient as alternative HTTP library
- Loading branch information
1 parent
4e58f80
commit 68bcfa7
Showing
8 changed files
with
221 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Kubeclient | ||
# Defines the common API for libraries to be used by Kubeclient for making HTTP requests. | ||
# To create a wrapper for a library, create a new class that inherits from this class | ||
# and override the +request+ method. See +RestClientWrapper+ or +HTTPClientWrapper+ for examples. | ||
class HTTPWrapper | ||
def delete(path = nil, **options) | ||
request(:delete, path, **options) | ||
end | ||
|
||
def get(path = nil, **options) | ||
request(:get, path, **options) | ||
end | ||
|
||
def patch(path = nil, **options) | ||
request(:patch, path, **options) | ||
end | ||
|
||
def post(path = nil, **options) | ||
request(:post, path, **options) | ||
end | ||
|
||
def put(path = nil, **options) | ||
request(:put, path, **options) | ||
end | ||
|
||
def request | ||
raise 'Must implement the `request` method.' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require_relative 'http_wrapper' | ||
|
||
module Kubeclient | ||
# Wraps the API of +httpclient+ gem to be used by Kubeclient for making HTTP requests. | ||
class HTTPClientWrapper < HTTPWrapper | ||
def initialize(url, options) | ||
@url = url | ||
@options = options | ||
@client = HTTPClient.new | ||
end | ||
|
||
attr_reader :client | ||
|
||
def request(method, path = nil, **options) | ||
uri = [@url, path].compact.join('/') | ||
query = options[:params] if options.key?(:params) | ||
body = options[:body] if options.key?(:body) | ||
headers = options[:headers] if options.key(:headers) | ||
|
||
@client.request(method, uri, query, body, headers) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require_relative 'http_wrapper' | ||
|
||
module Kubeclient | ||
# Wraps the API of +rest_client+ gem to be used by Kubeclient for making HTTP requests. | ||
class RestClientWrapper < HTTPWrapper | ||
def initialize(url, options) | ||
@client = RestClient::Resource.new(url, options) | ||
end | ||
|
||
attr_reader :client | ||
|
||
def request(method, path = nil, **options) | ||
url = path.nil? ? @client.url : @client[path].url | ||
headers_with_params = create_headers_with_params(options[:headers], options[:params]) | ||
payload = options[:body] | ||
|
||
execute_options = @client.options.merge( | ||
method: method, | ||
url: url, | ||
headers: headers_with_params, | ||
payload: payload | ||
) | ||
RestClient::Request.execute(execute_options) | ||
end | ||
|
||
private | ||
|
||
# In RestClient, you pass params hash inside the headers hash, in :params key. | ||
def create_headers_with_params(headers, params) | ||
headers_with_params = {}.merge(headers || {}) | ||
headers_with_params[:params] = params if params | ||
headers_with_params | ||
end | ||
end | ||
end |
Oops, something went wrong.