require 'connection/RESTConnection.rb' require 'util/util.rb' require 'appTab/UsageResponseBuilder.rb' require 'appTab/Usage.rb' require 'appTab/BandwidthUnit.rb' require 'appTab/TimeUnit.rb' require 'appTab/StorageUnit.rb' require 'json/pure' module App42 module AppTab # # # UsageService is part of AppTab which is a rating, metering, charging and # billing engine. # # This service allows app developers to specify the rate for a particular usage # parameter. e.g. Level - Storage - space, Bandwidth, Time, Feature, Level of # game, OneTime - Which can be used for one time charging e.g. charging for # downloads and License for traditional license based charging. # # It provides methods for first creating the scheme for charging which # specifies the unit of charging and the associated price. Subsequently a # chargeXXX call has to be made for charging. e.g. If an App developer wants to # charge on Storage, He can use the method createStorageCharge and specify that # for 10 KB/MB/GB TB the price is 10 USD. Once the scheme is created. The app # developer can call the chargeStorage whenever storage is utilized. e.g. 5MB. # # Using the Bill service the app developer can find out what is the monthly # bill for a particular user based on his utilization. The bill is calculated # based on scheme which is specified. # # @see BillService # # class UsageService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "UsageService->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "usage" @version = "1.0" end # # Creates the scheme for level based charging. Level based charging is # suited for usage based charging. # # @param levelName # - The name of the scheme # @param levelPrice # - The price of the level scheme # @param levelCurrency # - Currency to be used for scheme # @param levelDescription # - Description of the scheme # # @returns Created Scheme # # @raise App42Exception # def create_level_charge(levelName, levelPrice, levelCurrency, levelDescription) puts "createLevelCharge Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(levelName, "Name"); util.throwExceptionIfNullOrBlank(levelPrice, "Price"); util.throwExceptionIfNullOrBlank(levelCurrency, "Currency"); util.throwExceptionIfNullOrBlank(levelDescription, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"level"=>{ "name" => levelName, "price" => levelPrice, "currency" => levelCurrency, "description" => levelDescription }}}}}.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 query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/level" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Gets the information for the scheme. This method can be used by the app # developers to show the pricing plans to their users. # # @param levelName # - The Name of scheme # # @returns Scheme Information # # @raise App42Exception # def get_level(levelName) puts "getLicense " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(levelName, "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("name", levelName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/level/#{levelName}" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Remove a particular scheme. Note: A level is not physically deleted from # the storage. Only the state is changed so that it is available to fetch # older information. # # @param levelName # - The name of scheme # # @returns Scheme Information which has been removed # # @raise App42Exception # def remove_level(levelName) puts "remove Level Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(levelName, "levelName"); 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("name", levelName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/level/#{levelName}" response = connection.delete(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Creates the scheme for one time based charging. One Time based charging # is suited for downloads. e.g. app, Images, Music, Video, software etc. # downloads. # # @param oneTimeName # - The name of the scheme # @param oneTimePrice # - The price of the level scheme # @param oneTimeCurrency # - Currency to be used for scheme # @param oneTimeDescription # - Description of the scheme # # @returns Created Scheme # # @raise App42Exception # def create_one_time_charge(oneTimeName, oneTimePrice, oneTimeCurrency, oneTimeDescription) puts "create One Time Charge Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(oneTimeName, "Name"); util.throwExceptionIfNullOrBlank(oneTimePrice, "Price"); util.throwExceptionIfNullOrBlank(oneTimeCurrency, "Currency"); util.throwExceptionIfNullOrBlank(oneTimeDescription, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"oneTime"=>{ "name" => oneTimeName, "price" => oneTimePrice, "currency" => oneTimeCurrency, "description" => oneTimeDescription }}}}}.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}/oneTime" puts " =============resource_url================== #{resource_url}" response = connection.post(signature, resource_url, query_params, body) puts " =============response================== #{response}" usageObj = UsageResponseBuilder.new().buildResponse(response) puts " =============usageObj================== #{usageObj}" rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Gets the information for the scheme. This method can be used by the app # developer to show the pricing plans to their users. # # @param oneTimeName # - The name of scheme # # @returns Scheme Information # # @raise App42Exception # def get_one_time(oneTimeName) puts "getOneTime " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(oneTimeName, "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("name", oneTimeName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/oneTime/#{oneTimeName}" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Removes a particular scheme. Note: A level is not physically deleted from # the storage. Only the state is changed so that it is available to fetch # older information. # # @param oneTimeName # - The name of scheme to be removed # # @returns Scheme Information which has been removed # # @raise App42Exception # def remove_one_time(oneTimeName) puts "remove OneTime Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(oneTimeName, "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("name", oneTimeName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/oneTime/#{oneTimeName}" response = connection.delete(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Creates the scheme for feature based charging. Feature based charging is # suited for Software Applications. E.g. Within mobile, desktop, SaaS based # charging based on features. One can charge based on number of features # one uses. # # @param featureName # - The name of the scheme # @param featurePrice # - The price of the scheme # @param featureCurrency # - Currency to be used for that scheme # @param featureDescription # - Description of the scheme # # @returns Created Scheme # # @raise App42Exception # def create_feature_charge(featureName, featurePrice, featureCurrency, featureDescription) puts "create Feature Charge Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(featureName, "Name"); util.throwExceptionIfNullOrBlank(featurePrice, "Price"); util.throwExceptionIfNullOrBlank(featureCurrency, "Currency"); util.throwExceptionIfNullOrBlank(featureDescription, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) puts "Debug 1" util = Util.new body = {'app42' => {"appTab"=> {"usage"=>{"feature"=>{ "name" => featureName, "price" => featurePrice, "currency" => featureCurrency, "description" => featureDescription } }}}}.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}/feature" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Gets the information for the scheme. This method can be used by the app # developer to show his pricing plans to their users. # # @param featureName # - The name of scheme # # @returns Returns Scheme # # @raise App42Exception # def get_feature(featureName) puts "featureName " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(featureName, "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("name", featureName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/feature/#{featureName}" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Remove a particular scheme. Note: A level is not physically deleted from # the storage. Only the state is changed so that it is available to fetch # older information. # # @param featureName # - The name of scheme which has to be removed # # @returns Scheme Information which has been removed # # @raise App42Exception # def remove_feature(featureName) puts "remove feature Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(featureName, "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("name", featureName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/feature/#{featureName}" response = connection.delete(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Creates the scheme for bandwidth based charging. It is best suited for # network based bandwidth usage. # # @param bandwidthName # - name of the scheme # @param bandwidthUsage # - bandwidth usage for the scheme # @param usageBandWidth # - bandwidth unit for the scheme # @param bandwidthPrice # - The price of the level scheme # @param bandwidthCurrency # - Currency to be used for the scheme # @param bandwidthDescription # - Description of the scheme # # @returns Created Scheme # # @raise App42Exception # def create_bandwidth_charge(bandwidthName, bandwidthUsage, usageBandWidth, bandwidthPrice, bandwidthCurrency, bandwidthDescription) puts "create Bandwidth Charge Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(bandwidthName, "Name"); util.throwExceptionIfNullOrBlank(bandwidthUsage, "BandWidth"); util.throwExceptionIfNullOrBlank(usageBandWidth, "usageBandWidth"); util.throwExceptionIfNullOrBlank(bandwidthPrice, "Price"); util.throwExceptionIfNullOrBlank(bandwidthCurrency, "Currency"); util.throwExceptionIfNullOrBlank(bandwidthDescription, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"bandwidth"=>{ "name" => bandwidthName, "price" => bandwidthPrice, "bandwidth" => bandwidthUsage, "unit" => usageBandWidth, "currency" => bandwidthCurrency, "description" => bandwidthDescription } }}}}.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}/bandwidth" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Gets the information for the scheme. This method can be used by the app # developers to show the pricing plans to their users. # # @param bandwidthName # - The name of scheme # # @returns Scheme Information # # @raise App42Exception # def get_bandwidth(bandwidthName) puts "featureName " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(bandwidthName, "BandWidthName"); 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("name", bandwidthName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/bandwidth/#{bandwidthName}" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Remove a particular scheme. Note: A level is not physically deleted from # the storage. Only the state is changed so that it is available to fetch # older information. # # @param bandwidthName # - The name of the scheme to be removed # # @returns Scheme Information which has been removed # # @raise App42Exception # def remove_bandwidth(bandwidthName) puts "remove bandwidth Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(bandwidthName, "BandWidthName"); 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("name", bandwidthName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/bandwidth/#{bandwidthName}" response = connection.delete(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Creates the scheme for storage based charging. It is best suited for disk # based storage usage. E.g. photo Storage, file Storage, RAM usage, # Secondary Storage. # # @param storageName # - The name of the scheme # @param storageSpace # - storage space for the scheme # @param usageStorage # - Storage units to be used for the scheme # @param storagePrice # - The price of the scheme # @param storageCurrency # - Currency to be used for that scheme # @param storageDescription # - Description of the scheme # # @returns Created Scheme # # @raise App42Exception # def create_storage_charge(storageName, storageSpace, usageStorage, storagePrice, storageCurrency, storageDescription) puts "create Storage Charge Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(storageName, "Name"); util.throwExceptionIfNullOrBlank(storageSpace, "Space"); util.throwExceptionIfNullOrBlank(usageStorage, "usageStorage"); util.throwExceptionIfNullOrBlank(storagePrice, "Price"); util.throwExceptionIfNullOrBlank(storageCurrency, "Currency"); util.throwExceptionIfNullOrBlank(storageDescription, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"storage"=>{ "name" => storageName, "price" => storagePrice, "space" => storageSpace, "unit" => usageStorage, "currency" => storageCurrency, "description" => storageDescription } }}}}.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}/storage" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Gets the information for the scheme. This method can be used by the App # developer to show his pricing plans to their users. # # @param storageName # - The name of scheme # # @returns Scheme Information # # @raise App42Exception # def get_storage(storageName) puts "featureName " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(storageName, "StorageName"); 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("name", storageName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/storage/#{storageName}" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Remove a particular scheme. Note: A level is not physically deleted from # the storage. Only the state is changed so that it is available to fetch # older information. # # @param storageName # - The name of scheme # # @returns Scheme Information which has been removed # # @raise App42Exception # def remove_storage(storageName) puts "remove feature Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(storageName, "StorageName"); 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("name", storageName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/storage/#{storageName}" response = connection.delete(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Creates the scheme for time based charging. It is best suited for # applications which want to charge based on time usage or elapsed. E.g. # How long one is listening to music or watching a video. How long the # person is reading a online book or magazine etc. # # @param timeName # - The name of the scheme # @param timeUsage # - usage time for the scheme # @param usageTime # - unit of time for the scheme # @param timePrice # - The price of the level scheme # @param timeCurrency # - Currency used for the scheme # @param timeDescription # - Description of the scheme # # @returns Created Scheme # # @raise App42Exception # def create_time_charge(timeName, timeUsage, usageTime, timePrice, timeCurrency, timeDescription) puts "create Time Charge Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(timeName, "Name"); util.throwExceptionIfNullOrBlank(timeUsage, "Time"); util.throwExceptionIfNullOrBlank(usageTime, "usageTime"); util.throwExceptionIfNullOrBlank(timePrice, "Price"); util.throwExceptionIfNullOrBlank(timeCurrency, "Currency"); util.throwExceptionIfNullOrBlank(timeDescription, "Description"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"time"=>{ "name" => timeName, "price" => timePrice, "time" => timeUsage, "unit" => usageTime, "currency" => timeCurrency, "description" => timeDescription }}}}}.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}/time" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Gets the information for the scheme based on timeName. This method can be # used by the app developers to show his pricing plans to their users. # # @param timeName # - The name of scheme # # @returns Scheme Information # # @raise App42Exception # def get_time(timeName) puts "timeName " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(timeName, "timeName"); 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("name", timeName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/time/#{timeName}" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Remove a particular scheme based on timeName. Note: A level is not # physically deleted from the storage. Only the state is changed so that it # is available to fetch older information. # # @param timeName # - The name of scheme # # @returns Scheme Information which has been removed # # @raise App42Exception # def remove_time(timeName) puts "remove time Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(timeName, "timeName"); 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("name", timeName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/time/#{timeName}" response = connection.delete(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Charge on a particular scheme. A Charging record is created whenever this # method is called. Which is used for billing and usage behaviour analysis # purpose. # # @param chargeUser # - The user against whom the charging has to be done # @param levelName # - The name of scheme # # @returns Returns charging information # # @raise App42Exception # def charge_level(chargeUser, levelName) puts "create Time Charge Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(chargeUser, "User"); util.throwExceptionIfNullOrBlank(levelName, "Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"charge"=>{"level"=>{ "name" => levelName, "user" => chargeUser }}}}}}.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}/charge/level" response = connection.post(signature, resource_url, query_params, body) puts "------------ i am here #{response}" usageObj = UsageResponseBuilder.new().buildResponse(response) puts "------------ i am here also but error are killing me #{usageObj}" rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Charge on a particular scheme. A Charging record is created whenever this # method is called. Which is used for billing and usage behaviour analysis # purpose. # # @param chargeUser # - The user against whom the charging has to be done # @param oneTimeName # - The name of scheme # # @returns Returns charging information # def charge_one_time(chargeUser, oneTimeName) puts "chargeOneTimee Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(chargeUser, "User"); util.throwExceptionIfNullOrBlank(oneTimeName, "Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"charge"=>{"oneTime"=>{ "user" => chargeUser, "name" => oneTimeName }}}}}}.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}/charge/oneTime" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Charge on a particular scheme. A Charging record is created whenever this # method is called. Which is used for billing and usage behaviour analysis # purpose. # # @param chargeUser # - The user against whom the charging has to be done # @param featureName # - The name of scheme # # @returns Returns charging information # # @raise App42Exception # def charge_feature(chargeUser, featureName) puts "chargeFeature Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(chargeUser, "User"); util.throwExceptionIfNullOrBlank(featureName, "Name"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"charge"=>{"feature"=>{ "user" => chargeUser, "name" => featureName }}}}}}.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}/charge/feature" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Charge on a particular scheme. A Charging record is created whenever this # method is called. Which is used for billing and usage behaviour analysis # purpose. # # @param chargeUser # - The user against whom the charging has to be done # @param bandwidthName # - The name of scheme # @param bandwidth # - bandwidth for which the charging has to be done # @param usageBandWidth # - unit of bandwidth charging # # @returns Returns charging information # # @raise App42Exception # def charge_bandwidth(chargeUser, bandwidthName, bandwidth, usageBandWidth) puts "chargeBandwidth Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(chargeUser, "Uesr"); util.throwExceptionIfNullOrBlank(bandwidthName, "Name"); util.throwExceptionIfNullOrBlank(bandwidth, " BandWidth"); util.throwExceptionIfNullOrBlank(usageBandWidth, "usageBandWidth"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"charge"=>{"bandwidth"=>{ "user" => chargeUser, "name" => bandwidthName, "bandwidth" => bandwidth, "unit" => usageBandWidth }}}}}}.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}/charge/bandwidth" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Charge on a particular scheme. A Charging record is created whenever this # method is called. Which is used for billing and usage behaviour analysis # purpose. # # @param chargeUser # - The user against whom the charging has to be done # @param storageName # - The name of scheme # @param storageSpace # - storage for which the charging has to be done # @param usageStorage # - unit of storage charging # # @returns Returns charging information # # @raise App42Exception # def charge_storage(chargeUser, storageName, storageSpace, usageStorage) puts "charge Storage Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(chargeUser, "User"); util.throwExceptionIfNullOrBlank(storageName, "StorageName"); util.throwExceptionIfNullOrBlank(storageSpace, "Space"); util.throwExceptionIfNullOrBlank(usageStorage, "usageStorage"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"charge"=>{"storage"=>{ "user" => chargeUser, "name" => storageName, "space" => storageSpace, "unit" => usageStorage }}}}}}.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}/charge/storage" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Charge on a particular scheme. A Charging record is created whenever this # method is called. Which is used for billing and usage behaviour analysis # purpose. # # @param chargeUser # - The user against whom the charging has to be done # @param timeName # - The name of scheme # @param chargetime # - time for which the charging has to be done # @param usageTime # - unit of time charging # # @returns Returns charging information # # @raise App42Exception # def charge_time(chargeUser, timeName, chargetime, usageTime) puts "charge Time Called " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new util.throwExceptionIfNullOrBlank(chargeUser, "User"); util.throwExceptionIfNullOrBlank(timeName, "Name"); util.throwExceptionIfNullOrBlank(chargetime, "Time"); util.throwExceptionIfNullOrBlank(usageTime, "usageTime"); begin connection = App42::Connection::RESTConnection.new(@base_url) body = {'app42' => {"appTab"=> {"usage"=>{"charge"=>{"time"=>{ "user" => chargeUser, "name" => timeName, "time" => chargetime, "unit" => usageTime }}}}}}.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}/charge/time" response = connection.post(signature, resource_url, query_params, body) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Returns all the schemes for this usage type. This can be used by the app # developers to display their usage based pricing plan. # # @raise App42Exception # def get_all_level_usage() puts "getAllLevelUsage " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new 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 query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/level" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Returns all the schemes for this usage type. This can be used by the app # developers to display their usage based pricing plan. # # @raise App42Exception # def get_all_one_time_usage() puts "getAllOneTimeUsage " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new 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 query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/oneTime" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Returns all the schemes for this usage type. This can be used by the app # developers to display their usage based pricing plan. # # @raise App42Exception # def get_all_feature_usage() puts "getAllFeatureUsage " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new 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 puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/feature" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Returns all the schemes for this usage type. This can be used by the app # developers to display their usage based pricing plan. # # @raise App42Exception # def get_all_bandwidth_usage() puts "getAllBandwidthUsage " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new 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 query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/bandwidth" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Returns all the schemes for this usage type. This can be used by the app # developers to display their usage based pricing plan. # # @raise App42Exception # def get_all_storage_usage() puts "getAllStorageUsage " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new 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 query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/storage" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Returns all the schemes for this usage type. This can be used by the app # developers to display their usage based pricing plan. # # @raise App42Exception # def get_all_time_usage() puts "getAllTimeUsage " puts "Base url #{@base_url}" response = nil usageObj = nil usageObj = Usage.new util = Util.new 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 query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/time" response = connection.get(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Creates the scheme for custom based charging. It is best suited for # applications which want to charge based on custom parameters. # # @param customName # - The name of the scheme # @param customType # - usage time for the scheme # @param customUnit # - unit of time for the scheme # @param customPrice # - The price of the level scheme # @param customCurrency # - Currency used for the scheme # @param customDescription # - Description of the scheme # # @returns Created Scheme # # @raise App42Exception # # def create_custom_charge(customName, customType, customUnit, customPrice, customCurrency, customDescription) # puts "create Custom Charge Called " # puts "Base url #{@base_url}" # response = nil # usageObj = Usage.new # usageObj = nil # util = Util.new # util.throwExceptionIfNullOrBlank(customName, "Name"); # util.throwExceptionIfNullOrBlank(customType, "Type"); # util.throwExceptionIfNullOrBlank(customUnit, "Unit"); # util.throwExceptionIfNullOrBlank(customPrice, "Price"); # util.throwExceptionIfNullOrBlank(customCurrency, "Currency"); # util.throwExceptionIfNullOrBlank(customDescription, "Description"); # begin # connection = App42::Connection::RESTConnection.new(@base_url) # body = {'app42' => {"appTab"=> {"usage"=>{"custom"=>{ # "name" => customName, # "price" => customPrice, # "type" => customType, # "unit" => customUnit, # "currency" => customCurrency, # "description" => customDescription # }}}}}.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}/custom" # response = connection.post(signature, resource_url, query_params, body) # usageObj = UsageResponseBuilder.new().buildResponse(response) # rescue App42Exception =>e # raise e # rescue Exception => e # raise App42Exception.new(e) # end # return usageObj # end # # Gets the information for the scheme. This method can be used by the app # developers to show his pricing plans to their users. # # @param customName # - The name of scheme # # @returns Scheme Information # # @raise App42Exception # # def get_custom(customName) # puts "getCustomCalled " # puts "Base url #{@base_url}" # response = nil # usageObj = Usage.new # usageObj = nil # util = Util.new # util.throwExceptionIfNullOrBlank(customName, "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 # params.store("name", customName); # puts query_params # signature = util.sign(@secret_key, params) # resource_url = "#{@version}/#{@resource}/custom/#{customName}" # response = connection.get(signature, resource_url, query_params) # usageObj = UsageResponseBuilder.new().buildResponse(response) # rescue App42Exception =>e # raise e # rescue Exception => e # raise App42Exception.new(e) # end # return usageObj # end # # Removes a particular scheme. Note: A Custom charge is not physically # deleted from the storage. Only the state is changed so that it is # available to fetch older information. # # @param customName # - The name of scheme # # @returns Scheme Information which has been removed # # @raise App42Exception # def remove_custom(customName) puts "remove Custom Called " puts "Base url #{@base_url}" response = nil usageObj = Usage.new usageObj = nil util = Util.new util.throwExceptionIfNullOrBlank(customName, "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("name", customName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/custom/#{customName}" response = connection.delete(signature, resource_url, query_params) usageObj = UsageResponseBuilder.new().buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return usageObj end # # Charge on a particular scheme. A Charging record is created whenever this # method is called. Which is used for billing and usage behaviour analysis # purpose. # # @param chargeUser # - The user against whom the charging has to be done # @param customName # - The name of scheme # @param customType # - unit of custom charging # @param customValue # - value for which the charging has to be done # # @returns Returns charging information # # @raise App42Exception # # def charge_custom(chargeUser, customName, customValue, customType) # puts "charge custom Called " # puts "Base url #{@base_url}" # response = nil # usageObj = Usage.new # usageObj = nil # util = Util.new # Util.throwExceptionIfNullOrBlank(chargeUser, "User"); # util.throwExceptionIfNullOrBlank(customName, "Name"); # util.throwExceptionIfNullOrBlank(customType, "Type"); # util.throwExceptionIfNullOrBlank(customValue, "Value"); # begin # connection = App42::Connection::RESTConnection.new(@base_url) # body = {'app42' => {"appTab"=> {"usage"=>{"charge"=>{"custom"=>{ # "user" => chargeUser, # "name" => customName, # "type" => customType, # "value" => customValue # }}}}}}.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}/charge/custom" # response = connection.post(signature, resource_url, query_params, body) # usageObj = UsageResponseBuilder.new().buildResponse(response) # rescue App42Exception =>e # raise e # rescue Exception => e # raise App42Exception.new(e) # end # return usageObj # end # # Returns all the schemes for custom usage type. This can be used by the # app developers to display their usage based pricing plan. # # @raise App42Exception # # def get_all_custom_usage() # puts "getAllCustomUsage called" # puts "Base url #{@base_url}" # response = nil # usageObj = Usage.new # usageObj = nil # util = Util.new # 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 # puts query_params # signature = util.sign(@secret_key, params) # resource_url = "#{@version}/#{@resource}/custom" # response = connection.get(signature, resource_url, query_params) # usageObj = UsageResponseBuilder.new().buildResponse(response) # rescue App42Exception =>e # raise e # rescue Exception => e # raise App42Exception.new(e) # end # return usageObj # end end end end