Class: MgmtConsole::Client

Inherits:
Object
  • Object
show all
Includes:
Authentication, EngageCommunities, EngageEnvironments, EngageInstances, EngageServers, ServerEnvironments, SpigitConfigs, Configurable
Defined in:
lib/mgmt_console/client.rb,
lib/mgmt_console/client/engage_servers.rb,
lib/mgmt_console/client/spigit_configs.rb,
lib/mgmt_console/client/engage_instances.rb,
lib/mgmt_console/client/engage_communities.rb,
lib/mgmt_console/client/engage_environments.rb,
lib/mgmt_console/client/server_environments.rb

Overview

Client for the Mgmt Console API

Defined Under Namespace

Modules: EngageCommunities, EngageEnvironments, EngageInstances, EngageServers, ServerEnvironments, SpigitConfigs

Constant Summary collapse

CONVENIENCE_HEADERS =

Header keys that can be passed in options hash to #get,#head

Set.new([:accept, :content_type])

Instance Attribute Summary

Attributes included from Configurable

#access_token, #api_endpoint, #auto_paginate, #client_id, #client_secret, #connection_options, #default_media_type, #login, #middleware, #netrc, #netrc_file, #password, #per_page, #proxy, #ssl_verify, #user_agent, #web_endpoint

Instance Method Summary collapse

Methods included from SpigitConfigs

#create_spigit_config, #spigit_configs, #update_spigit_config

Methods included from EngageInstances

#create_engage_instance, #engage_instances, #update_engage_instance

Methods included from EngageServers

#create_engage_server, #engage_servers, #update_engage_server

Methods included from EngageCommunities

#create_engage_communities, #engage_communities

Methods included from EngageEnvironments

#create_engage_environment, #engage_environments, #update_engage_environment

Methods included from ServerEnvironments

#create_server_environment, #server_environments

Methods included from Configurable

#configure, keys, #reset!

Methods included from Authentication

#token_authenticated?, #user_authenticated?

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



31
32
33
34
35
36
# File 'lib/mgmt_console/client.rb', line 31

def initialize(options = {})
  # Use options passed in, but fall back to module defaults
  MgmtConsole::Configurable.keys.each do |key|
    instance_variable_set(:"@#{key}", resolve_opt(options, key))
  end
end

Instance Method Details

#access_token=(value) ⇒ Object

Set OAuth access token for authentication

Parameters:

  • value (String)

    40 character GitHub OAuth access token



181
182
183
184
# File 'lib/mgmt_console/client.rb', line 181

def access_token=(value)
  reset_agent
  @access_token = value
end

#agentSawyer::Agent

Hypermedia agent for the GitHub API

Returns:

  • (Sawyer::Agent)


151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mgmt_console/client.rb', line 151

def agent
  @agent ||= Sawyer::Agent.new(api_endpoint, sawyer_options) do |http|
    http.headers[:accept] = default_media_type
    http.headers[:content_type] = "application/json"
    http.headers[:user_agent] = user_agent
    http.ssl[:verify] = ssl_verify

    if token_authenticated?
      http.headers['X-User-Token'] = @access_token
    end
  end
end

#delete(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP DELETE request

Parameters:

Returns:

  • (Sawyer::Resource)


101
102
103
# File 'lib/mgmt_console/client.rb', line 101

def delete(url, options = {})
  request :delete, url, options
end

#get(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP GET request

Parameters:

Returns:

  • (Sawyer::Resource)


65
66
67
# File 'lib/mgmt_console/client.rb', line 65

def get(url, options = {})
  request :get, url, parse_query_and_convenience_headers(options)
end

#head(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP HEAD request

Parameters:

Returns:

  • (Sawyer::Resource)


110
111
112
# File 'lib/mgmt_console/client.rb', line 110

def head(url, options = {})
  request :head, url, parse_query_and_convenience_headers(options)
end

#inspectString

Text representation of the client, masking tokens and passwords

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
# File 'lib/mgmt_console/client.rb', line 49

def inspect
  inspected = super

  # Only show last 4 of token, secret
  if @access_token
    inspected = inspected.gsub! @access_token, "#{'*'*36}#{@access_token[36..-1]}"
  end

  inspected
end

#last_responseSawyer::Response

Response for last HTTP request

Returns:

  • (Sawyer::Response)


174
175
176
# File 'lib/mgmt_console/client.rb', line 174

def last_response
  @last_response if defined? @last_response
end

#octokit_warn(*message) ⇒ nil

Wrapper around Kernel#warn to print warnings unless OCTOKIT_SILENT is set to true.

Returns:

  • (nil)


190
191
192
193
194
# File 'lib/mgmt_console/client.rb', line 190

def octokit_warn(*message)
  unless ENV['OCTOKIT_SILENT']
    warn message
  end
end

#paginate(url, options = {}, &block) ⇒ Sawyer::Resource

Make one or more HTTP GET requests, optionally fetching the next page of results from URL in Link response header based on value in MgmtConsole::Configurable#auto_paginate.

Parameters:

  • url (String)
  • options (Hash) (defaults to: {})

    Query and header params for request

  • block (Block)

    Block to perform the data concatination of the multiple requests. The block is called with two parameters, the first contains the contents of the requests so far and the second parameter contains the latest response.

Returns:

  • (Sawyer::Resource)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mgmt_console/client.rb', line 125

def paginate(url, options = {}, &block)
  opts = parse_query_and_convenience_headers(options.dup)
  if @auto_paginate || @per_page
    opts[:query][:per_page] ||=  @per_page || (@auto_paginate ? 100 : nil)
  end

  data = request(:get, url, opts)

  if @auto_paginate
    while @last_response.rels[:next]# && rate_limit.remaining > 0
      @last_response = @last_response.rels[:next].get
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.data) if @last_response.data.is_a?(Array)
      end
    end

  end

  data
end

#patch(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PATCH request

Parameters:

Returns:

  • (Sawyer::Resource)


92
93
94
# File 'lib/mgmt_console/client.rb', line 92

def patch(url, options = {})
  request :patch, url, options
end

#post(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP POST request

Parameters:

Returns:

  • (Sawyer::Resource)


74
75
76
# File 'lib/mgmt_console/client.rb', line 74

def post(url, options = {})
  request :post, url, options
end

#put(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PUT request

Parameters:

Returns:

  • (Sawyer::Resource)


83
84
85
# File 'lib/mgmt_console/client.rb', line 83

def put(url, options = {})
  request :put, url, options
end

#rootSawyer::Resource

Fetch the root resource for the API

Returns:

  • (Sawyer::Resource)


167
168
169
# File 'lib/mgmt_console/client.rb', line 167

def root
  get "/"
end

#same_options?(opts) ⇒ Boolean

Compares client options to a Hash of requested options

Parameters:

  • opts (Hash)

    Options to compare with current client options

Returns:

  • (Boolean)


42
43
44
# File 'lib/mgmt_console/client.rb', line 42

def same_options?(opts)
  opts.hash == options.hash
end