class Ketra::Client
The Ketra::Client
class is used for gaining an Access Token for authorization and performing GET and POST requests to the Ketra
API
Constants
- LOCAL_ENDPOINT_PREFIX
Endpoint prefix to be attached to the base url before the rest of the endpoint
- PRODUCTION_HOST
Production Host base url
- REMOTE_ENDPOINT_PREFIX
- REMOTE_PRODUCTION_HOST
- REMOTE_TEST_HOST
- TEST_HOST
Test Host base url
Attributes
Public Class Methods
Instantiate a new Ketra
Client
using the Client
ID and Client
Secret registered to your application.
@param [String] id the Client
ID value @param [String] secret the Client
Secret value @param [Hash] options the options to create the client with @option options [Symbol] :server (:production) which authorization server to use to get an Access Token (:production or :test) @option options [String] :redirect_uri the redirect uri for the authorization code OAuth2 grant type @option options [String] :hub_serial the serial number of the Hub to communicate with
# File lib/ketra/client.rb, line 28 def initialize(id, secret, options = {}) opts = options.dup @id = id @secret = secret @options = {:server => :production, :redirect_uri => 'urn:ietf:wg:oauth:2.0:oob', :hub_discovery_mode => :cloud, :api_mode => :local}.merge(opts) end
Public Instance Methods
OAuth Client
@return [OAuth2::Client] oauth2 client
# File lib/ketra/client.rb, line 72 def auth_client @auth_client ||= OAuth2::Client.new(Ketra.client_id, Ketra.client_secret, :site => host, :ssl => { :verify => false }) end
performs a GET Request using the OAuth2 Access Token and parses the result as JSON
@param [String] endpoint to be appended to the base url @param [Hash] params to be used as query params for the request @return [Hash] deserialized response hash
# File lib/ketra/client.rb, line 84 def get(endpoint, params = {}) JSON.parse access_token.get(url(endpoint), :params => params).body end
performs a POST request using the OAuth2 Access Token and parses the result as JSON
@param [String] endpoint to be appended to the base url @param [Hash] params except :query_params will be serialized into JSON and supplied as the body of the request @option params [Hash] :query_params to be used as the query params for the request @return [Hash] deserialized response hash
# File lib/ketra/client.rb, line 94 def post(endpoint, params = {}) internal_params = params.dup resp = access_token.post url(endpoint), :params => internal_params.delete(:query_params), :body => JSON.generate(internal_params), :headers => { 'Content-Type' => 'application/json' } JSON.parse resp.body end
Private Instance Methods
# File lib/ketra/client.rb, line 152 def cloud_discovery(serial_number) @hub_ip ||= perform_cloud_hub_discovery(serial_number) end
# File lib/ketra/client.rb, line 147 def discover_hub(serial_number) #TODO implement additional hub discovery modes cloud_discovery(serial_number) end
# File lib/ketra/client.rb, line 105 def host case options[:server] when :test TEST_HOST else PRODUCTION_HOST end end
# File lib/ketra/client.rb, line 143 def hub_ip @hub_ip || discover_hub(options[:hub_serial]) end
# File lib/ketra/client.rb, line 135 def local_url "https://#{hub_ip}/#{LOCAL_ENDPOINT_PREFIX}" end
# File lib/ketra/client.rb, line 156 def perform_cloud_hub_discovery(serial_number) response = auth_client.request :get, "#{host}/api/n4/v1/query" info = response.parsed["content"].detect { |h| h["serial_number"] == serial_number } raise RuntimeError, "Could not discover hub with serial: #{serial_number}" if info.nil? info["internal_ip"] end
# File lib/ketra/client.rb, line 114 def remote_host case options[:server] when :test REMOTE_TEST_HOST else REMOTE_PRODUCTION_HOST end end
# File lib/ketra/client.rb, line 139 def remote_url "#{remote_host}/#{options[:installation_id]}/#{options[:hub_serial]}/#{REMOTE_ENDPOINT_PREFIX}" end
# File lib/ketra/client.rb, line 123 def url(endpoint) #TODO implement additional api modes case options[:api_mode] when :remote base = remote_url else base = local_url end url = "#{base}/#{endpoint}" Addressable::URI.encode(url) end