Class: ZuoraRestClient::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/zuora_rest_client/connection.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
entity_id: nil,
entity_name: nil,
logger: Logger.new($stdout),
log_level: :error,
log: true,
api_proxy_port: nil,
request_timeout: 120,
request_open_timeout: 120 }

Instance Method Summary collapse

Constructor Details

#initialize(username, password, environment = :production, options = {}) ⇒ Connection

Returns a new instance of Connection.



28
29
30
31
32
33
# File 'lib/zuora_rest_client/connection.rb', line 28

def initialize(username, password, environment = :production, options = {})
  @username = username
  @password = password
  @environment = environment
  @options = {}.merge(DEFAULT_OPTIONS).merge(options)
end

Instance Method Details

#app_get(path) ⇒ Object

Deprecated.

Since the Client class no longer uses this, it will be removed.



36
37
38
39
40
41
42
43
# File 'lib/zuora_rest_client/connection.rb', line 36

def app_get(path)
  warn "[DEPRECATION] `app_get` is deprecated. Please use `rest_get` instead."
  response = app_connection.get do |request|
    request.url path
    request.headers = app_headers
  end
  process_response(response)
end

#app_post(path, post_data = nil, is_json = true) ⇒ Object

Deprecated.

Since the Client class no longer uses this, it will be removed.



46
47
48
49
50
51
52
53
54
55
# File 'lib/zuora_rest_client/connection.rb', line 46

def app_post(path, post_data = nil, is_json = true)
  warn "[DEPRECATION] `app_post` is deprecated. Please use `rest_post` instead."
  response = app_connection.post do |request|
    request.url path
    request.headers = app_headers
    request.body = MultiJson.dump(post_data) if !post_data.nil? && is_json
    request.body = post_data if !post_data.nil? && !is_json
  end
  process_response(response)
end

#rest_delete(path, zuora_version = nil) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/zuora_rest_client/connection.rb', line 114

def rest_delete(path, zuora_version = nil)
  response = rest_connection(use_api_proxy?(path)).delete do |request|
    request.url [ZUORA_REST_MAJOR_VERSION, path].join('')
    request.headers = rest_headers(zuora_version)
  end
  process_response(response)
end

#rest_get(path, zuora_version = nil) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/zuora_rest_client/connection.rb', line 57

def rest_get(path, zuora_version = nil)
  response = rest_connection(use_api_proxy?(path)).get do |request|
    request.url [ZUORA_REST_MAJOR_VERSION, path].join('')
    request.headers = rest_headers(zuora_version)
  end
  process_response(response)
end

#rest_post(path, post_data = nil, zuora_version = nil, is_json = true) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/zuora_rest_client/connection.rb', line 94

def rest_post(path, post_data = nil, zuora_version = nil, is_json = true)
  response = rest_connection(use_api_proxy?(path)).post do |request|
    request.url [ZUORA_REST_MAJOR_VERSION, path].join('')
    request.headers = rest_headers(zuora_version)
    request.body = MultiJson.dump(post_data) if !post_data.nil? && is_json
    request.body = post_data if !post_data.nil? && !is_json
  end
  process_response(response)
end

#rest_put(path, put_data = nil, zuora_version = nil, is_json = true) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/zuora_rest_client/connection.rb', line 104

def rest_put(path, put_data = nil, zuora_version = nil, is_json = true)
  response = rest_connection(use_api_proxy?(path)).put do |request|
    request.url [ZUORA_REST_MAJOR_VERSION, path].join('')
    request.headers = rest_headers(zuora_version)
    request.body = MultiJson.dump(put_data) if !put_data.nil? && is_json
    request.body = put_data if !put_data.nil? && !is_json
  end
  process_response(response)
end

#rest_streamed_get(path, destination_io, zuora_version = nil) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/zuora_rest_client/connection.rb', line 65

def rest_streamed_get(path, destination_io, zuora_version = nil)

  # Set IO to binary mode
  destination_io.binmode

  endpoint_uri = Addressable::URI.parse(zuora_endpoint.rest)
  Net::HTTP.start(endpoint_uri.normalized_host, endpoint_uri.normalized_port,
      use_ssl: endpoint_uri.normalized_scheme == 'https') do |http|
    request = Net::HTTP::Get.new [endpoint_uri.normalized_path, '/', ZUORA_REST_MAJOR_VERSION, path].join('')
    rest_headers(zuora_version).each_pair do |header_key, header_value|
      request[header_key] = header_value
    end
    http.request request do |response|
      if response.kind_of? Net::HTTPSuccess
        response.read_body do |chunk|
          destination_io.write chunk
        end
      else
        raise ConnectionError.new("Error: #{response.code} - #{response.message}")
      end
    end
  end

  # Set pointer to beginning of file
  destination_io.rewind

  nil
end

#zuora_endpointObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/zuora_rest_client/connection.rb', line 122

def zuora_endpoint
  if @environment.is_a? Symbol
    if @environment.to_s.start_with?('services')
      rest_endpoint = "https://#{@environment.to_s}.zuora.com/apps"
      app_endpoint = "https://#{@environment.to_s}.zuora.com/apps/api"
    else
      rest_endpoint = "#{ZUORA_ENVIRONMENTS[@environment][:rest]}"
      app_endpoint = "#{ZUORA_ENVIRONMENTS[@environment][:app]}/apps/api"
    end
  elsif @environment.is_a? Hash
    rest_endpoint = "#{@environment[:rest]}"
    app_endpoint = "#{@environment[:app]}/apps/api"
  else
    raise 'Possible values for environment are: :production, :api_sandbox, :test, :servicesNNN or a hash with base URL values for :rest and :app.'
  end
  OpenStruct.new({ rest: rest_endpoint, app: app_endpoint })
end