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.

Parameters:

  • apis

    a hash of Swagger AEM client’s API instances

  • spec

    ruby_aem specification



37
38
39
40
# File 'lib/ruby_aem/client.rb', line 37

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.

Parameters:

  • key

    optional param key

  • value

    optional param value

  • params

    combined list of required and optional parameters

  • call_params

    API call parameters



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ruby_aem/client.rb', line 92

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
  else
    if 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
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.

Parameters:

  • clazz

    the class name of the caller resource

  • action

    the action of the API call

  • call_params

    API call parameters

Returns:

  • RubyAem::Result



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
81
82
83
84
# File 'lib/ruby_aem/client.rb', line 52

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 { |key, 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.

Parameters:

  • response

    response containing HTTP status code, body, and headers

  • responses_spec

    a list of response specifications as configured in conf/spec.yaml

  • call_params

    API call parameters

Returns:

  • RubyAem::Result

Raises:

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



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ruby_aem/client.rb', line 125

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']
    result = 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