yotpo-api 0.0.1.pre.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b59cbe17dae9bc5880f3f56b0f6f087495388cd
4
+ data.tar.gz: 1b3adac97c4eadadd2393015a7e9651523e90ddc
5
+ SHA512:
6
+ metadata.gz: 435431bda3354c7dcb2bb44c82fc400146d41941357178fe87c1813cd34e7f8360ca37095acfa88bb36c0183521cdda12f7023f5ff94fa5f3d6551e55faa7e11
7
+ data.tar.gz: cc8d14037180f5232e5f61aaca97266b4c9f4b533915c37f2fbff13a89fd2d03daf440983d96173ecf7c08209ddab2bc6e8acca1e4c5ec8b69a71481d340a905
@@ -0,0 +1,43 @@
1
+ =begin
2
+ Yotpo API
3
+
4
+ Yotpo API Entry Point
5
+
6
+ OpenAPI spec version: 0.0.1
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ # Common files
14
+ require 'yotpo-api/api_client'
15
+ require 'yotpo-api/api_error'
16
+ require 'yotpo-api/version'
17
+ require 'yotpo-api/configuration'
18
+
19
+ # Models
20
+ require 'yotpo-api/models/link_response'
21
+ require 'yotpo-api/models/link_request'
22
+ require 'yotpo-api/models/inline_response_200'
23
+
24
+ # APIs
25
+ require 'yotpo-api/api/link_api'
26
+
27
+ module YotpoApi
28
+ class << self
29
+ # Customize default settings for the SDK using block.
30
+ # YotpoApi.configure do |config|
31
+ # config.username = "xxx"
32
+ # config.password = "xxx"
33
+ # end
34
+ # If no block given, return the default Configuration object.
35
+ def configure
36
+ if block_given?
37
+ yield(Configuration.default)
38
+ else
39
+ Configuration.default
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,83 @@
1
+ =begin
2
+ Yotpo API
3
+
4
+ Yotpo API Entry Point
5
+
6
+ OpenAPI spec version: 0.0.1
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require "uri"
14
+
15
+ module YotpoApi
16
+ class LinkApi
17
+ attr_accessor :api_client
18
+
19
+ def initialize(api_client = ApiClient.default)
20
+ @api_client = api_client
21
+ end
22
+
23
+ # Creates a new short url and returns it
24
+ # Creates a new short url and returns it
25
+ # @param body link request to create a short url
26
+ # @param [Hash] opts the optional parameters
27
+ # @return [inline_response_200]
28
+ def create_link(body, opts = {})
29
+ data, status_code, headers = create_link_with_http_info(body, opts)
30
+ return data
31
+ end
32
+
33
+ # Creates a new short url and returns it
34
+ # Creates a new short url and returns it
35
+ # @param body link request to create a short url
36
+ # @param [Hash] opts the optional parameters
37
+ # @return [Array<(inline_response_200, Fixnum, Hash)>] inline_response_200 data, response status code and response headers
38
+ def create_link_with_http_info(body, opts = {})
39
+ if @api_client.config.debugging
40
+ @api_client.config.logger.debug "Calling API: LinkApi#create_link ..."
41
+ end
42
+
43
+ # verify the required parameter 'body' is set
44
+ fail "Missing the required parameter 'body' when calling create_link" if body.nil?
45
+
46
+ # resource path
47
+ path = "/go".sub('{format}','json')
48
+
49
+ # query parameters
50
+ query_params = {}
51
+
52
+ # header parameters
53
+ header_params = {}
54
+
55
+ # HTTP header 'Accept' (if needed)
56
+ _header_accept = ['application/json']
57
+ _header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
58
+
59
+ # HTTP header 'Content-Type'
60
+ _header_content_type = ['application/json']
61
+ header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
62
+
63
+ # form parameters
64
+ form_params = {}
65
+
66
+ # http body (model)
67
+ post_body = @api_client.object_to_http_body(body)
68
+
69
+ auth_names = []
70
+ data, status_code, headers = @api_client.call_api(:POST, path,
71
+ :header_params => header_params,
72
+ :query_params => query_params,
73
+ :form_params => form_params,
74
+ :body => post_body,
75
+ :auth_names => auth_names,
76
+ :return_type => 'inline_response_200')
77
+ if @api_client.config.debugging
78
+ @api_client.config.logger.debug "API called: LinkApi#create_link\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
79
+ end
80
+ return data, status_code, headers
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,332 @@
1
+ =begin
2
+ Yotpo API
3
+
4
+ Yotpo API Entry Point
5
+
6
+ OpenAPI spec version: 0.0.1
7
+
8
+ Generated by: https://github.com/swagger-api/swagger-codegen.git
9
+
10
+
11
+ =end
12
+
13
+ require 'date'
14
+ require 'json'
15
+ require 'logger'
16
+ require 'tempfile'
17
+ require 'typhoeus'
18
+ require 'uri'
19
+
20
+ module YotpoApi
21
+ class ApiClient
22
+ # The Configuration object holding settings to be used in the API client.
23
+ attr_accessor :config
24
+
25
+ # Defines the headers to be used in HTTP requests of all API calls by default.
26
+ #
27
+ # @return [Hash]
28
+ attr_accessor :default_headers
29
+
30
+ def initialize(config = Configuration.default)
31
+ @config = config
32
+ @user_agent = "ruby-swagger-#{VERSION}"
33
+ @default_headers = {
34
+ 'Content-Type' => "application/json",
35
+ 'User-Agent' => @user_agent
36
+ }
37
+ end
38
+
39
+ def self.default
40
+ @@default ||= ApiClient.new
41
+ end
42
+
43
+ # Call an API with given options.
44
+ #
45
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
46
+ # the data deserialized from response body (could be nil), response status code and response headers.
47
+ def call_api(http_method, path, opts = {})
48
+ request = build_request(http_method, path, opts)
49
+ response = request.run
50
+
51
+ if @config.debugging
52
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
53
+ end
54
+
55
+ unless response.success?
56
+ fail ApiError.new(:code => response.code,
57
+ :response_headers => response.headers,
58
+ :response_body => response.body),
59
+ response.status_message
60
+ end
61
+
62
+ if opts[:return_type]
63
+ data = deserialize(response, opts[:return_type])
64
+ else
65
+ data = nil
66
+ end
67
+ return data, response.code, response.headers
68
+ end
69
+
70
+ def build_request(http_method, path, opts = {})
71
+ url = build_request_url(path)
72
+ http_method = http_method.to_sym.downcase
73
+
74
+ header_params = @default_headers.merge(opts[:header_params] || {})
75
+ query_params = opts[:query_params] || {}
76
+ form_params = opts[:form_params] || {}
77
+
78
+
79
+
80
+ req_opts = {
81
+ :method => http_method,
82
+ :headers => header_params,
83
+ :params => query_params,
84
+ :timeout => @config.timeout,
85
+ :ssl_verifypeer => @config.verify_ssl,
86
+ :sslcert => @config.cert_file,
87
+ :sslkey => @config.key_file,
88
+ :verbose => @config.debugging
89
+ }
90
+
91
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
92
+
93
+ if [:post, :patch, :put, :delete].include?(http_method)
94
+ req_body = build_request_body(header_params, form_params, opts[:body])
95
+ req_opts.update :body => req_body
96
+ if @config.debugging
97
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
98
+ end
99
+ end
100
+
101
+ Typhoeus::Request.new(url, req_opts)
102
+ end
103
+
104
+ # Check if the given MIME is a JSON MIME.
105
+ # JSON MIME examples:
106
+ # application/json
107
+ # application/json; charset=UTF8
108
+ # APPLICATION/JSON
109
+ def json_mime?(mime)
110
+ !!(mime =~ /\Aapplication\/json(;.*)?\z/i)
111
+ end
112
+
113
+ # Deserialize the response to the given return type.
114
+ #
115
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
116
+ def deserialize(response, return_type)
117
+ body = response.body
118
+ return nil if body.nil? || body.empty?
119
+
120
+ # return response body directly for String return type
121
+ return body if return_type == 'String'
122
+
123
+ # handle file downloading - save response body into a tmp file and return the File instance
124
+ return download_file(response) if return_type == 'File'
125
+
126
+ # ensuring a default content type
127
+ content_type = response.headers['Content-Type'] || 'application/json'
128
+
129
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
130
+
131
+ begin
132
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
133
+ rescue JSON::ParserError => e
134
+ if %w(String Date DateTime).include?(return_type)
135
+ data = body
136
+ else
137
+ raise e
138
+ end
139
+ end
140
+
141
+ convert_to_type data, return_type
142
+ end
143
+
144
+ # Convert data to the given return type.
145
+ def convert_to_type(data, return_type)
146
+ return nil if data.nil?
147
+ case return_type
148
+ when 'String'
149
+ data.to_s
150
+ when 'Integer'
151
+ data.to_i
152
+ when 'Float'
153
+ data.to_f
154
+ when 'BOOLEAN'
155
+ data == true
156
+ when 'DateTime'
157
+ # parse date time (expecting ISO 8601 format)
158
+ DateTime.parse data
159
+ when 'Date'
160
+ # parse date time (expecting ISO 8601 format)
161
+ Date.parse data
162
+ when 'Object'
163
+ # generic object, return directly
164
+ data
165
+ when /\AArray<(.+)>\z/
166
+ # e.g. Array<Pet>
167
+ sub_type = $1
168
+ data.map {|item| convert_to_type(item, sub_type) }
169
+ when /\AHash\<String, (.+)\>\z/
170
+ # e.g. Hash<String, Integer>
171
+ sub_type = $1
172
+ {}.tap do |hash|
173
+ data.each {|k, v| hash[k] = convert_to_type(v, sub_type) }
174
+ end
175
+ else
176
+ # models, e.g. Pet
177
+ YotpoApi.const_get(return_type.split("_").map{|v| v[0].upcase + v[1..-1] }.join).new.tap do |model|
178
+ model.build_from_hash data
179
+ end
180
+ end
181
+ end
182
+
183
+ # Save response body into a file in (the defined) temporary folder, using the filename
184
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
185
+ #
186
+ # @see Configuration#temp_folder_path
187
+ # @return [Tempfile] the file downloaded
188
+ def download_file(response)
189
+ content_disposition = response.headers['Content-Disposition']
190
+ if content_disposition
191
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
192
+ prefix = sanitize_filename(filename)
193
+ else
194
+ prefix = 'download-'
195
+ end
196
+ prefix = prefix + '-' unless prefix.end_with?('-')
197
+
198
+ tempfile = nil
199
+ encoding = response.body.encoding
200
+ Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file|
201
+ file.write(response.body)
202
+ tempfile = file
203
+ end
204
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
205
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
206
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
207
+ "explicitly with `tempfile.delete`"
208
+ tempfile
209
+ end
210
+
211
+ # Sanitize filename by removing path.
212
+ # e.g. ../../sun.gif becomes sun.gif
213
+ #
214
+ # @param [String] filename the filename to be sanitized
215
+ # @return [String] the sanitized filename
216
+ def sanitize_filename(filename)
217
+ filename.gsub /.*[\/\\]/, ''
218
+ end
219
+
220
+ def build_request_url(path)
221
+ # Add leading and trailing slashes to path
222
+ path = "/#{path}".gsub(/\/+/, '/')
223
+ URI.encode(@config.base_url + path)
224
+ end
225
+
226
+ def build_request_body(header_params, form_params, body)
227
+ # http form
228
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
229
+ header_params['Content-Type'] == 'multipart/form-data'
230
+ data = {}
231
+ form_params.each do |key, value|
232
+ case value
233
+ when File, Array, nil
234
+ # let typhoeus handle File, Array and nil parameters
235
+ data[key] = value
236
+ else
237
+ data[key] = value.to_s
238
+ end
239
+ end
240
+ elsif body
241
+ data = body.is_a?(String) ? body : body.to_json
242
+ else
243
+ data = nil
244
+ end
245
+ data
246
+ end
247
+
248
+ # Update hearder and query params based on authentication settings.
249
+ def update_params_for_auth!(header_params, query_params, auth_names)
250
+ Array(auth_names).each do |auth_name|
251
+ auth_setting = @config.auth_settings[auth_name]
252
+ next unless auth_setting
253
+ case auth_setting[:in]
254
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
255
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
256
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
257
+ end
258
+ end
259
+ end
260
+
261
+ def user_agent=(user_agent)
262
+ @user_agent = user_agent
263
+ @default_headers['User-Agent'] = @user_agent
264
+ end
265
+
266
+ # Return Accept header based on an array of accepts provided.
267
+ # @param [Array] accepts array for Accept
268
+ # @return [String] the Accept header (e.g. application/json)
269
+ def select_header_accept(accepts)
270
+ return nil if accepts.nil? || accepts.empty?
271
+ # use JSON when present, otherwise use all of the provided
272
+ json_accept = accepts.find { |s| json_mime?(s) }
273
+ return json_accept || accepts.join(',')
274
+ end
275
+
276
+ # Return Content-Type header based on an array of content types provided.
277
+ # @param [Array] content_types array for Content-Type
278
+ # @return [String] the Content-Type header (e.g. application/json)
279
+ def select_header_content_type(content_types)
280
+ # use application/json by default
281
+ return 'application/json' if content_types.nil? || content_types.empty?
282
+ # use JSON when present, otherwise use the first one
283
+ json_content_type = content_types.find { |s| json_mime?(s) }
284
+ return json_content_type || content_types.first
285
+ end
286
+
287
+ # Convert object (array, hash, object, etc) to JSON string.
288
+ # @param [Object] model object to be converted into JSON string
289
+ # @return [String] JSON string representation of the object
290
+ def object_to_http_body(model)
291
+ return model if model.nil? || model.is_a?(String)
292
+ _body = nil
293
+ if model.is_a?(Array)
294
+ _body = model.map{|m| object_to_hash(m) }
295
+ else
296
+ _body = object_to_hash(model)
297
+ end
298
+ _body.to_json
299
+ end
300
+
301
+ # Convert object(non-array) to hash.
302
+ # @param [Object] obj object to be converted into JSON string
303
+ # @return [String] JSON string representation of the object
304
+ def object_to_hash(obj)
305
+ if obj.respond_to?(:to_hash)
306
+ obj.to_hash
307
+ else
308
+ obj
309
+ end
310
+ end
311
+
312
+ # Build parameter value according to the given collection format.
313
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
314
+ def build_collection_param(param, collection_format)
315
+ case collection_format
316
+ when :csv
317
+ param.join(',')
318
+ when :ssv
319
+ param.join(' ')
320
+ when :tsv
321
+ param.join("\t")
322
+ when :pipes
323
+ param.join('|')
324
+ when :multi
325
+ # return the array directly as typhoeus will handle it as expected
326
+ param
327
+ else
328
+ fail "unknown collection format: #{collection_format.inspect}"
329
+ end
330
+ end
331
+ end
332
+ end