deliveree_sdk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ # Deliveree::PositionTracking
2
+
3
+ ## Properties
4
+ Name | Type | Description | Notes
5
+ ------------ | ------------- | ------------- | -------------
6
+ **id** | **Integer** | | [optional]
7
+ **latitude** | **Float** | | [optional]
8
+ **longitude** | **Float** | | [optional]
9
+ **tracked_at** | **DateTime** | | [optional]
10
+ **tracking_type** | **String** | | [optional]
11
+
12
+
@@ -0,0 +1,9 @@
1
+ # Deliveree::Quote
2
+
3
+ ## Properties
4
+ Name | Type | Description | Notes
5
+ ------------ | ------------- | ------------- | -------------
6
+ **time_type** | **String** | Only accept now or schedule | [optional]
7
+ **locations** | [**Array<Location>**](Location.md) | | [optional]
8
+
9
+
@@ -0,0 +1,175 @@
1
+ =begin
2
+ #Deliveree SDK
3
+ #With Deliveree SDK, developers can integrate our on-demand local delivery platform into their applications. The SDK is designed for developers to check prices, book an immediate or scheduled delivery and follow updates until delivery completion.
4
+ Contact: duke@deliveree.com
5
+ =end
6
+
7
+ require 'uri'
8
+
9
+ module Deliveree
10
+ class DelivereeApi
11
+ attr_accessor :api_client
12
+
13
+ def initialize(api_client = ApiClient.default)
14
+ @api_client = api_client
15
+ end
16
+ # @param id ID of delivery
17
+ # @param [Hash] opts the optional parameters
18
+ # @option opts [String] :accept_language (default to en)
19
+ # @return [nil]
20
+ def cancel_booking(id, opts = {})
21
+ cancel_booking_with_http_info(id, opts)
22
+ nil
23
+ end
24
+
25
+ # @param id ID of delivery
26
+ # @param [Hash] opts the optional parameters
27
+ # @option opts [String] :accept_language
28
+ # @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
29
+ def cancel_booking_with_http_info(id, opts = {})
30
+ if @api_client.config.debugging
31
+ @api_client.config.logger.debug 'Calling API: DelivereeApi.cancel_booking ...'
32
+ end
33
+ # verify the required parameter 'id' is set
34
+ if @api_client.config.client_side_validation && id.nil?
35
+ fail ArgumentError, "Missing the required parameter 'id' when calling DelivereeApi.cancel_booking"
36
+ end
37
+ # resource path
38
+ local_var_path = '/deliveries/{id}/cancel'.sub('{' + 'id' + '}', id.to_s)
39
+
40
+ # query parameters
41
+ query_params = {}
42
+
43
+ # header parameters
44
+ header_params = {}
45
+ # HTTP header 'Accept' (if needed)
46
+ header_params['Accept'] = @api_client.select_header_accept(['application/xml', 'application/json'])
47
+ # HTTP header 'Content-Type'
48
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json', 'application/xml'])
49
+ header_params[:'Accept-Language'] = opts[:'accept_language'] if !opts[:'accept_language'].nil?
50
+
51
+ # form parameters
52
+ form_params = {}
53
+
54
+ # http body (model)
55
+ post_body = nil
56
+ auth_names = ['ApiKeyAuth']
57
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
58
+ :header_params => header_params,
59
+ :query_params => query_params,
60
+ :form_params => form_params,
61
+ :body => post_body,
62
+ :auth_names => auth_names)
63
+ if @api_client.config.debugging
64
+ @api_client.config.logger.debug "API called: DelivereeApi#cancel_booking\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
65
+ end
66
+ return data, status_code, headers
67
+ end
68
+ # @param body
69
+ # @param [Hash] opts the optional parameters
70
+ # @option opts [String] :accept_language (default to en)
71
+ # @return [ResponseWithData]
72
+ def deliveries_get_quote(body, opts = {})
73
+ data, _status_code, _headers = deliveries_get_quote_with_http_info(body, opts)
74
+ data
75
+ end
76
+
77
+ # @param body
78
+ # @param [Hash] opts the optional parameters
79
+ # @option opts [String] :accept_language
80
+ # @return [Array<(ResponseWithData, Fixnum, Hash)>] ResponseWithData data, response status code and response headers
81
+ def deliveries_get_quote_with_http_info(body, opts = {})
82
+ if @api_client.config.debugging
83
+ @api_client.config.logger.debug 'Calling API: DelivereeApi.deliveries_get_quote ...'
84
+ end
85
+ # verify the required parameter 'body' is set
86
+ if @api_client.config.client_side_validation && body.nil?
87
+ fail ArgumentError, "Missing the required parameter 'body' when calling DelivereeApi.deliveries_get_quote"
88
+ end
89
+ # resource path
90
+ local_var_path = '/deliveries/get_quote'
91
+
92
+ # query parameters
93
+ query_params = {}
94
+
95
+ # header parameters
96
+ header_params = {}
97
+ # HTTP header 'Accept' (if needed)
98
+ header_params['Accept'] = @api_client.select_header_accept(['application/xml', 'application/json'])
99
+ # HTTP header 'Content-Type'
100
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json', 'application/xml'])
101
+ header_params[:'Accept-Language'] = opts[:'accept_language'] if !opts[:'accept_language'].nil?
102
+
103
+ # form parameters
104
+ form_params = {}
105
+
106
+ # http body (model)
107
+ post_body = @api_client.object_to_http_body(body)
108
+ auth_names = ['ApiKeyAuth']
109
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
110
+ :header_params => header_params,
111
+ :query_params => query_params,
112
+ :form_params => form_params,
113
+ :body => post_body,
114
+ :auth_names => auth_names,
115
+ :return_type => 'ResponseWithData')
116
+ if @api_client.config.debugging
117
+ @api_client.config.logger.debug "API called: DelivereeApi#deliveries_get_quote\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
118
+ end
119
+ return data, status_code, headers
120
+ end
121
+ # @param body
122
+ # @param [Hash] opts the optional parameters
123
+ # @option opts [String] :accept_language (default to en)
124
+ # @return [ResponseDefault]
125
+ def deliveries_post(body, opts = {})
126
+ data, _status_code, _headers = deliveries_post_with_http_info(body, opts)
127
+ data
128
+ end
129
+
130
+ # @param body
131
+ # @param [Hash] opts the optional parameters
132
+ # @option opts [String] :accept_language
133
+ # @return [Array<(ResponseDefault, Fixnum, Hash)>] ResponseDefault data, response status code and response headers
134
+ def deliveries_post_with_http_info(body, opts = {})
135
+ if @api_client.config.debugging
136
+ @api_client.config.logger.debug 'Calling API: DelivereeApi.deliveries_post ...'
137
+ end
138
+ # verify the required parameter 'body' is set
139
+ if @api_client.config.client_side_validation && body.nil?
140
+ fail ArgumentError, "Missing the required parameter 'body' when calling DelivereeApi.deliveries_post"
141
+ end
142
+ # resource path
143
+ local_var_path = '/deliveries'
144
+
145
+ # query parameters
146
+ query_params = {}
147
+
148
+ # header parameters
149
+ header_params = {}
150
+ # HTTP header 'Accept' (if needed)
151
+ header_params['Accept'] = @api_client.select_header_accept(['application/xml', 'application/json'])
152
+ # HTTP header 'Content-Type'
153
+ header_params['Content-Type'] = @api_client.select_header_content_type(['application/json', 'application/xml'])
154
+ header_params[:'Accept-Language'] = opts[:'accept_language'] if !opts[:'accept_language'].nil?
155
+
156
+ # form parameters
157
+ form_params = {}
158
+
159
+ # http body (model)
160
+ post_body = @api_client.object_to_http_body(body)
161
+ auth_names = ['ApiKeyAuth']
162
+ data, status_code, headers = @api_client.call_api(:POST, local_var_path,
163
+ :header_params => header_params,
164
+ :query_params => query_params,
165
+ :form_params => form_params,
166
+ :body => post_body,
167
+ :auth_names => auth_names,
168
+ :return_type => 'ResponseDefault')
169
+ if @api_client.config.debugging
170
+ @api_client.config.logger.debug "API called: DelivereeApi#deliveries_post\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
171
+ end
172
+ return data, status_code, headers
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,382 @@
1
+ =begin
2
+ #Deliveree SDK
3
+ #With Deliveree SDK, developers can integrate our on-demand local delivery platform into their applications. The SDK is designed for developers to check prices, book an immediate or scheduled delivery and follow updates until delivery completion.
4
+ Contact: duke@deliveree.com
5
+ =end
6
+
7
+ require 'date'
8
+ require 'json'
9
+ require 'logger'
10
+ require 'tempfile'
11
+ require 'typhoeus'
12
+ require 'uri'
13
+
14
+ module Deliveree
15
+ class ApiClient
16
+ # The Configuration object holding settings to be used in the API client.
17
+ attr_accessor :config
18
+
19
+ # Defines the headers to be used in HTTP requests of all API calls by default.
20
+ #
21
+ # @return [Hash]
22
+ attr_accessor :default_headers
23
+
24
+ # Initializes the ApiClient
25
+ # @option config [Configuration] Configuration for initializing the object, default to Configuration.default
26
+ def initialize(config = Configuration.default)
27
+ @config = config
28
+ @user_agent = "Deliveree SDK ruby version"
29
+ @default_headers = {
30
+ 'Content-Type' => 'application/json',
31
+ 'User-Agent' => @user_agent
32
+ }
33
+ end
34
+
35
+ def self.default
36
+ @@default ||= ApiClient.new
37
+ end
38
+
39
+ # Call an API with given options.
40
+ #
41
+ # @return [Array<(Object, Fixnum, Hash)>] an array of 3 elements:
42
+ # the data deserialized from response body (could be nil), response status code and response headers.
43
+ def call_api(http_method, path, opts = {})
44
+ request = build_request(http_method, path, opts)
45
+ response = request.run
46
+
47
+ if @config.debugging
48
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
49
+ end
50
+
51
+ unless response.success?
52
+ if response.timed_out?
53
+ fail ApiError.new('Connection timed out')
54
+ elsif response.code == 0
55
+ # Errors from libcurl will be made visible here
56
+ fail ApiError.new(:code => 0,
57
+ :message => response.return_message)
58
+ else
59
+ fail ApiError.new(:code => response.code,
60
+ :response_headers => response.headers,
61
+ :response_body => response.body),
62
+ response.status_message
63
+ end
64
+ end
65
+
66
+ if opts[:return_type]
67
+ data = deserialize(response, opts[:return_type])
68
+ else
69
+ data = nil
70
+ end
71
+ return data, response.code, response.headers
72
+ end
73
+
74
+ # Builds the HTTP request
75
+ #
76
+ # @param [String] http_method HTTP method/verb (e.g. POST)
77
+ # @param [String] path URL path (e.g. /account/new)
78
+ # @option opts [Hash] :header_params Header parameters
79
+ # @option opts [Hash] :query_params Query parameters
80
+ # @option opts [Hash] :form_params Query parameters
81
+ # @option opts [Object] :body HTTP body (JSON/XML)
82
+ # @return [Typhoeus::Request] A Typhoeus Request
83
+ def build_request(http_method, path, opts = {})
84
+ url = build_request_url(path)
85
+ http_method = http_method.to_sym.downcase
86
+
87
+ header_params = @default_headers.merge(opts[:header_params] || {})
88
+ query_params = opts[:query_params] || {}
89
+ form_params = opts[:form_params] || {}
90
+
91
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
92
+
93
+ # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
94
+ _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
95
+
96
+ req_opts = {
97
+ :method => http_method,
98
+ :headers => header_params,
99
+ :params => query_params,
100
+ :params_encoding => @config.params_encoding,
101
+ :timeout => @config.timeout,
102
+ :ssl_verifypeer => @config.verify_ssl,
103
+ :ssl_verifyhost => _verify_ssl_host,
104
+ :sslcert => @config.cert_file,
105
+ :sslkey => @config.key_file,
106
+ :verbose => @config.debugging
107
+ }
108
+
109
+ # set custom cert, if provided
110
+ req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
111
+
112
+ if [:post, :patch, :put, :delete].include?(http_method)
113
+ req_body = build_request_body(header_params, form_params, opts[:body])
114
+ req_opts.update :body => req_body
115
+ if @config.debugging
116
+ @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
117
+ end
118
+ end
119
+
120
+ request = Typhoeus::Request.new(url, req_opts)
121
+ download_file(request) if opts[:return_type] == 'File'
122
+ request
123
+ end
124
+
125
+ # Check if the given MIME is a JSON MIME.
126
+ # JSON MIME examples:
127
+ # application/json
128
+ # application/json; charset=UTF8
129
+ # APPLICATION/JSON
130
+ # */*
131
+ # @param [String] mime MIME
132
+ # @return [Boolean] True if the MIME is application/json
133
+ def json_mime?(mime)
134
+ (mime == '*/*') || !(mime =~ /Application\/.*json(?!p)(;.*)?/i).nil?
135
+ end
136
+
137
+ # Deserialize the response to the given return type.
138
+ #
139
+ # @param [Response] response HTTP response
140
+ # @param [String] return_type some examples: "User", "Array[User]", "Hash[String,Integer]"
141
+ def deserialize(response, return_type)
142
+ body = response.body
143
+
144
+ # handle file downloading - return the File instance processed in request callbacks
145
+ # note that response body is empty when the file is written in chunks in request on_body callback
146
+ return @tempfile if return_type == 'File'
147
+
148
+ return nil if body.nil? || body.empty?
149
+
150
+ # return response body directly for String return type
151
+ return body if return_type == 'String'
152
+
153
+ # ensuring a default content type
154
+ content_type = response.headers['Content-Type'] || 'application/json'
155
+
156
+ fail "Content-Type is not supported: #{content_type}" unless json_mime?(content_type)
157
+
158
+ begin
159
+ data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
160
+ rescue JSON::ParserError => e
161
+ if %w(String Date DateTime).include?(return_type)
162
+ data = body
163
+ else
164
+ raise e
165
+ end
166
+ end
167
+
168
+ convert_to_type data, return_type
169
+ end
170
+
171
+ # Convert data to the given return type.
172
+ # @param [Object] data Data to be converted
173
+ # @param [String] return_type Return type
174
+ # @return [Mixed] Data in a particular type
175
+ def convert_to_type(data, return_type)
176
+ return nil if data.nil?
177
+ case return_type
178
+ when 'String'
179
+ data.to_s
180
+ when 'Integer'
181
+ data.to_i
182
+ when 'Float'
183
+ data.to_f
184
+ when 'BOOLEAN'
185
+ data == true
186
+ when 'DateTime'
187
+ # parse date time (expecting ISO 8601 format)
188
+ DateTime.parse data
189
+ when 'Date'
190
+ # parse date time (expecting ISO 8601 format)
191
+ Date.parse data
192
+ when 'Object'
193
+ # generic object (usually a Hash), return directly
194
+ data
195
+ when /\AArray<(.+)>\z/
196
+ # e.g. Array<Pet>
197
+ sub_type = $1
198
+ data.map { |item| convert_to_type(item, sub_type) }
199
+ when /\AHash\<String, (.+)\>\z/
200
+ # e.g. Hash<String, Integer>
201
+ sub_type = $1
202
+ {}.tap do |hash|
203
+ data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
204
+ end
205
+ else
206
+ # models, e.g. Pet
207
+ Deliveree.const_get(return_type).new.tap do |model|
208
+ model.build_from_hash data
209
+ end
210
+ end
211
+ end
212
+
213
+ # Save response body into a file in (the defined) temporary folder, using the filename
214
+ # from the "Content-Disposition" header if provided, otherwise a random filename.
215
+ # The response body is written to the file in chunks in order to handle files which
216
+ # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
217
+ # process can use.
218
+ #
219
+ # @see Configuration#temp_folder_path
220
+ def download_file(request)
221
+ tempfile = nil
222
+ encoding = nil
223
+ request.on_headers do |response|
224
+ content_disposition = response.headers['Content-Disposition']
225
+ if content_disposition && content_disposition =~ /filename=/i
226
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
227
+ prefix = sanitize_filename(filename)
228
+ else
229
+ prefix = 'download-'
230
+ end
231
+ prefix = prefix + '-' unless prefix.end_with?('-')
232
+ encoding = response.body.encoding
233
+ tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
234
+ @tempfile = tempfile
235
+ end
236
+ request.on_body do |chunk|
237
+ chunk.force_encoding(encoding)
238
+ tempfile.write(chunk)
239
+ end
240
+ request.on_complete do |response|
241
+ tempfile.close
242
+ @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
243
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
244
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
245
+ "explicitly with `tempfile.delete`"
246
+ end
247
+ end
248
+
249
+ # Sanitize filename by removing path.
250
+ # e.g. ../../sun.gif becomes sun.gif
251
+ #
252
+ # @param [String] filename the filename to be sanitized
253
+ # @return [String] the sanitized filename
254
+ def sanitize_filename(filename)
255
+ filename.gsub(/.*[\/\\]/, '')
256
+ end
257
+
258
+ def build_request_url(path)
259
+ # Add leading and trailing slashes to path
260
+ path = "/#{path}".gsub(/\/+/, '/')
261
+ URI.encode(@config.base_url + path)
262
+ end
263
+
264
+ # Builds the HTTP request body
265
+ #
266
+ # @param [Hash] header_params Header parameters
267
+ # @param [Hash] form_params Query parameters
268
+ # @param [Object] body HTTP body (JSON/XML)
269
+ # @return [String] HTTP body data in the form of string
270
+ def build_request_body(header_params, form_params, body)
271
+ # http form
272
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
273
+ header_params['Content-Type'] == 'multipart/form-data'
274
+ data = {}
275
+ form_params.each do |key, value|
276
+ case value
277
+ when ::File, ::Array, nil
278
+ # let typhoeus handle File, Array and nil parameters
279
+ data[key] = value
280
+ else
281
+ data[key] = value.to_s
282
+ end
283
+ end
284
+ elsif body
285
+ data = body.is_a?(String) ? body : body.to_json
286
+ else
287
+ data = nil
288
+ end
289
+ data
290
+ end
291
+
292
+ # Update hearder and query params based on authentication settings.
293
+ #
294
+ # @param [Hash] header_params Header parameters
295
+ # @param [Hash] query_params Query parameters
296
+ # @param [String] auth_names Authentication scheme name
297
+ def update_params_for_auth!(header_params, query_params, auth_names)
298
+ Array(auth_names).each do |auth_name|
299
+ auth_setting = @config.auth_settings[auth_name]
300
+ next unless auth_setting
301
+ case auth_setting[:in]
302
+ when 'header' then header_params[auth_setting[:key]] = auth_setting[:value]
303
+ when 'query' then query_params[auth_setting[:key]] = auth_setting[:value]
304
+ else fail ArgumentError, 'Authentication token must be in `query` of `header`'
305
+ end
306
+ end
307
+ end
308
+
309
+ # Sets user agent in HTTP header
310
+ #
311
+ def user_agent=(user_agent)
312
+ @user_agent = user_agent
313
+ @default_headers['User-Agent'] = @user_agent
314
+ end
315
+
316
+ # Return Accept header based on an array of accepts provided.
317
+ # @param [Array] accepts array for Accept
318
+ # @return [String] the Accept header (e.g. application/json)
319
+ def select_header_accept(accepts)
320
+ return nil if accepts.nil? || accepts.empty?
321
+ # use JSON when present, otherwise use all of the provided
322
+ json_accept = accepts.find { |s| json_mime?(s) }
323
+ json_accept || accepts.join(',')
324
+ end
325
+
326
+ # Return Content-Type header based on an array of content types provided.
327
+ # @param [Array] content_types array for Content-Type
328
+ # @return [String] the Content-Type header (e.g. application/json)
329
+ def select_header_content_type(content_types)
330
+ # use application/json by default
331
+ return 'application/json' if content_types.nil? || content_types.empty?
332
+ # use JSON when present, otherwise use the first one
333
+ json_content_type = content_types.find { |s| json_mime?(s) }
334
+ json_content_type || content_types.first
335
+ end
336
+
337
+ # Convert object (array, hash, object, etc) to JSON string.
338
+ # @param [Object] model object to be converted into JSON string
339
+ # @return [String] JSON string representation of the object
340
+ def object_to_http_body(model)
341
+ return model if model.nil? || model.is_a?(String)
342
+ local_body = nil
343
+ if model.is_a?(Array)
344
+ local_body = model.map { |m| object_to_hash(m) }
345
+ else
346
+ local_body = object_to_hash(model)
347
+ end
348
+ local_body.to_json
349
+ end
350
+
351
+ # Convert object(non-array) to hash.
352
+ # @param [Object] obj object to be converted into JSON string
353
+ # @return [String] JSON string representation of the object
354
+ def object_to_hash(obj)
355
+ if obj.respond_to?(:to_hash)
356
+ obj.to_hash
357
+ else
358
+ obj
359
+ end
360
+ end
361
+
362
+ # Build parameter value according to the given collection format.
363
+ # @param [String] collection_format one of :csv, :ssv, :tsv, :pipes and :multi
364
+ def build_collection_param(param, collection_format)
365
+ case collection_format
366
+ when :csv
367
+ param.join(',')
368
+ when :ssv
369
+ param.join(' ')
370
+ when :tsv
371
+ param.join("\t")
372
+ when :pipes
373
+ param.join('|')
374
+ when :multi
375
+ # return the array directly as typhoeus will handle it as expected
376
+ param
377
+ else
378
+ fail "unknown collection format: #{collection_format.inspect}"
379
+ end
380
+ end
381
+ end
382
+ end