require "rest-client" require 'uri' require 'json/pure' module App42 module Client class App42RestClient attr_accessor :host include App42::Base::Util def initialize(host= HOST)#:nodoc: @host = host @exception_msg = nil end # @param signature # @param url # @param params # @return # @rescue Exception def get(signature, url, params) params.store("signature", signature) resource, json_obj = "", nil resource << @host << url resource = URI.escape(resource).gsub("+", "%20") begin response = RestClient.get resource, :params => params, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :apiKey => params['apiKey'], :timeStamp => params['timeStamp'], :signature => signature rescue Errno::ECONNREFUSED => e @exception_msg = "#{Message::COULD_NOT_CONNECT}" rescue SystemCallError, SocketError => e @exception_msg = "#{Message::NW_CONNECTION_ERROR}" rescue RestClient::RequestTimeout => e @exception_msg = "#{Message::TIME_OUT_ERROR}" rescue => e if e.response.code != 200 begin json_obj = JSON.parse(e.response) rescue => e message "#{Message::SERVER_ERROR}", true, 'red' exit! end end if e.response.code == 404 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 400 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 401 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 413 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 500 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) else message "#{Message::SERVER_ERROR}", true, 'red' exit! end ensure unless @exception_msg.nil? # TODO, may need later #app42_client_info message "#{@exception_msg}", true, 'red' exit! end end response = json_parse(response) status = response['app42']['response']['code'] if request_failed?(status) # TODO, may need later #app42_client_info parse_error_message(status, response) end if status return response["app42"]["response"] rescue JSON::ParserError => e # TODO, may need later #app42_client_info message "#{Message::SERVER_ERROR}", true, 'red' exit! end # @param signature # @param url # @param params # @param bodyPayload # @return # @rescue Exception def post(signature, url, params, body) params.store("signature", signature) resource = "" resource << @host << url resource = URI.escape(resource).gsub("+", "%20") begin response = RestClient.post resource, body, :params => params, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :apiKey => params['apiKey'], :timeStamp => params['timeStamp'], :signature => signature rescue Errno::ECONNREFUSED => e @exception_msg = "#{Message::COULD_NOT_CONNECT}" rescue SystemCallError, SocketError => e @exception_msg = "#{Message::NW_CONNECTION_ERROR}" rescue RestClient::RequestTimeout => e @exception_msg = "#{Message::TIME_OUT_ERROR}" rescue => e if e.response.code != 200 begin json_obj = JSON.parse(e.response) rescue => e message "#{Message::SERVER_ERROR}", true, 'red' exit! end end if e.response.code == 404 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 400 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 401 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 413 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 500 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) else message "#{Message::SERVER_ERROR}", true, 'red' exit! end ensure unless @exception_msg.nil? #app42_client_info message "#{@exception_msg}", true, 'red' exit! end end response = json_parse(response) status = response['app42']['response']['code'] if request_failed?(status) #app42_client_info parse_error_message(status, response) end if status return response["app42"]["response"] rescue JSON::ParserError => e #app42_client_info message "#{Message::SERVER_ERROR}", true, 'red' exit! end # @param signature # @param url # @param params # @param bodyPayload # @return # @rescue Exception def put(signature, url, params, body) params.store("signature", signature) resource = "" resource << @host << url resource = URI.escape(resource).gsub("+", "%20") begin response = RestClient.put resource, body, :params => params, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :apiKey => params['apiKey'], :timeStamp => params['timeStamp'], :signature => signature rescue Errno::ECONNREFUSED => e @exception_msg = "#{Message::COULD_NOT_CONNECT}" rescue SystemCallError, SocketError => e @exception_msg = "#{Message::NW_CONNECTION_ERROR}" rescue RestClient::RequestTimeout => e @exception_msg = "#{Message::TIME_OUT_ERROR}" rescue => e if e.response.code != 200 begin json_obj = JSON.parse(e.response) rescue => e message "#{Message::SERVER_ERROR}", true, 'red' exit! end end if e.response.code == 404 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 400 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 401 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 413 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 500 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) else message "#{Message::SERVER_ERROR}", true, 'red' exit! end ensure unless @exception_msg.nil? #app42_client_info message "#{@exception_msg}", true, 'red' exit! end end response = json_parse(response) status = response['app42']['response']['code'] if request_failed?(status) #app42_client_info parse_error_message(status, response) end if status return response["app42"]["response"] rescue JSON::ParserError => e #app42_client_info message "#{Message::SERVER_ERROR}", true, 'red' exit! end # @param signature # @param url # @param params # @return # @rescue Exception def delete(signature, url, params) params.store("signature", signature) resource = "" resource << @host << url resource = URI.escape(resource).gsub("+", "%20") begin response = RestClient.delete resource, :params => params, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :apiKey => params['apiKey'], :timeStamp => params['timeStamp'], :signature => signature rescue Errno::ECONNREFUSED => e @exception_msg = "#{Message::COULD_NOT_CONNECT}" rescue SystemCallError, SocketError => e @exception_msg = "#{Message::NW_CONNECTION_ERROR}" rescue RestClient::RequestTimeout => e @exception_msg = "#{Message::TIME_OUT_ERROR}" rescue => e if e.response.code != 200 begin json_obj = JSON.parse(e.response) rescue => e message "#{Message::SERVER_ERROR}", true, 'red' exit! end end if e.response.code == 404 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 400 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 401 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 413 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 500 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) else message "#{Message::SERVER_ERROR}", true, 'red' exit! end ensure unless @exception_msg.nil? #app42_client_info message "#{@exception_msg}", true, 'red' exit! end end response = json_parse(response) status = response['app42']['response']['code'] if request_failed?(status) #app42_client_info parse_error_message(status, response) end if status return response["app42"]["response"] rescue JSON::ParserError => e #app42_client_info message "#{Message::SERVER_ERROR}", true, 'red' exit! end # MULTIPART for upload # # @param signature # @param url # @param params # @param post_params # @ source_path # @return # @rescue Exception # def multipart(signature, url, params, post_params, source_path) params.store("signature", signature) resource = "" resource << @host << url resource = URI.escape(resource).gsub("+", "%20") begin # TODO, should be dynamic for multiple calls response = RestClient.put( resource, { :source_zip => File.open(source_path, "rb"), :name => 'test', :type => 'zip' }, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :params => post_params, :apiKey => params['apiKey'], :appName => params['appName'], :timeStamp => params['timeStamp'], :signature => signature ) rescue Errno::ECONNREFUSED => e @exception_msg = "#{Message::COULD_NOT_CONNECT}" rescue Errno::ENOENT => e @exception_msg = "#{e.message}" rescue SystemCallError, SocketError => e @exception_msg = "#{Message::NW_CONNECTION_ERROR}" rescue RestClient::RequestTimeout => e @exception_msg = "#{Message::TIME_OUT_ERROR}" rescue => e if e.response.code != 200 begin json_obj = JSON.parse(e.response) rescue => e message "#{Message::SERVER_ERROR}", true, 'red' exit! end end if e.response.code == 404 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 400 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 401 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 413 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) elsif e.response.code == 500 json_obj['app42Fault'].nil? ? @exception_msg = "#{Message::BAD_RESPONSE_ERROR} : #{e}" : parse_error_message(json_obj['app42Fault']['httpErrorCode'], json_obj['app42Fault']) else message "#{Message::SERVER_ERROR}", true, 'red' exit! end ensure unless @exception_msg.nil? #app42_client_info message "#{@exception_msg}", true, 'red' exit! end end response = json_parse(response) status = response['app42']['response']['code'] if request_failed?(status) #app42_client_info parse_error_message(status, response) end if status return response["app42"]["response"] rescue JSON::ParserError => e #app42_client_info message "#{Message::SERVER_ERROR}", true, 'red' exit! end end end end