# ----------------------------------------------------------------------- # Copyright © 2012 ShepHertz Technologies Pvt Ltd. All rights reserved. # ----------------------------------------------------------------------- require 'rubygems' require 'connection/RESTConnection.rb' require 'util/util.rb' require 'json/pure' require 'App42Response.rb' require 'gallery/AlbumResponseBuilder.rb' require 'gallery/Album.rb' module App42 module Gallery # # Adds Photo to the created Album on the Cloud # All photos for a given Album can be managed through this service. Photos can be uploaded # to the cloud. Uploaded photos are accessible through the generated URL. # The service also creates a thumbnail for the Photo which has been uploaded. # # @see Album # @see Photo # class PhotoService # # this is a constructor that takes # # @param apiKey # @param secretKey # @param baseURL # def initialize(api_key, secret_key, base_url) puts "Photo Gallery->initialize" @api_key = api_key @secret_key = secret_key @base_url = base_url @resource = "gallery" @version = "1.0" end # # Adds Photo for a particular user and album. The Photo is uploaded on the cloud # # @param userName # - Name of the User whose photo has to be added # @param albumName # - Name of the Album in which photo has to be added # @param photoName # - Name of the Photo that has to be added # @param photoDescription # - Description of the Photo that has to be added # @param path # - Path from where Photo has to be picked for addition # # @return Album object containing the Photo which has been added # # @raise App42Exception # def add_photo(userName, albumName, photoName, photoDescription, path) puts "Add photo called " puts "Base url #{@base_url}" response = nil; photoObj = nil; photoObj = Album.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album Name"); util.throwExceptionIfNullOrBlank(photoName, "Photo Name"); util.throwExceptionIfNullOrBlank(photoDescription, "Description"); util.throwExceptionIfNullOrBlank(path, "Path"); if (FileTest.exists?(path) == false) raise App42Exception.new("The file with the name #{path} not found") end begin connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } params = query_params.clone post_params = Hash.new post_params.store("userName", userName); post_params.store("albumName", albumName); post_params.store("name", photoName); post_params.store("description", photoDescription); params = params.merge(post_params) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}" response = connection.photoMultipart(signature, resource_url, query_params, params, path) puts "Response is #{response}" album = AlbumResponseBuilder.new() photoObj = album.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return photoObj end # # Adds Photo for a particular user and album via Stream. The Photo is uploaded on the cloud # # @param userName # - Name of the User whose photo has to be added # @param albumName # - Name of the Album in which photo has to be added # @param photoName # - Name of the Photo that has to be added # @param photoDescription # - Description of the Photo that has to be added # @param spath # - Input Stream for the Photo that has to be added # # @return Album object containing the Photo which has been added # # @raise App42Exception # def add_photo_with_stream(userName, albumName, photoName, photoDescription, spath) puts "Add photo with Stream called " puts "Base url #{@base_url}" response = nil; photoObj = nil; photoObj = Album.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album Name"); util.throwExceptionIfNullOrBlank(photoName, "Photo Name"); util.throwExceptionIfNullOrBlank(photoDescription, "Description"); util.throwExceptionIfNullOrBlank(spath, "Path"); if (FileTest.exists?(spath) == false) raise App42Exception.new("The file with the name #{spath} not found") end begin connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } params = query_params.clone post_params = Hash.new post_params.store("userName", userName); post_params.store("albumName", albumName); post_params.store("name", photoName); post_params.store("description", photoDescription); params = params.merge(post_params) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}" response = connection.photoMultipartStream(signature, resource_url, query_params, params, spath) puts "Response is #{response}" album = AlbumResponseBuilder.new() photoObj = album.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return photoObj end # # Fetch all the Photos based on the userName # # @param userName # - Name of the User whose photos have to be fetched # # @return ArrayList of Album object containing all the Photos for the given userName # # @raise App42Exception # def get_photos(userName) puts "Get Photos Called " puts "Base url #{@base_url}" response = nil; albumList = nil; albumList = Array.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User 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("userName", userName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}" response = connection.get(signature, resource_url, query_params) photo = AlbumResponseBuilder.new() albumList = photo.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return albumList end # # Fetch all Photos based on the userName and album name # # @param userName # - Name of the User whose photos have to be fetched # @param albumName # - Name of the Album from which photos have to be fetched # # @return Album object containing all the Photos for the given userName and albumName # # @raise App42Exception # def get_photos_by_album_name(userName, albumName) puts "Get Photos Called " puts "Base url #{@base_url}" response = nil; photoObj = nil; photoObj = Album.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album 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("userName", userName) params.store("albumName", albumName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}/#{albumName}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" photo = AlbumResponseBuilder.new() photoObj = photo.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return photoObj end # # Fetch all Photos based on the userName and album name by paging. # # @param userName # - Name of the User whose photos have to be fetched # @param albumName # - Name of the Album from which photos have to be fetched # @param max # - Maximum number of records to be fetched # @param offset # - From where the records are to be fetched # # @return Album object containing all the Photos for the given userName and # albumName # # @raise App42Exception # def get_photos_by_album_name_by_paging(userName, albumName, max, offset) puts "getPhotosByAlbumNameByPaging Called " puts "Base url #{@base_url}" response = nil; photoObj = nil; photoObj = Album.new util = Util.new util.validateMax(max); util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album Name"); util.throwExceptionIfNullOrBlank(max, "Max"); util.throwExceptionIfNullOrBlank(offset, "Offset"); 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("userName", userName) params.store("albumName", albumName) params.store("max", "" + (max.to_i).to_s); params.store("offset", "" + (offset.to_i).to_s); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/album/#{userName}/#{albumName}/paging/#{(max.to_i).to_s}/#{(offset.to_i).to_s}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" photo = AlbumResponseBuilder.new() photoObj = photo.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return photoObj end # # Fetch the particular photo based on the userName, album name and photo name # # @param userName # - Name of the User whose photo has to be fetched # @param albumName # - Name of the Album from which photo has to be fetched # @param photoName # - Name of the Photo that has to be fetched # # @return Album object containing the Photo for the given userName, albumName and photoName # # @raise App42Exception # def get_photos_by_album_and_photo_name(userName, albumName, photoName) puts "Get Photos Called " puts "Base url #{@base_url}" response = nil; photoObj = nil; photoObj = Album.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album Name"); util.throwExceptionIfNullOrBlank(photoName, "Photo 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("userName", userName) params.store("albumName", albumName) params.store("name", photoName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}/#{albumName}/#{photoName}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" photo = AlbumResponseBuilder.new() photoObj = photo.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return photoObj end # # Removes the particular Photo from the specified Album for a particular user. # Note: The Photo is removed from the cloud and wont be accessible in future # # @param userName # - Name of the User whose photo has to be removed # @param albumName # - Name of the Album in which photo has to be removed # @param photoName # - Name of the Photo that has to be removed # # @return App42Response if removed successfully # # @raise App42Exception # def remove_photo(userName, albumName, photoName) puts "Delete Photo Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new(); util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album Name"); util.throwExceptionIfNullOrBlank(photoName, "Photo 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("userName", userName) params.store("albumName", albumName) params.store("name", photoName) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}/#{albumName}/#{photoName}" response = connection.delete(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Fetch the count of all Photos based on the userName and album name # # @param userName # - Name of the User whose count of photos have to be fetched # @param albumName # - Name of the Album from which count of photos have to be fetched # # @return App42Response object containing the count of all the Photos for the given userName and # albumName # # @raise App42Exception # def get_photos_count_by_album_name(userName, albumName) puts "getPhotosCountByAlbumName Called " puts "Base url #{@base_url}" response = nil; responseObj = App42Response.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album 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("userName", userName); params.store("albumName", albumName); puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/#{userName}/#{albumName}/count" response = connection.get(signature, resource_url, query_params) responseObj.strResponse=(response) responseObj.isResponseSuccess=(true) responseObj = AlbumResponseBuilder.new() responseObj.getTotalRecords(response); rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return responseObj end # # Adds tag to the Photo of the user in the album. # # @param userName # - Name of the User whose name has to be tagged to photo # @param albumName # - Album name whose photo is to be tagged # @param photoName # - Photo name to be tagged # @param tagList # - list of tages in Photo # # @return Album object containing the Photo which has been added # # @raise App42Exception # def add_tag_to_photo(userName, albumName, photoName, tagList) puts "addTagToPhoto called " puts "Base url #{@base_url}" response = nil; photoObj = nil; photoObj = Album.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(albumName, "Album Name"); util.throwExceptionIfNullOrBlank(photoName, "Photo Name"); util.throwExceptionIfNullOrBlank(tagList, "TagList"); if tagList.size() == 0 raise App42Exception.new("TagList cannot be empty. Please add the name to be tagged"); end begin connection = App42::Connection::RESTConnection.new(@base_url) tagArray = Array.new() for tag in tagList do tagArray.push(tag) end body = {'app42' => {"photo"=> { "userName" => userName, "albumName" => albumName, "photoName" => photoName, "tags" => { "tag" => tagArray }}}}.to_json puts "Body #{body}" params = Hash.new query_params = Hash.new query_params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } params = query_params.clone post_params = Hash.new post_params.store("body", body); params = params.merge(post_params) signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/tag" response = connection.post(signature, resource_url, query_params, body) puts "Response is #{response}" album = AlbumResponseBuilder.new() photoObj = album.buildResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return photoObj end # # Fetch all the Photos based on the userName and tag # # @param userName # - Name of the User whose photos have to be fetched # @param tag # - tag on which basis photos have to be fetched # # @return ArrayList of Album object containing all the Photos for the given # userName # # @raise App42Exception # def get_tagged_photos(userName, tag) puts "getTaggedPhoto called " puts "Base url #{@base_url}" response = nil; albumList = nil; albumList = Array.new util = Util.new util.throwExceptionIfNullOrBlank(userName, "User Name"); util.throwExceptionIfNullOrBlank(tag, "Tag"); begin connection = App42::Connection::RESTConnection.new(@base_url) params = Hash.new query_params = Hash.new params = { 'apiKey'=> @api_key, 'version' => @version, 'timeStamp' => util.get_timestamp_utc, } query_params = params.clone params.store("userName", userName); params.store("tag", tag) puts query_params signature = util.sign(@secret_key, params) resource_url = "#{@version}/#{@resource}/tag/#{tag}/userName/#{userName}" response = connection.get(signature, resource_url, query_params) puts "Response is #{response}" album = AlbumResponseBuilder.new() albumList = album.buildArrayResponse(response) rescue App42Exception =>e raise e rescue Exception => e raise App42Exception.new(e) end return albumList end end end end