Class: MailchimpMarketing::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/MailchimpMarketing/api_client.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ ApiClient

Returns a new instance of ApiClient.



19
20
21
22
23
# File 'lib/MailchimpMarketing/api_client.rb', line 19

def initialize(config = {})
  @host = "https://server.api.mailchimp.com/3.0"
  @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
  set_config(config)
end

Class Method Details

.defaultObject



25
26
27
# File 'lib/MailchimpMarketing/api_client.rb', line 25

def self.default
  @@default ||= ApiClient.new
end

Instance Method Details

#build_collection_param(param, collection_format) ⇒ Object

Build parameter value according to the given collection format.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/MailchimpMarketing/api_client.rb', line 111

def build_collection_param(param, collection_format)
  case collection_format
  when :csv
    param.join(',')
  when :ssv
    param.join(' ')
  when :tsv
    param.join("\t")
  when :pipes
    param.join('|')
  when :multi
    param
  else
    fail "unknown collection format: #{collection_format.inspect}"
  end
end

#call_api(http_method, path, opts = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/MailchimpMarketing/api_client.rb', line 62

def call_api(http_method, path, opts = {})
  headers = {'Content-Type' => "application/json"}
  headers[:Authorization] = "Basic #{encoded_basic_token(@api_key)}" if @is_basic_auth
  headers[:Authorization] = "Bearer #@access_token" if @is_oauth

  host = @server.length > 0 ? @host.sub('server', @server) : @host
  conn = Excon.new(host + path, :headers => headers, :read_timeout => @read_timeout, :write_timeout => @write_timeout, :connect_timeout => @connect_timeout)

  res = nil
  case http_method.to_sym.downcase
  when :post, :put, :patch, :delete
    res = conn.request(:method => http_method, :query => opts[:query_params], :body => opts[:body])
  when :get
    res = conn.get(:query => opts[:query_params])
  end

  data = nil
  data = JSON.parse(res.body) if res.status == 200
  data = "success" if res.status == 204

  if (!data)
    fail ApiError.new(:status => res.status, :response_body => res.body)
  end

  return data
end

#get_server_from_api_key(api_key = '') ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/MailchimpMarketing/api_client.rb', line 29

def get_server_from_api_key(api_key = '')
  begin
    split = api_key.split('-')
    server = 'invalid-server'
    if split.length == 2
      server = split[1]
    end
    server
  rescue
    ""
  end
end

#object_to_hash(obj) ⇒ Object

Convert object(non-array) to hash.



102
103
104
105
106
107
108
# File 'lib/MailchimpMarketing/api_client.rb', line 102

def object_to_hash(obj)
  if obj.respond_to?(:to_hash)
    obj.to_hash
  else
    obj
  end
end

#object_to_http_body(model) ⇒ Object

Convert object (array, hash, object, etc) to JSON string.



90
91
92
93
94
95
96
97
98
99
# File 'lib/MailchimpMarketing/api_client.rb', line 90

def object_to_http_body(model)
  return model if model.nil? || model.is_a?(String)
  local_body = nil
  if model.is_a?(Array)
    local_body = model.map { |m| object_to_hash(m) }
  else
    local_body = object_to_hash(model)
  end
  local_body.to_json
end

#set_config(config = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/MailchimpMarketing/api_client.rb', line 42

def set_config(config = {})
  @api_key = config[:api_key] || ''
  @is_basic_auth = @api_key.to_s.strip.empty? == false

  @access_token = config[:access_token] || ''
  @is_oauth = @access_token.to_s.strip.empty? == false

  # If using Basic auth and no server is provided,
  # attempt to extract it from the api_key directy.
  @server = config[:server] || 'invalid-server'
  if @server == 'invalid-server' && @is_basic_auth
    @server = get_server_from_api_key(@api_key)
  end

  timeout = config[:timeout] || 120
  @write_timeout = config[:write_timeout] || timeout
  @read_timeout = config[:read_timeout] || timeout
  @connect_timeout = config[:connect_timeout] || timeout
end