Skip to main content

Scala SDK Samples - Files Operations

Upload Files
Get File
              
              
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 {
  /**
   *  Get File
   *
   * @param id                - The ID of the uploaded File.
   * @param destinationFolder - The absolute path of the destination folder to store the File
   * @throws Exception
   */
  @throws[Exception]
  def getFile(id: String, destinationFolder: String): Unit = { //example
    //String id = "ae9c7cefa418aec1d6a5cc2d54b7563c0dd42b"
    //String destinationFolder = "/Users/user_name/Desktop"
    //Get instance of FileOperations Class
    val fileOperations = new FileOperations
    //Get instance of ParameterMap Class
    val paramInstance = new ParameterMap
    paramInstance.add(new GetFileParam().id, id)
    //Call getFile method that takes paramInstance as parameters
    val responseOption = fileOperations.getFile(Option(paramInstance))
    if (responseOption.isDefined) {
      val response = responseOption.get
      println("Status Code: " + response.getStatusCode)
      if (util.Arrays.asList(204, 304).contains(response.getStatusCode)) {
        println(if (response.getStatusCode == 204) "No Content"
        else "Not Modified")
        return
      }
      if (response.isExpected) {
        val responseHandler = response.getObject
        if (responseHandler.isInstanceOf[FileBodyWrapper]) {
          val fileBodyWrapper = responseHandler.asInstanceOf[FileBodyWrapper]
          //Get StreamWrapper instance from the returned FileBodyWrapper instance
          val streamWrapper = fileBodyWrapper.getFile.get
          //Create a file instance with the absolute_file_path
          val file = new File(destinationFolder + java.io.File.separatorChar + streamWrapper.getName.get)
          //Get InputStream from the response
          val is = streamWrapper.getStream.get
          //Create an OutputStream for the destination file
          val os = new FileOutputStream(file)
          val buffer = new Array[Byte](1024)
          var bytesRead:Integer = 0
          //read the InputStream till the end
          while ( {
            bytesRead = is.read(buffer)
            bytesRead != -1
          }) { //write data to OutputStream
            os.write(buffer, 0, bytesRead)
          }
          //Close the InputStream
          is.close()
          //Flush and close the OutputStream
          os.flush()
          os.close()
        }
        else if (responseHandler.isInstanceOf[APIException]) {
          val exception = responseHandler.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.getStatusCode != 204) {
        val responseObject = response.getModel
        val clas = responseObject.getClass
        val fields = clas.getDeclaredFields
        for (field <- fields) {
          println(field.getName + ":" + field.get(responseObject))
        }
      }
    }
  }
}