require "rest-client" require 'uri' module App42 module Connection class RESTConnection attr_accessor :base_url def initialize(base_url) puts "RESTConnection->initialize" puts "Base url 0 #{@base_url}" @base_url = base_url end # # @param signature # @param url # @param params # @return # @throws Exception # def get(signature, url, params) params.store("signature", signature) resource = "" resource << self.base_url << url puts "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2" puts params puts resource puts signature resource = URI.escape(resource).gsub("+", "%20") begin response = RestClient.get resource,:accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :params => params puts response.headers puts response.body rescue => e if e.response.code!=200 jsonObj = nil begin jsonObj = JSON.parse(e.response) rescue => e raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) end end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # # @param signature # @param url # @param params # @param bodyPayload # @return # @throws IOException # @throws ClientProtocolException # def post(signature, url, params, body) params.store("signature", signature) resource = "" resource << self.base_url << url resource = URI.escape(resource).gsub("+", "%20") begin response = RestClient.post resource, body, :accept => JSON_MIME_TYPE, :content_type => JSON_MIME_TYPE, :params => params puts response.code puts response.to_str rescue => e puts e if e.response.code!=200 jsonObj = nil begin jsonObj = JSON.parse(e.response) rescue => e raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) end end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # # @param signature # @param url # @param params # @param bodyPayload # @return # @throws IOException # @throws ClientProtocolException # def put(signature, url, params, body) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") begin response = RestClient.put resource, body,:accept => 'application/json', :content_type => 'application/json', :params => params puts response.code puts response.to_str rescue => e if e.response.code!=200 jsonObj = JSON.parse(e.response) rescue nil end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # # @param signature # @param url # @param params # @return # @throws IOException # @throws ClientProtocolException # def delete(signature, url, params) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") begin response = RestClient.delete resource, :accept => 'application/json', :content_type => 'application/json', :params => params puts response.code puts response.to_str rescue => e if e.response.code!=200 jsonObj = nil begin jsonObj = JSON.parse(e.response) rescue => e raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) end end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # MULTIPART for upload api # # @param signature # @param url # @param params # @param post_params # @ filePath # @return # @throws IOException # @throws ClientProtocolException # def multipart(signature, url, params, post_params, filePath) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") puts "Connection to URL #{resource}" begin response = RestClient.post( resource, { :uploadFile => File.open(filePath, "rb"), :name =>post_params['name'], :type =>post_params['type'], :userName =>post_params['userName'], :description =>post_params['description'] }, :accept => 'application/json', :content_type => 'application/json', :params =>params ) puts response.code puts response.to_str rescue => e puts e if e.response.code!=200 jsonObj = JSON.parse(e.response) end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # MULTIPART for upload api via Stream # # @param signature # @param url # @param params # @param post_params # @ filePath # @return # @throws IOException # @throws ClientProtocolException # def multipartStream(signature, url, params, post_params, inputStream) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") puts "Connection to URL #{resource}" begin response = RestClient.post( resource, { :uploadFile => inputStream, :name =>post_params['name'], :type =>post_params['type'], :userName =>post_params['userName'], :description =>post_params['description'] }, :accept => 'application/json', :content_type => 'application/json', :params =>params ) puts response.code puts response.to_str rescue => e puts e if e.response.code!=200 jsonObj = JSON.parse(e.response) end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # MULTIPART for gallery api # # @param signature # @param url # @param params # @param post_params # @ filePath # @return # @throws IOException # @throws ClientProtocolException # def photoMultipart(signature, url, params, post_params, filePath) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") puts "Connection to URL #{resource}" begin response = RestClient.post( resource, { :imageFile => File.open(filePath, "rb"), :albumName =>post_params['albumName'], :name =>post_params['name'], :description =>post_params['description'] }, :accept => 'application/json', :content_type => 'application/json', :params =>params ) puts response.code puts response.to_str rescue => e if e.response.code!=200 jsonObj = JSON.parse(e.response) end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # MULTIPART for gallery api via Stream # # @param signature # @param url # @param params # @param post_params # @ filePath # @return # @throws IOException # @throws ClientProtocolException # def photoMultipartStream(signature, url, params, post_params, filePath) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") puts "Connection to URL #{resource}" begin response = RestClient.post( resource, { :imageFile => filePath, :albumName =>post_params['albumName'], :name =>post_params['name'], :description =>post_params['description'] }, :accept => 'application/json', :content_type => 'application/json', :params =>params ) puts response.code puts response.to_str rescue => e if e.response.code!=200 jsonObj = JSON.parse(e.response) end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # MULTIPART for Image Processor api # # @param signature # @param url # @param params # @param post_params # @ filePath # @return # @throws IOException # @throws ClientProtocolException # def imageMultipart(signature, url, params, post_params, imagePath) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") puts "Connection to URL #{resource}" begin response = RestClient.post( resource, { :imageFile => File.open(imagePath, "rb"), :name =>post_params['name'], :width =>post_params['width'], :height =>post_params['height'], :formatToConvert =>post_params['formatToConvert'], :percentage =>post_params['percentage'], :x =>post_params['x'], :y =>post_params['y'], }, :accept => 'application/json', :content_type => 'application/json', :params =>params ) puts response.code puts response.to_str rescue => e puts "e-----#{e}" if e.response.code!=200 jsonObj =JSON.parse(e.response) end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # MULTIPART for Image Processor api # # @param signature # @param url # @param params # @param post_params # @ filePath # @return # @throws IOException # @throws ClientProtocolException # def imageMultipartStream(signature, url, params, post_params, imagePath) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") puts "Connection to URL #{resource}" begin response = RestClient.post( resource, { :imageFile => imagePath, :name =>post_params['name'], :width =>post_params['width'], :height =>post_params['height'], :formatToConvert =>post_params['formatToConvert'], :percentage =>post_params['percentage'], :x =>post_params['x'], :y =>post_params['y'], }, :accept => 'application/json', :content_type => 'application/json', :params =>params ) puts response.code puts response.to_str rescue => e puts "e-----#{e}" if e.response.code!=200 jsonObj =JSON.parse(e.response) end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # MULTIPART for catalogue api # # @param signature # @param url # @param params # @param post_params # @ filePath # @return # @throws IOException # @throws ClientProtocolException # def shopMultipart(signature, url, params, post_params, imageFile) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") puts "Connection to URL #{resource}" begin response = RestClient.post( resource, { :imageFile => imageFile, :itemId =>post_params['itemId'], :name =>post_params['name'], :description =>post_params['description'], :price =>post_params['price'], }, :accept => 'application/json', :content_type => 'application/json', :params =>params ) puts response.code puts response.to_str rescue => e puts "e-----#{e}" if e.response.code!=200 jsonObj =JSON.parse(e.response) end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # MULTIPART for Recommended api # # @param signature # @param url # @param params # @param post_params # @ filePath # @return # @throws IOException # @throws ClientProtocolException # def recommendMultipart(signature, url, params, post_params, preferenceFile) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") puts "Connection to URL #{resource}" begin response = RestClient.post( resource, { :preferenceFile => File.open(preferenceFile, "rb"), :name =>post_params['name'], :preferenceFilePath =>post_params['preferenceFilePath'], :description =>post_params['description'] }, :accept => 'application/json', :content_type => 'application/json', :params =>params ) puts response.code puts response.to_str rescue => e puts "e-----#{e}" if e.response.code!=200 jsonObj =JSON.parse(e.response) end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end # MULTIPART for Recommended api via Stream # # @param signature # @param url # @param params # @param post_params # @ filePath # @return # @throws IOException # @throws ClientProtocolException # def recommendMultipartStream(signature, url, params, post_params, inputStream) params.store("signature", signature) resource = "" resource << self.base_url() resource << url resource = URI.escape(resource).gsub("+", "%20") puts "Connection to URL #{resource}" begin response = RestClient.post( resource, { :preferenceFile => inputStream, :name =>post_params['name'], :preferenceFilePath =>post_params['preferenceFilePath'], :description =>post_params['description'] }, :accept => 'application/json', :content_type => 'application/json', :params =>params ) puts response.code puts response.to_str rescue => e puts "e-----#{e}" if e.response.code!=200 jsonObj =JSON.parse(e.response) end if e.response.code == 404 raise App42NotFoundException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 400 raise App42BadParameterException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 401 raise App42SecurityException.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) elsif e.response.code == 413 raise App42LimitException.new(jsonObj['app42Fault']['details'], e.response.code,1413) elsif e.response.code == 500 raise App42Exception.new(jsonObj['app42Fault']['details'], e.response.code,jsonObj['app42Fault']['appErrorCode']) else raise App42Exception.new("We are sorry something has gone wrong on our side", 500, 1500); end end return response end end end end