Class: RestApiClient::RequestsHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rest/api/client/request_handler.rb

Class Method Summary collapse

Class Method Details

.do_request_with_payload(method, service_key, path, args = {:params => {}}, headers = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rest/api/client/request_handler.rb', line 26

def self.do_request_with_payload(method, service_key, path, args = {:params => {}}, headers = {})
  headers = treat_header headers, service_key
  url = get_service_url(service_key) + path

  res = RestClient::Resource.new(url)
  res.method(method).call(args[:params], headers) { |response, request, result, &block|
    get_response_callback(args).call(response, request, result, &block)
  }
end

.do_request_without_payload(method, service_key, path, args = {:params => {}}, headers = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/rest/api/client/request_handler.rb', line 36

def self.do_request_without_payload(method, service_key, path, args = {:params => {}}, headers = {})
  headers = treat_header headers, service_key
  url = get_service_url(service_key) + path + '?' + params_to_query(args[:params])

  res = RestClient::Resource.new(url)
  res.method(method).call(headers) { |response, request, result, &block|
    get_response_callback(args).call(response, request, result, &block)
  }
end

.get_response_callback(args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rest/api/client/request_handler.rb', line 62

def self.get_response_callback(args)
  lambda do |response, request, result, &block|
    if response.code >= 200 && response.code < 300
      RestApiClient.parse_json response, args

    elsif [301, 302, 307].include? response.code
      response.follow_redirection(request, result, &block)

    elsif response.code == 401
      raise RestApiClient::UnauthorizedException.new RestApiClient.parse_json response

    elsif response.code >= 400 && response.code < 500
      response = RestApiClient.parse_json response
      message = response.has_key?('message') ? response['message'] : ''
      raise RestApiClient::ModelErrorsException.new message

    else
      response.return!(request, result, &block)
    end
  end
end

.get_service_url(service_key) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/rest/api/client/request_handler.rb', line 54

def self.get_service_url(service_key)
  redis = Redis.new
  path = redis.get "#{service_key}.url"
  raise RestApiClient::ServiceUrlException.new('You must need to set the service key') unless path
  path << '/' unless path.end_with?('/')
  path
end

.params_to_query(params) ⇒ Object



84
85
86
87
88
# File 'lib/rest/api/client/request_handler.rb', line 84

def self.params_to_query(params)
  uri = Addressable::URI.new
  uri.query_values = params
  uri.query || ''
end

.perform_delete(service_key, path, args = {:params => {}}, headers = {}) ⇒ Object



14
15
16
# File 'lib/rest/api/client/request_handler.rb', line 14

def self.perform_delete(service_key, path, args = {:params => {}}, headers = {})
  self.do_request_without_payload('delete', service_key, path, args, headers)
end

.perform_get(service_key, path, args = {:params => {}}, headers = {}) ⇒ Object



10
11
12
# File 'lib/rest/api/client/request_handler.rb', line 10

def self.perform_get(service_key, path, args = {:params => {}}, headers = {})
  self.do_request_without_payload('get', service_key, path, args, headers)
end

.perform_post(service_key, path, args = {:params => {}}, headers = {}) ⇒ Object



18
19
20
# File 'lib/rest/api/client/request_handler.rb', line 18

def self.perform_post(service_key, path, args = {:params => {}}, headers = {})
  self.do_request_with_payload('post', service_key, path, args, headers)
end

.perform_put(service_key, path, args = {:params => {}}, headers = {}) ⇒ Object



22
23
24
# File 'lib/rest/api/client/request_handler.rb', line 22

def self.perform_put(service_key, path, args = {:params => {}}, headers = {})
  self.do_request_with_payload('put', service_key, path, args, headers)
end

.treat_header(headers, service_key) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/rest/api/client/request_handler.rb', line 46

def self.treat_header(headers, service_key)
  authorization_key = RestApiClient.get_auth_key service_key
  if authorization_key
    headers = headers.merge(:Authorization => authorization_key)
  end
  headers
end