app42 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +119 -0
  6. data/Rakefile +5 -0
  7. data/TODO.txt +36 -0
  8. data/app42.gemspec +41 -0
  9. data/bin/app42 +15 -0
  10. data/lib/app42.rb +23 -0
  11. data/lib/app42/base/constants.rb +33 -0
  12. data/lib/app42/base/help.rb +717 -0
  13. data/lib/app42/base/http_helper.rb +46 -0
  14. data/lib/app42/base/message.rb +35 -0
  15. data/lib/app42/base/shell.rb +39 -0
  16. data/lib/app42/base/util.rb +304 -0
  17. data/lib/app42/client/app42_rest_client.rb +451 -0
  18. data/lib/app42/client/rest_util.rb +66 -0
  19. data/lib/app42/command/app.rb +194 -0
  20. data/lib/app42/command/authorize.rb +29 -0
  21. data/lib/app42/command/base.rb +660 -0
  22. data/lib/app42/command/client.rb +232 -0
  23. data/lib/app42/command/config.rb +185 -0
  24. data/lib/app42/command/info.rb +101 -0
  25. data/lib/app42/command/service.rb +196 -0
  26. data/lib/app42/command/user.rb +140 -0
  27. data/lib/app42/command/user_key.rb +68 -0
  28. data/lib/app42/version.rb +13 -0
  29. data/spec/app42/base/constants_spec.rb +11 -0
  30. data/spec/app42/command/app_spec.rb +103 -0
  31. data/spec/app42/command/base_spec.rb +7 -0
  32. data/spec/app42/command/config_spec.rb +20 -0
  33. data/spec/app42/command/info_spec.rb +27 -0
  34. data/spec/app42/command/service_spec.rb +98 -0
  35. data/spec/app42/command/user_spec.rb +7 -0
  36. data/spec/app42/command/user_token_spec.rb +40 -0
  37. data/spec/app42/version_spec.rb +7 -0
  38. data/spec/app42_spec.rb +2 -0
  39. data/spec/data/info.yml +16 -0
  40. data/spec/data/services.yml +25 -0
  41. data/spec/data/state.yml +16 -0
  42. data/spec/data/user_services.yml +18 -0
  43. data/spec/spec_helper.rb +18 -0
  44. metadata +257 -0
