Class: BrocadeAPIClient::JSONRestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/BrocadeAPIClient/httpclient.rb

Overview

Class for talking to API

Constant Summary collapse

USER_AGENT =
'ruby-brocadeclient'.freeze
ACCEPT_TYPE =
'application/vnd.brocade.networkadvisor+json;version=v1'.freeze
'WStoken'.freeze
CONTENT_TYPE =
'application/vnd.brocade.networkadvisor+json;version=v1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url, secure = false, http_log_debug = false, client_logger = nil) ⇒ JSONRestClient

Returns a new instance of JSONRestClient.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/BrocadeAPIClient/httpclient.rb', line 26

def initialize(api_url, secure = false, http_log_debug = false,
               client_logger = nil)
  @api_url = api_url
  @secure = secure
  @http_log_debug = http_log_debug
  @session_key = nil
  @client_logger = client_logger
  @httparty_log_level = :info
  @httparty_log_format = :logstash
  set_debug_flag
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



22
23
24
# File 'lib/BrocadeAPIClient/httpclient.rb', line 22

def api_url
  @api_url
end

#http_log_debugObject

Returns the value of attribute http_log_debug.



22
23
24
# File 'lib/BrocadeAPIClient/httpclient.rb', line 22

def http_log_debug
  @http_log_debug
end

#log_levelObject

Returns the value of attribute log_level.



22
23
24
# File 'lib/BrocadeAPIClient/httpclient.rb', line 22

def log_level
  @log_level
end

#loggerObject

Returns the value of attribute logger.



22
23
24
# File 'lib/BrocadeAPIClient/httpclient.rb', line 22

def logger
  @logger
end

#secureObject

Returns the value of attribute secure.



22
23
24
# File 'lib/BrocadeAPIClient/httpclient.rb', line 22

def secure
  @secure
end

#session_keyObject

Returns the value of attribute session_key.



22
23
24
# File 'lib/BrocadeAPIClient/httpclient.rb', line 22

def session_key
  @session_key
end

#timeoutObject

Returns the value of attribute timeout.



22
23
24
# File 'lib/BrocadeAPIClient/httpclient.rb', line 22

def timeout
  @timeout
end

Instance Method Details

#authenticate(user, password, _optional = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/BrocadeAPIClient/httpclient.rb', line 46

def authenticate(user, password, _optional = nil)
  @username = user
  @password = password
  @session_key = nil
  auth_url = '/login'
  headers, body = post(auth_url)
  @session_key = headers['WStoken']
  JSON.parse(body)
rescue StandardError
  raise BrocadeAPIClient::ConnectionError
end

#delete(url, **kwargs) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/BrocadeAPIClient/httpclient.rb', line 96

def delete(url, **kwargs)
  headers, _payload = headers_payload(kwargs)
  response = HTTParty.delete(api_url + url,
                             headers: headers,
                             verify: false, logger: @client_logger,
                             log_level: @httparty_log_level,
                             log_format: @httparty_log_format)
  validate_answer(response)
end

#get(url, **kwargs) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/BrocadeAPIClient/httpclient.rb', line 63

def get(url, **kwargs)
  headers, _payload = headers_payload(kwargs)
  response = HTTParty.get(api_url + url,
                          headers: headers,
                          verify: false, logger: @client_logger,
                          log_level: @httparty_log_level,
                          log_format: @client_logger)
  validate_answer(response)
end

#headers_payload(**kwargs) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/BrocadeAPIClient/httpclient.rb', line 128

def headers_payload(**kwargs)
  kwargs['headers'] = kwargs.fetch('headers', {})
  if session_key
    kwargs['headers'] = kwargs.fetch('headers', {})
    kwargs['headers'][SESSION_COOKIE_NAME] = @session_key
  else
    kwargs['headers']['WSUsername'] = @username
    kwargs['headers']['WSPassword'] = @password
  end
  kwargs['headers']['User-Agent'] = USER_AGENT
  kwargs['headers']['Accept'] = ACCEPT_TYPE
  if kwargs.key?(:body)
    kwargs['headers']['Content-Type'] = CONTENT_TYPE
    kwargs[:body] = kwargs[:body].to_json
    payload = kwargs[:body]
  else
    payload = nil
  end
  [kwargs['headers'], payload]
end

#post(url, **kwargs) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/BrocadeAPIClient/httpclient.rb', line 73

def post(url, **kwargs)
  headers, payload = headers_payload(kwargs)
  response = HTTParty.post(api_url + url,
                           headers: headers,
                           body: payload,
                           verify: false,
                           logger: @client_logger,
                           log_level: @httparty_log_level,
                           log_format: @httparty_log_format)
  validate_answer(response)
end

#put(url, **kwargs) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/BrocadeAPIClient/httpclient.rb', line 85

def put(url, **kwargs)
  headers, payload = headers_payload(kwargs)
  response = HTTParty.put(api_url + url,
                          headers: headers,
                          body: payload,
                          verify: false, logger: @client_logger,
                          log_level: @httparty_log_level,
                          log_format: @httparty_log_format)
  validate_answer(response)
end

#set_debug_flagObject

This turns on/off http request/response debugging output to console



39
40
41
42
43
44
# File 'lib/BrocadeAPIClient/httpclient.rb', line 39

def set_debug_flag
  if @http_log_debug
    @httparty_log_level = :debug
    @httparty_log_format = :curl
  end
end

#unauthenticateObject



120
121
122
123
124
125
126
# File 'lib/BrocadeAPIClient/httpclient.rb', line 120

def unauthenticate
  # delete the session on the brocade network advisor
  unless @session_key.nil?
    post('/logout')
    @session_key = nil
  end
end

#url(api_url) ⇒ Object



58
59
60
61
# File 'lib/BrocadeAPIClient/httpclient.rb', line 58

def url(api_url)
  # should be http://<Server:Port>/rest
  @api_url = api_url.chomp('/')
end

#validate_answer(response) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/BrocadeAPIClient/httpclient.rb', line 106

def validate_answer(response)
  headers = response.headers
  body = response.parsed_response
  code_array = %w[200 204]
  unless code_array.include?(response.code.to_s)
    if body.nil?
      exception = BrocadeAPIClient.exception_from_response(response, body)
      @client_logger.error(exception)
      raise exception
    end
  end
  [headers, body]
end