Class: RubyAem::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_aem/client.rb

Overview

Client class makes Swagger AEM API calls and handles the response as configured in conf/spec.yaml .

Instance Method Summary collapse

Constructor Details

#initialize(apis, spec) ⇒ Object

Initialise a client.



34
35
36
37
# File 'lib/ruby_aem/client.rb', line 34

def initialize(apis, spec)
  @apis = apis
  @spec = spec
end

Instance Method Details

#add_optional_param(key, value, params, call_params) ⇒ Object

Add optional param into params list.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruby_aem/client.rb', line 88

def add_optional_param(key, value, params, call_params)
  # if there is no value in optional param spec,
  # then only add optional param that is set in call parameters
  if !value
    if call_params.key? key.to_sym
      params[-1][key.to_sym] = call_params[key.to_sym]
    end
  # if value is provided in optional param spec,
  # then apply variable interpolation the same way as required param
  elsif value.class == String
    if value == '__FILE__'
      File.open("#{call_params[:file_path]}/#{call_params[:package_name]}-#{call_params[:package_version]}.zip", 'r') { |file|
        params[-1][key.to_sym] = file
      }
    else
      params[-1][key.to_sym] = value % call_params
    end
  else
    params[-1][key.to_sym] = value
  end
end

#call(clazz, action, call_params) ⇒ Object

Make an API call using the relevant Swagger AEM API client. Clazz and action parameters are used to identify the action, API, and params from ruby_aem specification, alongside the response handlers. Call parameters are used to construct HTTP request parameters based on the specification.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby_aem/client.rb', line 49

def call(clazz, action, call_params)
  resource_name = clazz.name.downcase.sub('rubyaem::resources::', '')
  resource = @spec[resource_name]
  action_spec = resource['actions'][action]

  api = @apis[action_spec['api'].to_sym]
  operation = action_spec['operation']

  params = []
  required_params = action_spec['params']['required'] || {}
  required_params.each_value { |value|
    params.push(value % call_params)
  }
  params.push({})
  optional_params = action_spec['params']['optional'] || {}
  optional_params.each { |key, value|
    add_optional_param(key, value, params, call_params)
  }

  base_responses_spec = resource['responses'] || {}
  action_responses_spec = action_spec['responses'] || {}
  responses_spec = base_responses_spec.merge(action_responses_spec)

  begin
    method = RubyAem::Swagger.operation_to_method(operation)
    data, status_code, headers = api.send("#{method}_with_http_info", *params)
    response = RubyAem::Response.new(status_code, data, headers)
  rescue SwaggerAemClient::ApiError => err
    response = RubyAem::Response.new(err.code, err.response_body, err.response_headers)
  end
  handle(response, responses_spec, call_params)
end

#handle(response, responses_spec, call_params) ⇒ Object

Handle a response based on status code and a given list of response specifications. If none of the response specifications contains the status code, a failure result will then be returned.

Raises:

  • RubyAem::Error when the response status code is unexpected



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ruby_aem/client.rb', line 119

def handle(response, responses_spec, call_params)
  if responses_spec.key?(response.status_code)
    response_spec = responses_spec[response.status_code]
    handler = response_spec['handler']
    Handlers.send(handler, response, response_spec, call_params)
  else
    message = "Unexpected response\nstatus code: #{response.status_code}\nheaders: #{response.headers}\nbody: #{response.body}"
    result = Result.new(message, response)
    raise RubyAem::Error.new(message, result)
  end
end