@@ -0,0 +1,46 @@
1
+ module App42::Base
2
+ module HttpHelper
3
+
4
+ # post request to rest client
5
+ def post_request body, signed, rest_url, params
6
+ @connection.post(signed, rest_url, params, body)
7
+ end
8
+
9
+ # put request to rest client
10
+ def put_request body, signed, rest_url, params
11
+ @connection.put(signed, rest_url, params, body)
12
+ end
13
+
14
+ # get request to rest client
15
+ def get_request signed, rest_url, params
16
+ @connection.get(signed, rest_url, params)
17
+ end
18
+
19
+ # delete request to rest client
20
+ def delete_request signed, rest_url, params
21
+ @connection.delete(signed, rest_url, params)
22
+ end
23
+
24
+ # build post request
25
+ def build_post_request body, query_params, resource, action
26
+ response = post_request body, signature(query_params), resource_url(resource, action), query_params
27
+ end
28
+
29
+ # build put request
30
+ def build_put_request body, query_params, resource, action
31
+ response = put_request body, signature(query_params), resource_url(resource, action), query_params
32
+ end
33
+
34
+ # build get request
35
+ def build_get_request query_params, resource, action
36
+ response = get_request signature(query_params), resource_url(resource, action), query_params
37
+ end
38
+
39
+ # build delete request
40
+ def build_delete_request query_params, resource, action
41
+ response = delete_request signature(query_params), resource_url(resource, action), query_params
42
+ end
43
+
44
+ end
45
+ end
46
+
@@ -0,0 +1,35 @@
1
+ module Message
2
+
3
+ # key related messages
4
+ KEYS_NOT_FIND = "=== API key and Secret key not found. ==="
5
+ SOMETHING_WRONG = "=== Something went wrong ==="
6
+ KEYS_CLEARED = "=== API key and Secret key have been deleted. ==="
7
+ KEYS = "=== API/Secret Key ==="
8
+ KEYS_EXIST = "API/Secret key already added."
9
+ WRONG_KEY = "Either API key or Secret key is not valid."
10
+ ADD_KEY = "Please add your API key and Secret key."
11
+
12
+ # app related message
13
+ MORE_THAN_ONE_BINARY = "More than one binary exists at the specified location. Please choose one."
14
+ NO_BINARY = "No binary exists at the specified location."
15
+ NO_APP = "No App found."
16
+ WAIT_FOR_WHILE = "Please be patient... This process may take a while to complete."
17
+ LATEST_S_WAIT = "Please wait... It may take few minutes to complete."
18
+ NOT_A_VALID_NUM = "Instance(s) count should be a valid number and greater than zero."
19
+ REQUEST_PROGRESS = "\nYour request is in progress. Please check the request status after sometime."
20
+ LOG_MESSAGE = "Please visit below URL(s) to access app logs"
21
+
22
+ # validation message
23
+ IP_NOT_VALID = "Please provide the valid Source IP."
24
+ ACCESS_TIME = "Access time should be a valid number and it should not be more than 720(1 month) hours."
25
+ BIND_NOTE = "Please Provide Source IP\n(For public access, use '0.0.0.0')"
26
+ UNBIND_NOTE = "Please Provide Source IP"
27
+ ACCESS_TIME_NOTE = "Please provide the time duration(in hours) you want to bind IP(0 for unlimited access)"
28
+
29
+ # REST CLIENT EXCEPTION
30
+ SERVER_ERROR = "Something went wrong. We will look into the issue. For further information, Please drop a mail to 'support@shephertz.com'."
31
+ COULD_NOT_CONNECT = "Could not connect to the App42PaaS server."
32
+ NW_CONNECTION_ERROR = "Please check your n/w connection."
33
+ TIME_OUT_ERROR = "Request Timed Out. Please try after some time."
34
+ BAD_RESPONSE_ERROR = "Received bad HTTP response from"
35
+ end
@@ -0,0 +1,39 @@
1
+ module App42
2
+ module Base
3
+ extend self
4
+
5
+ # app42 available commands
6
+ APP42_COMMAND = [
7
+ 'version',
8
+ 'list',
9
+ 'deploy',
10
+ 'setupInfra',
11
+ 'keys',
12
+ 'clearKeys',
13
+ 'addKeys',
14
+ 'scale',
15
+ 'descale',
16
+ 'appState',
17
+ 'apps',
18
+ 'logs',
19
+ 'appInfo',
20
+ 'releases',
21
+ 'start',
22
+ 'restart',
23
+ 'stop',
24
+ 'deleteInfra',
25
+ 'iaasProviders',
26
+ 'runtimes',
27
+ 'services',
28
+ 'createService',
29
+ 'deleteService',
30
+ 'supportedServices',
31
+ 'serviceInfo',
32
+ 'resetServicePassword',
33
+ 'bindIP',
34
+ 'unbindIP',
35
+ 'bindInfo'
36
+ ]
37
+
38
+ end
39
+ end
@@ -0,0 +1,304 @@
1
+ require "interact"
2
+ require "ipaddress"
3
+
4
+ module App42
5
+ module Base
6
+ module Util
7
+
8
+ # ask application name to user
9
+ def ask_app_name
10
+ input "Enter App Name", [], true
11
+ end
12
+
13
+ #
14
+ # Reading input from user
15
+ #
16
+ # @param message
17
+ # @param choices
18
+ # @param indexed
19
+ #
20
+ # @return +input string+
21
+ def input message, choices, indexed = true
22
+ list = choices.compact unless choices.empty?
23
+ ans = ask Paint["#{message}", :cyan],
24
+ :choices => list,
25
+ :default => list[0],
26
+ :indexed => indexed
27
+ print_new_line
28
+ return ans
29
+ end
30
+
31
+ #
32
+ # console message
33
+ #
34
+ # @param msg
35
+ # @param new_line
36
+ # @param color
37
+ def message msg, new_line = true, color
38
+ new_line ? (puts Paint["#{msg}\n", color.to_sym]) : (print Paint["#{msg}", color.to_sym])
39
+ end
40
+
41
+ # json parser
42
+ def json_parse(str)
43
+ JSON.parse(str) if str
44
+ end
45
+
46
+
47
+ def request_failed?(status)
48
+ # TODO, if more than one error code
49
+ # APP42_HTTP_ERROR_CODES.detect{|error_code| status >= error_code}
50
+ APP42_HTTP_ERROR_CODES.include? status
51
+ end
52
+
53
+ # error message parser
54
+ def parse_error_message(status, body)
55
+ if status && body && body["message"] && body["details"]
56
+ desc = body["details"].gsub("\"","'")
57
+ # TODO, may need later
58
+ # app42_client_info
59
+ # FIXME, should be proper message
60
+ # message "#{body["message"]}: #{body["details"]}", true, 'red'
61
+ message "#{body["details"]}", true, 'red'
62
+ exit!
63
+ else
64
+ # TODO, may need later
65
+ # app42_client_info
66
+ message "Error: Something wrong", true, 'red'
67
+ exit!
68
+ end
69
+ end
70
+
71
+ # return client info +ruby-version+, +os+ and +client-version+ details
72
+ def app42_client_info
73
+ puts Paint["INFO: #{App42.user_agent}", :red]
74
+ end
75
+
76
+ # return util instance
77
+ def util_base
78
+ util = App42::Client::RestUtil.new
79
+ end
80
+
81
+ # hash of params that will return API key, version and timestamp
82
+ def params
83
+ params = {
84
+ 'apiKey'=> @api_key,
85
+ 'version' => VERSION,
86
+ 'timeStamp' => util_base.get_timestamp_utc,
87
+ }
88
+ end
89
+
90
+ # build resource url as per requested params
91
+ def resource_url resource, what
92
+ what.nil? ? (resource_url = "/#{resource}") : (resource_url = "/#{resource}/#{what}")
93
+ end
94
+
95
+ # will build signature using requested params and Secret key
96
+ #
97
+ # @params +requested params+
98
+ #
99
+ # @ return +signature+
100
+ def signature params
101
+ signature = util_base.sign(@secret_key, params )
102
+ end
103
+
104
+ # Spinning code for latest status
105
+ def show_wait_spinner(fps=10)
106
+ chars = %w[| / - \\]
107
+ delay = 1.0/fps
108
+ iter = 0
109
+ spinner = Thread.new do
110
+ while iter do
111
+ print chars[(iter+=1) % chars.length]
112
+ sleep delay
113
+ print "\b"
114
+ end
115
+ end
116
+ yield.tap{
117
+ iter = false
118
+ spinner.join
119
+ }
120
+ end
121
+
122
+ # checks transaction status
123
+ #
124
+ #REVIEW, need to verify FAILED use case
125
+ def check_transaction_status transaction_id, previous_completed, what
126
+ begin
127
+ flag = false
128
+ message "#{Message::LATEST_S_WAIT}", true, 'green'
129
+ print 'Latest Status....'
130
+ while flag == false do
131
+ response = status_call transaction_id
132
+ re_try ||= 1
133
+
134
+ if response["success"] == true && response["transactionStatus"] == "COMPLETED"
135
+ print_new_line
136
+ message "#{response["message"]}", true, 'green'
137
+ return true
138
+ elsif response["success"] == true && response["transactionStatus"] == "INPROGRESS"
139
+ if previous_completed != response["completed"]
140
+ print_new_line
141
+ puts Paint["#{response["completed"]} out of #{response["requested"]} #{what}", :green]
142
+ previous_completed = response["completed"]
143
+ end
144
+ show_wait_spinner{
145
+ sleep rand(4)+2
146
+ }
147
+ unless what.to_s == 'Uploaded'
148
+ if(re_try += 1 ) >= 25
149
+ message "#{Message::REQUEST_PROGRESS}", true, 'red'
150
+ exit!
151
+ end
152
+ end
153
+ else response["success"] == true && response["transactionStatus"] == "FAILED"
154
+ print_new_line
155
+ message "#{response["message"]}", true, 'red'
156
+ exit!
157
+ end
158
+ sleep 5
159
+ end
160
+ rescue Interrupt
161
+ puts Paint[" Command cancelled.", :red]
162
+ exit!
163
+ end
164
+ end
165
+
166
+ # rest call for transaction status check
167
+ def status_call transaction_id
168
+ begin
169
+ query_params = params
170
+ query_params.store('transactionId', transaction_id)
171
+ @same_status_retry ||= 1
172
+
173
+ response = get_request signature(query_params), resource_url('info', 'transaction'), query_params
174
+ return response
175
+ rescue Interrupt
176
+ puts Paint[" Command cancelled.", :red]
177
+ exit!
178
+ end
179
+ end
180
+
181
+ # Escape white space and special character from path as per OS
182
+ def escape_path(path)
183
+ path = File.expand_path(path)
184
+ if RUBY_PLATFORM =~ /mingw|mswin32|cygwin/
185
+ if path.include?(' ')
186
+ #FIXME, should be universal for window os
187
+ # return '"' + path + '"'
188
+ return path.gsub(' ', '\ ')
189
+ else
190
+ return path
191
+ end
192
+ else
193
+ return path.gsub(' ', '\ ')
194
+ end
195
+ end
196
+
197
+ def print_new_line #:nodoc:
198
+ puts "\n"
199
+ end
200
+
201
+ # regex for number excluding zero
202
+ def numeric?(obj)
203
+ obj.to_s.match(/^[-+]?[1-9]*\.?[1-9]+$/) == nil ? false : true
204
+ end
205
+
206
+ # regex for number including zero
207
+ def numeric_including_zero?(obj)
208
+ obj.to_s.match(/^[-+]?[0-9]*\.?[0-9]+$/) == nil ? false : true
209
+ end
210
+
211
+ # camel case to whitespace
212
+ def camel_case_to_whitespace str
213
+ str_cap = str.gsub(/::/, '/').
214
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
215
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
216
+ tr("-", "_").downcase
217
+
218
+ str_capitalize = str_cap.tr("_", " ").capitalize
219
+ str_capitalize.split(' ').map(&:capitalize).join(' ')
220
+ end
221
+
222
+ # validate whether the value in the variable is really an IP
223
+ def ip_address_valid? source_ip
224
+ if source_ip == "0.0.0.0" || source_ip.match(App42::IP_REGEX)
225
+ true
226
+ else
227
+ false
228
+ end
229
+ end
230
+
231
+ # Check whether number is valid number
232
+ def number_valid? num
233
+ ((!numeric_including_zero? num) || (num.to_s =~ App42::REGEX) || (num.to_i == 0)) ? (return false) : (return num)
234
+ end
235
+
236
+ # Check whether +access_time+ is valid
237
+ # +access time+ should be a valid number and
238
+ # not be more than +720+ hours
239
+ def time_valid? num
240
+ ((!numeric_including_zero? num) || (num.to_i > 720)) ? (return false) : (return num)
241
+ end
242
+
243
+ # Check whether +app OR service+ name is valid OR not
244
+ # +app OR service+ name lenght should not be more than 30 character
245
+ # And should not contain any special character
246
+ def validate_app_and_service_name name, str
247
+ if str.match(App42::REGEX) || str.length > 30
248
+ message "#{name} should not contain any special character or white space and it's length should be less than 30.", true, 'red'
249
+ return false
250
+ else
251
+ return str
252
+ end
253
+ end
254
+
255
+ # Check whether +database name+ is valid OR not
256
+ # +database name+ lenght should not be more than 64 character
257
+ # And should not contain any special character
258
+ def validate_database_name name, str
259
+ if str.match(App42::REGEX) || str.length > 64
260
+ message "#{name} should not contain any special character and it's length should be less than 64.", true, 'red'
261
+ return false
262
+ elsif App42::DATABASE_NAME_NOT_ALLOWED.include? str
263
+ message "Your can't create database name with: #{str}.", true, 'red'
264
+ return false
265
+ else
266
+ return str
267
+ end
268
+ end
269
+
270
+ # rest call to server to check whether +application+ exist OR not.
271
+ # return true if +application+ exist else display ERROR message and exit
272
+ def is_app_exist? app_name
273
+ query_params = params
274
+ query_params.store('appName', app_name)
275
+
276
+ response = build_get_request query_params, 'app', 'availability'
277
+
278
+ unless response["success"]
279
+ return true
280
+ else
281
+ message "App with the name #{app_name} does not exist.", true, 'red'
282
+ exit!
283
+ end
284
+ end
285
+
286
+ # rest call to server to check whether +service+ exist OR not.
287
+ # return true if +service+ exist else display ERROR message and exit
288
+ def is_service_exist? service_name
289
+ query_params = params
290
+ query_params.store('serviceName', service_name)
291
+
292
+ response = build_get_request query_params, 'service', 'availability'
293
+
294
+ unless response["success"]
295
+ return true
296
+ else
297
+ message "Service with the name #{service_name} does not exist.", true, 'red'
298
+ exit!
299
+ end
300
+ end
301
+
302
+ end
303
+ end
304
+ end
@@ -0,0 +1,451 @@
1
+ require "rest-client"
2
+ require 'uri'
3
+ require 'json/pure'
4
+
5
+ module App42
6
+ module Client
7
+ class App42RestClient
8
+ attr_accessor :host
9
+
10
+ include App42::Base::Util
11
+
12
+ def initialize(host= HOST)#:nodoc:
13
+ @host = host
14
+ @exception_msg = nil
15
+ end
16
+
17
+ #
18
+ # @param signature
19
+ # @param url
20
+ # @param params
21
+ # @return
22
+ # @rescue Exception
23
+ #
24
+ def get(signature, url, params)
25
+ params.store("signature", signature)
26
+ resource, json_obj = "", nil
27
+ resource << @host << url
28
+ resource = URI.escape(resource).gsub("+", "%20")
29
+ begin
30
+ response = RestClient.get resource, :params => params, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :apiKey => params['apiKey'], :timeStamp => params['timeStamp'], :signature => signature
31
+ rescue Errno::ECONNREFUSED => e
32
+ @exception_msg = "#{Message::COULD_NOT_CONNECT}"
33
+ rescue SystemCallError, SocketError => e
34
+ @exception_msg = "#{Message::NW_CONNECTION_ERROR}"
35
+ rescue RestClient::RequestTimeout => e
36
+ @exception_msg = "#{Message::TIME_OUT_ERROR}"
37
+ rescue => e
38
+ if e.response.code != 200
39
+ begin
40
+ json_obj = JSON.parse(e.response)
41
+ rescue => e
42
+ message "#{Message::SERVER_ERROR}", true, 'red'
43
+ exit!
44
+ end
45
+ end
46
+ if e.response.code == 404
47
+ json_obj['app42Fault'].nil? ?
48
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
49
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
50
+
51
+ elsif e.response.code == 400
52
+ json_obj['app42Fault'].nil? ?
53
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
54
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
55
+
56
+ elsif e.response.code == 401
57
+ json_obj['app42Fault'].nil? ?
58
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
59
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
60
+
61
+ elsif e.response.code == 413
62
+ json_obj['app42Fault'].nil? ?
63
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
64
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
65
+
66
+ elsif e.response.code == 500
67
+ json_obj['app42Fault'].nil? ?
68
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
69
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
70
+
71
+ else
72
+ message "#{Message::SERVER_ERROR}", true, 'red'
73
+ exit!
74
+ end
75
+ ensure
76
+ unless @exception_msg.nil?
77
+ # TODO, may need later
78
+ #app42_client_info
79
+ message "#{@exception_msg}", true, 'red'
80
+ exit!
81
+ end
82
+ end
83
+
84
+ response = json_parse(response)
85
+ status = response['app42']['response']['code']
86
+ if request_failed?(status)
87
+ # TODO, may need later
88
+ #app42_client_info
89
+ parse_error_message(status, response)
90
+ end if status
91
+
92
+ return response["app42"]["response"]
93
+
94
+ rescue JSON::ParserError => e
95
+ # TODO, may need later
96
+ #app42_client_info
97
+ message "#{Message::SERVER_ERROR}", true, 'red'
98
+ exit!
99
+ end
100
+
101
+ #
102
+ # @param signature
103
+ # @param url
104
+ # @param params
105
+ # @param bodyPayload
106
+ # @return
107
+ # @rescue Exception
108
+ #
109
+ def post(signature, url, params, body)
110
+ params.store("signature", signature)
111
+ resource = ""
112
+ resource << @host << url
113
+ resource = URI.escape(resource).gsub("+", "%20")
114
+ begin
115
+ response = RestClient.post resource, body, :params => params, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :apiKey => params['apiKey'], :timeStamp => params['timeStamp'], :signature => signature
116
+ rescue Errno::ECONNREFUSED => e
117
+ @exception_msg = "#{Message::COULD_NOT_CONNECT}"
118
+ rescue SystemCallError, SocketError => e
119
+ @exception_msg = "#{Message::NW_CONNECTION_ERROR}"
120
+ rescue RestClient::RequestTimeout => e
121
+ @exception_msg = "#{Message::TIME_OUT_ERROR}"
122
+ rescue => e
123
+ if e.response.code != 200
124
+ begin
125
+ json_obj = JSON.parse(e.response)
126
+ rescue => e
127
+ message "#{Message::SERVER_ERROR}", true, 'red'
128
+ exit!
129
+ end
130
+ end
131
+ if e.response.code == 404
132
+ json_obj['app42Fault'].nil? ?
133
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
134
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
135
+
136
+ elsif e.response.code == 400
137
+ json_obj['app42Fault'].nil? ?
138
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
139
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
140
+
141
+ elsif e.response.code == 401
142
+ json_obj['app42Fault'].nil? ?
143
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
144
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
145
+
146
+ elsif e.response.code == 413
147
+ json_obj['app42Fault'].nil? ?
148
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
149
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
150
+
151
+ elsif e.response.code == 500
152
+ json_obj['app42Fault'].nil? ?
153
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
154
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
155
+
156
+ else
157
+ message "#{Message::SERVER_ERROR}", true, 'red'
158
+ exit!
159
+ end
160
+ ensure
161
+ unless @exception_msg.nil?
162
+ #app42_client_info
163
+ message "#{@exception_msg}", true, 'red'
164
+ exit!
165
+ end
166
+ end
167
+
168
+ response = json_parse(response)
169
+ status = response['app42']['response']['code']
170
+
171
+ if request_failed?(status)
172
+ #app42_client_info
173
+ parse_error_message(status, response)
174
+ end if status
175
+
176
+ return response["app42"]["response"]
177
+
178
+ rescue JSON::ParserError => e
179
+ #app42_client_info
180
+ message "#{Message::SERVER_ERROR}", true, 'red'
181
+ exit!
182
+ end
183
+
184
+ #
185
+ # @param signature
186
+ # @param url
187
+ # @param params
188
+ # @param bodyPayload
189
+ # @return
190
+ # @rescue Exception
191
+ #
192
+ def put(signature, url, params, body)
193
+ params.store("signature", signature)
194
+ resource = ""
195
+ resource << @host << url
196
+ resource = URI.escape(resource).gsub("+", "%20")
197
+ begin
198
+ response = RestClient.put resource, body, :params => params, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :apiKey => params['apiKey'], :timeStamp => params['timeStamp'], :signature => signature
199
+ rescue Errno::ECONNREFUSED => e
200
+ @exception_msg = "#{Message::COULD_NOT_CONNECT}"
201
+ rescue SystemCallError, SocketError => e
202
+ @exception_msg = "#{Message::NW_CONNECTION_ERROR}"
203
+ rescue RestClient::RequestTimeout => e
204
+ @exception_msg = "#{Message::TIME_OUT_ERROR}"
205
+ rescue => e
206
+ if e.response.code != 200
207
+ begin
208
+ json_obj = JSON.parse(e.response)
209
+ rescue => e
210
+ message "#{Message::SERVER_ERROR}", true, 'red'
211
+ exit!
212
+ end
213
+ end
214
+ if e.response.code == 404
215
+ json_obj['app42Fault'].nil? ?
216
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
217
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
218
+
219
+ elsif e.response.code == 400
220
+ json_obj['app42Fault'].nil? ?
221
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
222
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
223
+
224
+ elsif e.response.code == 401
225
+ json_obj['app42Fault'].nil? ?
226
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
227
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
228
+
229
+ elsif e.response.code == 413
230
+ json_obj['app42Fault'].nil? ?
231
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
232
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
233
+
234
+ elsif e.response.code == 500
235
+ json_obj['app42Fault'].nil? ?
236
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
237
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
238
+
239
+ else
240
+ message "#{Message::SERVER_ERROR}", true, 'red'
241
+ exit!
242
+ end
243
+ ensure
244
+ unless @exception_msg.nil?
245
+ #app42_client_info
246
+ message "#{@exception_msg}", true, 'red'
247
+ exit!
248
+ end
249
+ end
250
+
251
+ response = json_parse(response)
252
+ status = response['app42']['response']['code']
253
+
254
+ if request_failed?(status)
255
+ #app42_client_info
256
+ parse_error_message(status, response)
257
+ end if status
258
+
259
+ return response["app42"]["response"]
260
+
261
+ rescue JSON::ParserError => e
262
+ #app42_client_info
263
+ message "#{Message::SERVER_ERROR}", true, 'red'
264
+ exit!
265
+ end
266
+
267
+ #
268
+ # @param signature
269
+ # @param url
270
+ # @param params
271
+ # @return
272
+ # @rescue Exception
273
+ #
274
+ def delete(signature, url, params)
275
+ params.store("signature", signature)
276
+ resource = ""
277
+ resource << @host << url
278
+ resource = URI.escape(resource).gsub("+", "%20")
279
+ begin
280
+ response = RestClient.delete resource, :params => params, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :apiKey => params['apiKey'], :timeStamp => params['timeStamp'], :signature => signature
281
+ rescue Errno::ECONNREFUSED => e
282
+ @exception_msg = "#{Message::COULD_NOT_CONNECT}"
283
+ rescue SystemCallError, SocketError => e
284
+ @exception_msg = "#{Message::NW_CONNECTION_ERROR}"
285
+ rescue RestClient::RequestTimeout => e
286
+ @exception_msg = "#{Message::TIME_OUT_ERROR}"
287
+ rescue => e
288
+ if e.response.code != 200
289
+ begin
290
+ json_obj = JSON.parse(e.response)
291
+ rescue => e
292
+ message "#{Message::SERVER_ERROR}", true, 'red'
293
+ exit!
294
+ end
295
+ end
296
+ if e.response.code == 404
297
+ json_obj['app42Fault'].nil? ?
298
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
299
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
300
+
301
+ elsif e.response.code == 400
302
+ json_obj['app42Fault'].nil? ?
303
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
304
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
305
+
306
+ elsif e.response.code == 401
307
+ json_obj['app42Fault'].nil? ?
308
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
309
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
310
+
311
+ elsif e.response.code == 413
312
+ json_obj['app42Fault'].nil? ?
313
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
314
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
315
+
316
+ elsif e.response.code == 500
317
+ json_obj['app42Fault'].nil? ?
318
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
319
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
320
+
321
+ else
322
+ message "#{Message::SERVER_ERROR}", true, 'red'
323
+ exit!
324
+ end
325
+ ensure
326
+ unless @exception_msg.nil?
327
+ #app42_client_info
328
+ message "#{@exception_msg}", true, 'red'
329
+ exit!
330
+ end
331
+ end
332
+
333
+ response = json_parse(response)
334
+ status = response['app42']['response']['code']
335
+
336
+ if request_failed?(status)
337
+ #app42_client_info
338
+ parse_error_message(status, response)
339
+ end if status
340
+
341
+ return response["app42"]["response"]
342
+
343
+ rescue JSON::ParserError => e
344
+ #app42_client_info
345
+ message "#{Message::SERVER_ERROR}", true, 'red'
346
+ exit!
347
+ end
348
+
349
+ # MULTIPART for upload
350
+ #
351
+ # @param signature
352
+ # @param url
353
+ # @param params
354
+ # @param post_params
355
+ # @ source_path
356
+ # @return
357
+ # @rescue Exception
358
+ #
359
+ def multipart(signature, url, params, post_params, source_path)
360
+ params.store("signature", signature)
361
+ resource = ""
362
+ resource << @host << url
363
+ resource = URI.escape(resource).gsub("+", "%20")
364
+ begin
365
+ # TODO, should be dynamic for multiple calls
366
+ response = RestClient.put( resource,
367
+ {
368
+ :source_zip => File.open(source_path, "rb"),
369
+ :name => 'test',
370
+ :type => 'zip'
371
+ },
372
+ :accept => JSON_MIME_TYPE,
373
+ :content_type => JSON_MIME_TYPE,
374
+ :params => post_params,
375
+ :apiKey => params['apiKey'],
376
+ :appName => params['appName'],
377
+ :timeStamp => params['timeStamp'],
378
+ :signature => signature
379
+ )
380
+ rescue Errno::ECONNREFUSED => e
381
+ @exception_msg = "#{Message::COULD_NOT_CONNECT}"
382
+ rescue SystemCallError, SocketError => e
383
+ @exception_msg = "#{Message::NW_CONNECTION_ERROR}"
384
+ rescue RestClient::RequestTimeout => e
385
+ @exception_msg = "#{Message::TIME_OUT_ERROR}"
386
+ rescue => e
387
+ if e.response.code != 200
388
+ begin
389
+ json_obj = JSON.parse(e.response)
390
+ rescue => e
391
+ message "#{Message::SERVER_ERROR}", true, 'red'
392
+ exit!
393
+ end
394
+ end
395
+ if e.response.code == 404
396
+ json_obj['app42Fault'].nil? ?
397
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
398
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
399
+
400
+ elsif e.response.code == 400
401
+ json_obj['app42Fault'].nil? ?
402
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
403
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
404
+
405
+ elsif e.response.code == 401
406
+ json_obj['app42Fault'].nil? ?
407
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
408
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
409
+
410
+ elsif e.response.code == 413
411
+ json_obj['app42Fault'].nil? ?
412
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
413
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
414
+
415
+ elsif e.response.code == 500
416
+ json_obj['app42Fault'].nil? ?
417
+ @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" :
418
+ parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault'])
419
+
420
+ else
421
+ message "#{Message::SERVER_ERROR}", true, 'red'
422
+ exit!
423
+ end
424
+ ensure
425
+ unless @exception_msg.nil?
426
+ #app42_client_info
427
+ message "#{@exception_msg}", true, 'red'
428
+ exit!
429
+ end
430
+ end
431
+
432
+ response = json_parse(response)
433
+ status = response['app42']['response']['code']
434
+
435
+ if request_failed?(status)
436
+ #app42_client_info
437
+ parse_error_message(status, response)
438
+ end if status
439
+
440
+ return response["app42"]["response"]
441
+
442
+ rescue JSON::ParserError => e
443
+ #app42_client_info
444
+ message "#{Message::SERVER_ERROR}", true, 'red'
445
+ exit!
446
+ end
447
+
448
+ end
449
+
450
+ end
451
+ end