Class: MailchimpMarketing::ApiClient

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, server = '') ⇒ ApiClient

Initializes the ApiClient



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/MailchimpMarketing/api_client.rb', line 24

def initialize(api_key, server = '')
  @host = "https://server.api.mailchimp.com/3.0"
  @user_agent = "Swagger-Codegen/#{VERSION}/ruby"
  @default_headers = {
    'Content-Type' => 'application/json',
    'User-Agent' => @user_agent
  }

  @api_key = api_key
  @server = server || get_server_from_api_key(api_key)
end

Instance Attribute Details

#default_headersHash

Defines the headers to be used in HTTP requests of all API calls by default.

Returns:

  • (Hash)


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

def default_headers
  @default_headers
end

Class Method Details

.defaultObject



36
37
38
# File 'lib/MailchimpMarketing/api_client.rb', line 36

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

Instance Method Details

#build_request_body(header_params, form_params, body) ⇒ String

Builds the HTTP request body

Parameters:

  • header_params (Hash)

    Header parameters

  • form_params (Hash)

    Query parameters

  • body (Object)

    HTTP body (JSON/XML)

Returns:

  • (String)

    HTTP body data in the form of string



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/MailchimpMarketing/api_client.rb', line 133

def build_request_body(header_params, form_params, body)
  # http form
  if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
      header_params['Content-Type'] == 'multipart/form-data'
    data = {}
    form_params.each do |key, value|
      case value
      when ::File, ::Array, nil
        # let typhoeus handle File, Array and nil parameters
        data[key] = value
      else
        data[key] = value.to_s
      end
    end
  elsif body
    data = body.is_a?(String) ? body : body.to_json
  else
    data = nil
  end
  data
end

#build_request_url(path) ⇒ Object



121
122
123
124
125
# File 'lib/MailchimpMarketing/api_client.rb', line 121

def build_request_url(path)
  # Add leading and trailing slashes to path
  path = "/#{path}".gsub(/\/+/, '/')
  URI.encode(@config.base_url + path)
end

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/MailchimpMarketing/api_client.rb', line 56

def call_api(http_method, path, opts = {})
  header_params = @default_headers.merge(opts[:header_params] || {})
  query_params = opts[:query_params] || {}
  form_params = opts[:form_params] || {}

  body = nil
  if [:post, :patch, :put, :delete].include?(http_method)
    body = build_request_body(header_params, form_params, opts[:body])
  end

  res = request(http_method, path, query_params, header_params, body)

  data = nil
  if res.headers['content-type'] && res.headers['content-type'].include?('application/json')
    data = JSON.parse(res.body)
  else
    data = res.body
  end

  if data
    if res.status <= 200
      data
    else
      fail ApiError.new(res.body)
    end
  end
end

#get_server_from_api_key(api_key = '') ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/MailchimpMarketing/api_client.rb', line 40

def get_server_from_api_key(api_key = '')
  begin
    split = api_key.split('-us', 1)
    server_prefix = 'us'
    server = server_prefix + api_key.split('-' + server_prefix, 1)[1]
    server
  rescue
    ""
  end
end

#json_mime?(mime) ⇒ Boolean

Check if the given MIME is a JSON MIME. JSON MIME examples:

application/json
application/json; charset=UTF8
APPLICATION/JSON
*/*

Parameters:

  • mime (String)

    MIME

Returns:

  • (Boolean)

    True if the MIME is application/json



184
185
186
# File 'lib/MailchimpMarketing/api_client.rb', line 184

def json_mime?(mime)
  (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
end

#request(http_method, path, query_params = nil, header_params = nil, body = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/MailchimpMarketing/api_client.rb', line 84

def request(http_method, path, query_params = nil, header_params = nil, body = nil)
  host = @server.length > 0 ? @host.sub('server', @server) : @host
  url = host + path

  conn = Faraday.new(url, params: query_params, headers: header_params) do |conn|
    conn.basic_auth('user', @api_key)
  end

  case http_method.to_sym.downcase
  when :delete
    conn.delete(body: body.to_json)
  when :get
    conn.get()
  when :head
    conn.head()
  when :options
    conn.options()
  when :post
    conn.post(body: body.to_json)
  when :put
    conn.put(body: body.to_json)
  when :patch
    conn.patch(body: body.to_json)
  else
    fail ApiError.new('http_method must be :post')
  end
end

#sanitize_filename(filename) ⇒ String

Sanitize filename by removing path. e.g. ../../sun.gif becomes sun.gif

Parameters:

  • filename (String)

    the filename to be sanitized

Returns:

  • (String)

    the sanitized filename



117
118
119
# File 'lib/MailchimpMarketing/api_client.rb', line 117

def sanitize_filename(filename)
  filename.gsub(/.*[\/\\]/, '')
end

#select_header_accept(accepts) ⇒ String

Return Accept header based on an array of accepts provided.

Parameters:

  • accepts (Array)

    array for Accept

Returns:

  • (String)

    the Accept header (e.g. application/json)



158
159
160
161
162
163
# File 'lib/MailchimpMarketing/api_client.rb', line 158

def select_header_accept(accepts)
  return nil if accepts.nil? || accepts.empty?
  # use JSON when present, otherwise use all of the provided
  json_accept = accepts.find { |s| json_mime?(s) }
  json_accept || accepts.join(',')
end

#select_header_content_type(content_types) ⇒ String

Return Content-Type header based on an array of content types provided.

Parameters:

  • content_types (Array)

    array for Content-Type

Returns:

  • (String)

    the Content-Type header (e.g. application/json)



168
169
170
171
172
173
174
# File 'lib/MailchimpMarketing/api_client.rb', line 168

def select_header_content_type(content_types)
  # use application/json by default
  return 'application/json' if content_types.nil? || content_types.empty?
  # use JSON when present, otherwise use the first one
  json_content_type = content_types.find { |s| json_mime?(s) }
  json_content_type || content_types.first
end

#set_config(api_key = '', server = '') ⇒ Object



51
52
53
54
# File 'lib/MailchimpMarketing/api_client.rb', line 51

def set_config(api_key = '', server = '')
  @api_key = api_key || @api_key
  @server = server || get_server_from_api_key(api_key) || @server
end