require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'rubygems' require 'App42Response.rb' require 'appTab/PackageData.rb' require 'appTab/PackageResponseBuilder.rb' module App42 module AppTab class PackageService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "PackageService->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "package" @version = "1.0" end # # Create a package for the specified scheme # # @param schemeName # - Name for which you want to create package # @param pkgData # - Details of the package like name, duration, price, currency, # description. # # @return The created packageData object # # @raise App42Exception # def create_package(schemeName, pkgData) puts "create Package Called " puts "Base url #{@base_url}" response = nil; packageObj1 = nil; packageObj1 = PackageData.new() util = Util.new util.throwExceptionIfNullOrBlank(schemeName, "Name"); util.throwExceptionIfNullOrBlank(pkgData, "PackageDetails"); begin connection = App42::Connection::RESTConnection.new(@base_url) packageObj = Hash.new() packageObj.store("name", pkgData.name); packageObj.store("duration", pkgData.duration); packageObj.store("price", pkgData.price); packageObj.store("currency", pkgData.currency); packageObj.store("description", pkgData.description); body = {'app42' => {"app"=> {"scheme"=>{ "name" => schemeName, "packageDetails" => packageObj }}}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts params puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}" response = connection.post(signature, resource_url, query_params, body) package = PackageResponseBuilder.new packageObj1 = package.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return packageObj1 end # # Add a storage scheme into the existing packageName # # @param schemeName # - Name for which you want to create package # @param packageName # - Name of the package in which you want to add storage, # description. # @param storageName # -Name of the storage you wan't to insert # # @return The created packageData object # # @raise App42Exception # def add_storage_in_to_package(schemeName, packageName, storageName) puts "add_storage_in_to_package Called " puts "Base url #{@base_url}" response = nil; packageObj = nil; packageObj = PackageData.new() util = Util.new util.throwExceptionIfNullOrBlank(schemeName, "Name"); util.throwExceptionIfNullOrBlank(packageName, "PackageDetails"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"app" =>{ "schemeName" => schemeName, "packageName" => packageName, "storageName" => storageName }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts params puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/package/#{packageName}/storage/#{storageName}" response = connection.post(signature, resource_url, query_params, body) package = PackageResponseBuilder.new packageObj = package.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return packageObj end # # NEW METHOD # def add_bandwidth_in_to_package(schemeName, packageName, bandwidthName) puts "addBandwidthInToPackage Called " puts "Base url #{@base_url}" response = nil; packageObj = nil; packageObj = PackageData.new() util = Util.new util.throwExceptionIfNullOrBlank(schemeName, "Name"); util.throwExceptionIfNullOrBlank(packageName, "PackageDetails"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"app" =>{ "schemeName" => schemeName, "packageName" => packageName, "bandwidthName" => bandwidthName }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts params puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/package/#{packageName}/bandwidth/#{bandwidthName}" response = connection.post(signature, resource_url, query_params, body) package = PackageResponseBuilder.new packageObj = package.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return packageObj end # # NEW METHOD # def add_feature_in_to_package(schemeName, packageName, featureList) puts "addFeatureInToPackage Called " puts "Base url #{@base_url}" response = nil; packageObj = nil; packageObj = PackageData.new() util = Util.new util.throwExceptionIfNullOrBlank(schemeName, "Name"); util.throwExceptionIfNullOrBlank(packageName, "packageName"); util.throwExceptionIfNullOrBlank(featureList, "FeatureList"); begin connection = App42::Connection::RESTConnection.new(@base_url) puts "-------connection---- #{connection}" featureArray = Array.new() for feature in featureList featureArray.push(feature); end puts "-------featureArray---- #{featureArray}" body = {'app42' => {"app" =>{ "schemeName" => schemeName, "packageName" => packageName, "featureName" => featureArray }}}.to_json puts "------Body -------- #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts params puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/package/#{packageName}/feature" puts "-------resource_url---- #{resource_url}" response = connection.post(signature, resource_url, query_params, body) puts "-------response---- #{response}" package = PackageResponseBuilder.new packageObj = package.buildResponse(response) puts "-------packageObj---- #{packageObj}" rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return packageObj end # # NEW METHOD # def create_package_with_usage(schemeName, pkgData) puts "addFeatureInToPackage Called " puts "Base url #{@base_url}" response = nil; packageDataObj1 = nil; packageDataObj1 = PackageData.new() util = Util.new util.throwExceptionIfNullOrBlank(schemeName, "Name"); util.throwExceptionIfNullOrBlank(pkgData, "PackageDetails"); begin storage = pkgData.storageData(); storageObj = Hash.new() storageObj.store("name", storage.name()); storageObj.store("price", storage.price); storageObj.store("unit", storage.unit); storageObj.store("space", storage.space); storageObj.store("description", storage.description); bandwidth = pkgData.bandwidthData(); bandwidthObj = Hash.new() bandwidthObj.store("name", bandwidth.name()); bandwidthObj.store("price", bandwidth.price); bandwidthObj.store("unit", bandwidth.unit); bandwidthObj.store("bandwidth", bandwidth.usage); bandwidthObj.store("description", bandwidth.description); feature = pkgData.featureData(); featureObj = Hash.new() featureObj.store("price", feature.price()); featureObj.store("name", feature.name); featureObj.store("description", feature.description); packageObj = Hash.new() packageObj.store("name", pkgData.name()); packageObj.store("duration", pkgData.duration()); packageObj.store("price", pkgData.price()); packageObj.store("currency", pkgData.currency()); packageObj.store("description", pkgData.description()); packageObj.store("storage", storageObj); packageObj.store("bandwidth", bandwidthObj); packageObj.store("feature", featureObj); connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"app"=> {"scheme"=>{ "name" => schemeName, "packageDetails" => packageObj }}}}.to_json query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("body", body) puts params puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/package/#{schemeName}" response = connection.post(signature, resource_url, query_params, body) puts " -------- response --------- #{response}" package = PackageResponseBuilder.new packageDataObj1 = package.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return packageDataObj1 end # # @param schemeName # @return # def get_all_packages(schemeName) puts "get Packages called" puts "Base url #{@base_url}" response = nil; packageList = nil; packageList = Array.new(); util = Util.new util.throwExceptionIfNullOrBlank(schemeName, "Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts params params.store("schemeName", schemeName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/scheme/#{schemeName}" response = connection.get(signature, resource_url, query_params) package = PackageResponseBuilder.new packageList = package.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return packageList end # # @param schemeName # @param packageName # @return # def get_package_by_package_name(schemeName, packageName) puts "get Packages By Name called" puts "Base url #{@base_url}" response = nil; packageObj = nil; packageObj = PackageData.new() util = Util.new util.throwExceptionIfNullOrBlank(schemeName, "Name"); util.throwExceptionIfNullOrBlank(packageName, "Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts params params.store("schemeName", schemeName); params.store("packageName", packageName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{packageName}/scheme/#{schemeName}" response = connection.get(signature, resource_url, query_params) package = PackageResponseBuilder.new packageObj = package.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return packageObj end # # @param schemeName # @param packageName # @param storageName # @param price # @param unit # @param space # @return # @throws App42Exception # def add_limit_to_storage(schemeName, packageName, storageName, price, unit, space) puts "add_limit_to_storage called" puts "Base url #{@base_url}" response = nil; packageObj = nil; packageObj = PackageData.new() util = Util.new util.throwExceptionIfNullOrBlank(schemeName, "Name"); util.throwExceptionIfNullOrBlank(packageName, "PackageDetails"); util.throwExceptionIfNullOrBlank(storageName, "StorageName"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"app"=> { "schemeName" => schemeName, "packageName" => packageName, "storageName" => storageName, "storagePrice" => price, "storageUnit" => unit, "storageSpace" => space }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts params puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/storage/#{storageName}" response = connection.post(signature, resource_url, query_params) package = PackageResponseBuilder.new packageObj = package.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return packageObj end # # @param schemeName # @param packageName # @param bandwidthName # @param price # @param unit # @param bandwidth # @return # @throws App42Exception # def add_limit_to_bandwidth(schemeName, packageName, bandwidthName, price, unit, bandwidth) puts "addLimitToBandwidth called" puts "Base url #{@base_url}" response = nil; packageObj = nil; packageObj = PackageData.new() util = Util.new util.throwExceptionIfNullOrBlank(schemeName, "Name"); util.throwExceptionIfNullOrBlank(packageName, "PackageDetails"); util.throwExceptionIfNullOrBlank(bandwidthName, "BandwidthName"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"app"=> { "schemeName" => schemeName, "packageName" => packageName, "bandwidthName" => bandwidthName, "bandwidthPrice" => price, "bandwidthUnit" => unit, "bandwidth" => bandwidth }}}.to_json puts "Body #{body}" query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone puts params puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/bandwidth/#{bandwidthName}" response = connection.post(signature, resource_url, query_params) package = PackageResponseBuilder.new packageObj = package.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return packageObj end end end end