Module: ThreeScale::Core::APIClient::Operations::ClassMethods

Includes:
Logger
Defined in:
lib/3scale/core/api_client/operations.rb

Instance Method Summary collapse

Instance Method Details

#api(method, attributes, options = {}) ⇒ Object

api method - talk with the remote HTTP service

method - HTTP method to use attributes - HTTP request body parameters options:

:prefix => string/symbol - an attribute prefix, '' for none, else default_prefix
:rprefix => string/symbol - same as above, but for parsing responses
:uri => string - sets the uri for this particular request
:raise => boolean - raise APIError on error, defaults to true
:build => boolean - create a new object with response's JSON if response is ok, defaults to false

block (optional) - receives two params: http status code and attributes

this block if present handles error responses, invalidates :raise option,
you should return an array of [raise (boolean), built_object (if any) or nil]

returns:

a hash consisting of:
  :response - http response
  :response_json - http response parsed JSON
  :ok - whether the response code was ok when related to the http method
  :object - nil or the object created if applicable
  :attributes - JSON parsed attributes of the response's body


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/3scale/core/api_client/operations.rb', line 176

def api(method, attributes, options = {})
  prefix = options.fetch(:prefix, default_prefix)
  attributes = { prefix => attributes } unless prefix.empty? or attributes.empty?
  uri = options.fetch(:uri, default_uri)

  logger.debug do
    "==> #{method.upcase} #{uri} [#{attributes}]"
  end

  before = Time.now
  response = api_http method, uri, attributes
  after = Time.now

  ok = status_ok? method, uri, response

  response_json = begin
    api_parse_json(response)
  rescue JSONError => e
    logger.error do
      "#{api_response_inspect(method, uri, response, '', after, before)} - #{e.message}"
    end
    raise e
  end

  ret = { response: response, response_json: response_json, ok: ok }

  logger.debug do
    api_response_inspect(method, uri, response, response_json, after, before)
  end

  if ok
    prefix = options.fetch(:rprefix, prefix)
    attributes = response_json.fetch(prefix, nil) unless prefix.empty?

    ret[:object] = if attributes and options[:build]
                     new attributes
                   else
                     nil
                   end
  else
    # something went wrong. let's either let the user fix it, and ask him to provide us
    # with directions returned from block or just use :raise
    do_raise = if block_given?
                 yield(ret)
               else
                 options.fetch(:raise, true)
               end
    raise APIError.new(method, uri, response, response_json) if do_raise
  end

  ret[:attributes] = attributes
  ret
end

#api_create(attributes, api_options = {}, &blk) ⇒ Object Also known as: api_save

CRUD methods



48
49
50
# File 'lib/3scale/core/api_client/operations.rb', line 48

def api_create(attributes, api_options = {}, &blk)
  api_create_object :api_do_post, attributes, api_options, &blk
end

#api_create_object(method, attributes, api_options = {}, &blk) ⇒ Object



86
87
88
89
90
# File 'lib/3scale/core/api_client/operations.rb', line 86

def api_create_object(method, attributes, api_options = {}, &blk)
  ret = send method, attributes, api_options.merge(build: true), &blk
  ret[:object].send :persisted=, true if ret[:object]
  ret[:object]
end

#api_delete(attributes, api_options = {}, &blk) ⇒ Object



62
63
64
# File 'lib/3scale/core/api_client/operations.rb', line 62

def api_delete(attributes, api_options = {}, &blk)
  api_do_delete(attributes, api_options, &blk)[:ok]
end

#api_do_delete(attributes, api_options = {}, &blk) ⇒ Object



81
82
83
84
# File 'lib/3scale/core/api_client/operations.rb', line 81

def api_do_delete(attributes, api_options = {}, &blk)
  blk = filter_404 if blk.nil?
  api :delete, attributes, api_options, &blk
end

#api_do_get(attributes, api_options = {}, &blk) ⇒ Object

Helpers



68
69
70
71
# File 'lib/3scale/core/api_client/operations.rb', line 68

def api_do_get(attributes, api_options = {}, &blk)
  blk = filter_404 if blk.nil?
  api :get, attributes, api_options, &blk
end

#api_do_post(attributes, api_options = {}, &blk) ⇒ Object



77
78
79
# File 'lib/3scale/core/api_client/operations.rb', line 77

def api_do_post(attributes, api_options = {}, &blk)
  api :post, attributes, api_options, &blk
end

#api_do_put(attributes, api_options = {}, &blk) ⇒ Object



73
74
75
# File 'lib/3scale/core/api_client/operations.rb', line 73

def api_do_put(attributes, api_options = {}, &blk)
  api :put, attributes, api_options, &blk
end

#api_read(attributes, api_options = {}, &blk) ⇒ Object Also known as: api_load



53
54
55
# File 'lib/3scale/core/api_client/operations.rb', line 53

def api_read(attributes, api_options = {}, &blk)
  api_create_object :api_do_get, attributes, api_options, &blk
end

#api_update(attributes, api_options = {}, &blk) ⇒ Object



58
59
60
# File 'lib/3scale/core/api_client/operations.rb', line 58

def api_update(attributes, api_options = {}, &blk)
  api_create_object :api_do_put, attributes, api_options, &blk
end