Method: TAM::API.dispatch_to_tam

Defined in:
lib/tam/api.rb

.dispatch_to_tam(http_method, endpoint, user, payload = '') ⇒ Object

Dispatches the request to the telco asset marketplace REST API



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tam/api.rb', line 40

def self.dispatch_to_tam(http_method, endpoint, user, payload='')
  consumer = create_oauth_consumer
  access_token = OAuth::AccessToken.new(consumer, user.access_token, user.token_secret)

  if (http_method == :get)
    response = access_token.send(http_method, endpoint, {'Content-Type' => 'application/json'})
  else
    response = access_token.send(http_method, endpoint, payload, {'Content-Type' => 'application/json'})
  end
  
  if response.class == Net::HTTPOK
    return response.body
  elsif response.class == Net::HTTPUnauthorized
    LOGGER.error 'Request not authorized ' + response.message + ', ' + response.body
    raise RequestNotAuthorized.new(response.message, response.body)
  elsif response.class == Net::HTTPBadRequest
    if response.body.include? 'consumer_key_unknown'
      LOGGER.error 'Configured telco asset marketplace consumer_key is not valid: ' + response.message + ', ' + response.body
      raise InvalidConsumerKey.new(response.message, response.body)
    elsif response.body.include? 'signature_invalid'
      LOGGER.error 'Configured telco asset marketplace consumer_secret is not valid: ' + response.message + ', ' + response.body
      raise InvalidConsumerSecret.new(response.message, response.body)
    else
      raise UnexpectedError.new(response.message, response.body)
    end
  elsif response.class == Net::HTTPServiceUnavailable
    LOGGER.error 'telco asset marketplace service ' + endpoint + ' not available'
    raise ServiceUnavailable.new(response.message, response.body)
  else
    raise UnexpectedError.new(response.message, response.body)
  end
end