class HikOpenapi::Client

Attributes

app_key[RW]
app_secret[RW]
host[RW]
prefix[RW]
proxy[RW]
timeouts[RW]
user_agent[W]

Public Class Methods

new(options = {}) { |self| ... } click to toggle source
# File lib/hik_openapi.rb, line 13
def initialize(options = {})
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
  yield(self) if block_given?
end

Public Instance Methods

get(path, params) click to toggle source
# File lib/hik_openapi.rb, line 20
def get(path, params)
  request(:get, path, params)
end
post(path, params) click to toggle source
# File lib/hik_openapi.rb, line 24
def post(path, params)
  request(:post, path, params)
end

Private Instance Methods

http_client() click to toggle source
# File lib/hik_openapi.rb, line 67
def http_client
  client = proxy ? HTTP.via(*proxy.values_at(:host, :port, :username, :password).compact) : HTTP
  client = client.timeout(connect: timeouts[:connect], read: timeouts[:read], write: timeouts[:write]) if timeout_keys_defined
  client
end
perform() click to toggle source
# File lib/hik_openapi.rb, line 40
def perform
  ctx = OpenSSL::SSL::SSLContext.new
  ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE

  response = http_client.headers(sign_headers(@headers)).public_send(@request_method, @uri.to_s, request_options.merge(ssl_context: ctx))
  response_body = response.code == 200 ? symbolize_keys!(response.parse) : {}
  response_headers = response.headers
  {
    headers: response_headers,
    body: response_body,
  }
end
request(request_method, path, params) click to toggle source
# File lib/hik_openapi.rb, line 30
def request(request_method, path, params)
  @uri = host + prefix + path
  @request_method = request_method
  @path = path
  @params = params
  @options_key = {get: :params, post: :json}[request_method] || :form
  @headers = {'Content-Type': 'application/json', 'Accept': '*/*', 'x-ca-timestamp': (Time.now.to_f * 1000).to_i.to_s, 'x-ca-nonce': SecureRandom.uuid, 'x-ca-key': app_key}
  perform
end
request_options() click to toggle source
# File lib/hik_openapi.rb, line 73
def request_options
  {@options_key => @params}
end
sign(method, path, headers) click to toggle source
# File lib/hik_openapi.rb, line 83
def sign(method, path, headers)
  sign_str = headers.reject { |a| a.to_s.start_with?('x-ca') }.values.sort
  sign_str = sign_str.unshift(method.to_s.upcase).push(path).join("\n")

  Base64.encode64(
    OpenSSL::HMAC.digest(
      OpenSSL::Digest.new('sha256'),
      app_secret,
      sign_str
    )
  ).strip
end
sign_headers(headers) click to toggle source
# File lib/hik_openapi.rb, line 77
def sign_headers(headers)
  headers.merge!({
                   "x-ca-signature": sign(@request_method, (prefix + @path), headers),
                 })
end
symbolize_keys!(object) click to toggle source
# File lib/hik_openapi.rb, line 53
def symbolize_keys!(object)
  case object
  when Array
    object.each_with_index { |val, index| object[index] = symbolize_keys!(val) }
  when Hash
    object.dup.each_key { |key| object[key.to_sym] = symbolize_keys!(object.delete(key)) }
  end
  object
end
timeout_keys_defined() click to toggle source
# File lib/hik_openapi.rb, line 63
def timeout_keys_defined
  (%i[write connect read] - (timeouts&.keys || [])).empty?
end