Ejemplo n.º 1
0
        /// <summary>
        /// This method is used to upload link attachment to a single record of a module with ID and print the response.
        /// </summary>
        /// <param name="moduleAPIName">The API Name of the record's module</param>
        /// <param name="recordId">The ID of the record to upload Link attachment</param>
        /// <param name="attachmentURL">The attachmentURL of the doc or image link to be attached</param>
        public static void UploadLinkAttachments(string moduleAPIName, long recordId, string attachmentURL)
        {
            //example
            //string moduleAPIName = "Leads";
            //long recordId = 3477002;
            //string attachmentURL = "https://5.imimg.com/data5/KJ/UP/MY-8655440/zoho-crm-500x500.png";

            //Get instance of AttachmentsOperations Class that takes moduleAPIName and recordId as parameter
            AttachmentsOperations attachmentsOperations = new AttachmentsOperations(moduleAPIName, recordId);

            //Get instance of ParameterMap Class
            ParameterMap paramInstance = new ParameterMap();

            paramInstance.Add(UploadLinkAttachmentParam.ATTACHMENTURL, attachmentURL);

            //Call UploadLinkAttachment method that takes paramInstance as parameter
            APIResponse <API.Attachments.ActionHandler> response = attachmentsOperations.UploadLinkAttachment(paramInstance);

            if (response != null)
            {
                //Get the status code from response
                Console.WriteLine("Status Code: " + response.StatusCode);

                //Check if expected response is received
                if (response.IsExpected)
                {
                    //Get object from response
                    API.Attachments.ActionHandler actionHandler = response.Object;

                    if (actionHandler is API.Attachments.ActionWrapper)
                    {
                        //Get the received ActionWrapper instance
                        API.Attachments.ActionWrapper actionWrapper = (API.Attachments.ActionWrapper)actionHandler;

                        //Get the list of obtained Action Response instances
                        List <API.Attachments.ActionResponse> actionResponses = actionWrapper.Data;

                        foreach (API.Attachments.ActionResponse actionResponse in actionResponses)
                        {
                            //Check if the request is successful
                            if (actionResponse is API.Attachments.SuccessResponse)
                            {
                                //Get the received SuccessResponse instance
                                API.Attachments.SuccessResponse successResponse = (API.Attachments.SuccessResponse)actionResponse;

                                //Get the Status
                                Console.WriteLine("Status: " + successResponse.Status.Value);

                                //Get the Code
                                Console.WriteLine("Code: " + successResponse.Code.Value);

                                Console.WriteLine("Details: ");

                                //Get the details map
                                foreach (KeyValuePair <string, object> entry in successResponse.Details)
                                {
                                    //Get each value in the map
                                    Console.WriteLine(entry.Key + " : " + JsonConvert.SerializeObject(entry.Value));
                                }

                                //Get the Message
                                Console.WriteLine("Message: " + successResponse.Message.Value);
                            }
                            //Check if the request returned an exception
                            else if (actionResponse is API.Attachments.APIException)
                            {
                                //Get the received APIException instance
                                API.Attachments.APIException exception = (API.Attachments.APIException)actionResponse;

                                //Get the Status
                                Console.WriteLine("Status: " + exception.Status.Value);

                                //Get the Code
                                Console.WriteLine("Code: " + exception.Code.Value);

                                Console.WriteLine("Details: ");

                                //Get the details map
                                foreach (KeyValuePair <string, object> entry in exception.Details)
                                {
                                    //Get each value in the map
                                    Console.WriteLine(entry.Key + " : " + JsonConvert.SerializeObject(entry.Value));
                                }

                                //Get the Message
                                Console.WriteLine("Message: " + exception.Message.Value);
                            }
                        }
                    }
                    //Check if the request returned an exception
                    else if (actionHandler is API.Attachments.APIException)
                    {
                        //Get the received APIException instance
                        API.Attachments.APIException exception = (API.Attachments.APIException)actionHandler;

                        //Get the Status
                        Console.WriteLine("Status: " + exception.Status.Value);

                        //Get the Code
                        Console.WriteLine("Code: " + exception.Code.Value);

                        Console.WriteLine("Details: ");

                        //Get the details map
                        foreach (KeyValuePair <string, object> entry in exception.Details)
                        {
                            //Get each value in the map
                            Console.WriteLine(entry.Key + " : " + JsonConvert.SerializeObject(entry.Value));
                        }

                        //Get the Message
                        Console.WriteLine("Message: " + exception.Message.Value);
                    }
                }
                else
                { //If response is not as expected
                    //Get model object from response
                    Model responseObject = response.Model;

                    //Get the response object's class
                    Type type = responseObject.GetType();

                    //Get all declared fields of the response class
                    Console.WriteLine("Type is: {0}", type.Name);

                    PropertyInfo[] props = type.GetProperties();

                    Console.WriteLine("Properties (N = {0}):", props.Length);

                    foreach (var prop in props)
                    {
                        if (prop.GetIndexParameters().Length == 0)
                        {
                            Console.WriteLine("{0} ({1}) : {2}", prop.Name, prop.PropertyType.Name, prop.GetValue(responseObject));
                        }
                        else
                        {
                            Console.WriteLine("{0} ({1}) : <Indexed>", prop.Name, prop.PropertyType.Name);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method is used to upload an attachment to a single record of a module with ID and print the response.
        /// </summary>
        /// <param name="moduleAPIName">The API Name of the record's module</param>
        /// <param name="recordId">The ID of the record to upload attachment</param>
        /// <param name="absoluteFilePath">The absolute file path of the file to be attached</param>
        public static void UploadAttachments(string moduleAPIName, long recordId, string absoluteFilePath)
        {
            //example
            //string moduleAPIName = "Leads";
            //long recordId = 3477002;
            //string absoluteFilePath = "/Users/use_name/Desktop/image.png";

            //Get instance of AttachmentsOperations Class that takes moduleAPIName and recordId as parameter
            AttachmentsOperations attachmentsOperations = new AttachmentsOperations(moduleAPIName, recordId);

            //Get instance of FileBodyWrapper class that will contain the request file
            API.Attachments.FileBodyWrapper fileBodyWrapper = new API.Attachments.FileBodyWrapper();

            //Get instance of StreamWrapper class that takes absolute path of the file to be attached as parameter
            StreamWrapper streamWrapper = new StreamWrapper(absoluteFilePath);

            //Set file to the FileBodyWrapper instance
            fileBodyWrapper.File = streamWrapper;

            //Call UploadAttachment method that takes FileBodyWrapper instance as parameter
            APIResponse <API.Attachments.ActionHandler> response = attachmentsOperations.UploadAttachment(fileBodyWrapper);

            if (response != null)
            {
                //Get the status code from response
                Console.WriteLine("Status Code: " + response.StatusCode);

                //Check if expected response is received
                if (response.IsExpected)
                {
                    //Get object from response
                    API.Attachments.ActionHandler actionHandler = response.Object;

                    if (actionHandler is API.Attachments.ActionWrapper)
                    {
                        //Get the received ActionWrapper instance
                        API.Attachments.ActionWrapper actionWrapper = (API.Attachments.ActionWrapper)actionHandler;

                        //Get the list of obtained action responses
                        List <API.Attachments.ActionResponse> actionResponses = actionWrapper.Data;

                        foreach (API.Attachments.ActionResponse actionResponse in actionResponses)
                        {
                            //Check if the request is successful
                            if (actionResponse is API.Attachments.SuccessResponse)
                            {
                                //Get the received SuccessResponse instance
                                API.Attachments.SuccessResponse successResponse = (API.Attachments.SuccessResponse)actionResponse;

                                //Get the Status
                                Console.WriteLine("Status: " + successResponse.Status.Value);

                                //Get the Code
                                Console.WriteLine("Code: " + successResponse.Code.Value);

                                Console.WriteLine("Details: ");

                                if (successResponse.Details != null)
                                {
                                    //Get the details map
                                    foreach (KeyValuePair <string, object> entry in successResponse.Details)
                                    {
                                        //Get each value in the map
                                        Console.WriteLine(entry.Key + " : " + JsonConvert.SerializeObject(entry.Value));
                                    }
                                }

                                //Get the Message
                                Console.WriteLine("Message: " + successResponse.Message.Value);
                            }
                            //Check if the request returned an exception
                            else if (actionResponse is API.Attachments.APIException)
                            {
                                //Get the received APIException instance
                                API.Attachments.APIException exception = (API.Attachments.APIException)actionResponse;

                                //Get the Status
                                Console.WriteLine("Status: " + exception.Status.Value);

                                //Get the Code
                                Console.WriteLine("Code: " + exception.Code.Value);

                                Console.WriteLine("Details: ");

                                if (exception.Details != null)
                                {
                                    //Get the details map
                                    foreach (KeyValuePair <string, object> entry in exception.Details)
                                    {
                                        //Get each value in the map
                                        Console.WriteLine(entry.Key + " : " + JsonConvert.SerializeObject(entry.Value));
                                    }
                                }

                                //Get the Message
                                Console.WriteLine("Message: " + exception.Message.Value);
                            }
                        }
                    }
                    //Check if the request returned an exception
                    else if (actionHandler is API.Attachments.APIException)
                    {
                        //Get the received APIException instance
                        API.Attachments.APIException exception = (API.Attachments.APIException)actionHandler;

                        //Get the Status
                        Console.WriteLine("Status: " + exception.Status.Value);

                        //Get the Code
                        Console.WriteLine("Code: " + exception.Code.Value);

                        Console.WriteLine("Details: ");

                        if (exception.Details != null)
                        {
                            //Get the details map
                            foreach (KeyValuePair <string, object> entry in exception.Details)
                            {
                                //Get each value in the map
                                Console.WriteLine(entry.Key + ": " + JsonConvert.SerializeObject(entry.Value));
                            }
                        }

                        //Get the Message
                        Console.WriteLine("Message: " + exception.Message.Value);
                    }
                }
                else
                {                 //If response is not as expected
                    //Get model object from response
                    Model responseObject = response.Model;

                    //Get the response object's class
                    Type type = responseObject.GetType();

                    //Get all declared fields of the response class
                    Console.WriteLine("Type is: {0}", type.Name);

                    PropertyInfo[] props = type.GetProperties();

                    Console.WriteLine("Properties (N = {0}):", props.Length);

                    foreach (var prop in props)
                    {
                        if (prop.GetIndexParameters().Length == 0)
                        {
                            Console.WriteLine("{0} ({1}) : {2}", prop.Name, prop.PropertyType.Name, prop.GetValue(responseObject));
                        }
                        else
                        {
                            Console.WriteLine("{0} ({1}) : <Indexed>", prop.Name, prop.PropertyType.Name);
                        }
                    }
                }
            }
        }