Scala SDK Samples - Files Operations
Upload Files
package com.zoho.crm.sample.file
import java.io.{File, FileOutputStream, InputStream, OutputStream}
import java.util
import com.zoho.crm.api.ParameterMap
import com.zoho.crm.api.file.APIException
import com.zoho.crm.api.file.ActionHandler
import com.zoho.crm.api.file.ActionResponse
import com.zoho.crm.api.file.ActionWrapper
import com.zoho.crm.api.file.BodyWrapper
import com.zoho.crm.api.file.FileBodyWrapper
import com.zoho.crm.api.file.FileOperations
import com.zoho.crm.api.file.ResponseHandler
import com.zoho.crm.api.file.SuccessResponse
import com.zoho.crm.api.file.FileOperations.GetFileParam
import com.zoho.crm.api.util.APIResponse
import com.zoho.crm.api.util.Model
import com.zoho.crm.api.util.StreamWrapper
import scala.collection.mutable.ArrayBuffer
object Files {
/**
* Upload File
* This method is used to upload a file and print the response.
*
* @throws Exception
*/
@throws[Exception]
def uploadFile(): Unit = { //Get instance of RecordOperations Class
val fileOperations = new FileOperations
val bodyWrapper = new BodyWrapper
//Get instance of StreamWrapper class that takes absolute path of the file to be attached as parameter
val streamWrapper = new StreamWrapper("/Users/abc-123/Documents/file/download.png")
val streamWrapper1 = new StreamWrapper("/Users/abc-123/Documents/file/download.png")
var file = new ArrayBuffer[StreamWrapper]
file+=(streamWrapper, streamWrapper1)
bodyWrapper.setFile(file)
val paramInstance = new ParameterMap
//Call uploadFile method that takes BodyWrapper instance as parameter.
val responseOption = fileOperations.uploadFiles(bodyWrapper, Option(paramInstance))
if (responseOption.isDefined) { //check response
var response= responseOption.get
println("Status Code: " + response.getStatusCode)
//Check if expected response is received
if (response.isExpected) { //Get object from response
val actionHandler = response.getObject
if (actionHandler.isInstanceOf[ActionWrapper]) { //Get the received ActionWrapper instance
val actionWrapper = actionHandler.asInstanceOf[ActionWrapper]
//Get the list of obtained action responses
val actionResponses = actionWrapper.getData
for (actionResponse <- actionResponses) { //Check if the request is successful
if (actionResponse.isInstanceOf[SuccessResponse]) { //Get the received SuccessResponse instance
val successResponse = actionResponse.asInstanceOf[SuccessResponse]
//Get the Status
println("Status: " + successResponse.getStatus.getValue)
//Get the Code
println("Code: " + successResponse.getCode.getValue)
println("Details: ")
//Get the details map
successResponse.getDetails.foreach(entry=>{
println(entry._1 + ": " + entry._2)
})
//Get the Message
println("Message: " + successResponse.getMessage.getValue)
}
else { //Check if the request returned an exception
if (actionResponse.isInstanceOf[APIException]) { //Get the received APIException instance
val exception = actionResponse.asInstanceOf[APIException]
println("Status: " + exception.getStatus.getValue)
println("Code: " + exception.getCode.getValue)
println("Details: ")
exception.getDetails.foreach(entry=>{
println(entry._1 + ": " + entry._2)
})
println("Message: " + exception.getMessage.getValue)
}
}
}
}
else if (actionHandler.isInstanceOf[APIException]) {
val exception = actionHandler.asInstanceOf[APIException]
println("Status: " + exception.getStatus.getValue)
println("Code: " + exception.getCode.getValue)
println("Details: ")
exception.getDetails.foreach(entry=>{
println(entry._1 + ": " + entry._2)
})
println("Message: " + exception.getMessage.getValue)
}
}
else { //If response is not as expected
//Get model object from response
val responseObject = response.getModel
//Get the response object's class
val clas = responseObject.getClass
//Get all declared fields of the response class
val fields = clas.getDeclaredFields
for (field <- fields) { //Get each value
println(field.getName + ":" + field.get(responseObject))
}
}
}
}
}
Get File