Example #1
0
        ///<summary>
        /// The method to delete all the field JSON files under resources directory.
        ///</summary>
        public static void DeleteAllFieldFiles()
        {
            lock (LOCK)
            {
                try
                {
                    if (Directory.Exists(GetDirectory()))
                    {
                        string[] files = Directory.GetFiles(GetDirectory());

                        if (files != null)
                        {
                            foreach (string file in files)
                            {
                                if (file.EndsWith(Constants.JSON_FILE_EXTENSION))
                                {
                                    System.IO.File.Delete(file);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    SDKException exception = new SDKException(e);

                    SDKLogger.LogError(Constants.DELETE_FIELD_FILES_ERROR + JsonConvert.SerializeObject(exception));

                    throw exception;
                }
            }
        }
Example #2
0
        /// <summary>
        /// The method to force-refresh fields of a module.
        /// </summary>
        /// <param name="module">module A string representing the module.</param>
        public static void RefreshFields(string module)
        {
            lock (LOCK)
            {
                try
                {
                    DeleteFields(module);

                    Utility.GetFields(module);
                }
                catch (SDKException e)
                {
                    SDKLogger.LogError(Constants.REFRESH_SINGLE_MODULE_FIELDS_ERROR + module + JsonConvert.SerializeObject(e));

                    throw e;
                }
                catch (Exception e)
                {
                    SDKException exception = new SDKException(e);

                    SDKLogger.LogError(Constants.REFRESH_SINGLE_MODULE_FIELDS_ERROR + module + JsonConvert.SerializeObject(exception));

                    throw exception;
                }
            }
        }
Example #3
0
        /// <summary>
        /// The method to delete fields of the given module from the current user's fields JSON file.
        /// </summary>
        /// <param name="module">A string representing the module.</param>
        public static void DeleteFields(string module)
        {
            try
            {
                string recordFieldDetailsPath = GetDirectory() + Path.DirectorySeparatorChar + GetEncodedFileName();

                if (System.IO.File.Exists(recordFieldDetailsPath))
                {
                    JObject recordFieldDetailsJson = Initializer.GetJSON(recordFieldDetailsPath);

                    if (recordFieldDetailsJson.ContainsKey(module.ToLower()))
                    {
                        Utility.DeleteFields(recordFieldDetailsJson, module);

                        using (StreamWriter sw = System.IO.File.CreateText(recordFieldDetailsPath))
                        {
                            JsonSerializer serializer = new JsonSerializer();

                            serializer.Serialize(sw, recordFieldDetailsJson);

                            sw.Flush();

                            sw.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                SDKException exception = new SDKException(e);

                throw exception;
            }
        }
Example #4
0
        private void SetAPIURL(APIHTTPConnector connector)
        {
            string APIPath = "";

            if (apiPath.Contains(Constants.HTTP))
            {
                if (apiPath.Contains(Constants.CONTENT_API_URL))
                {
                    APIPath = string.Concat(APIPath, Initializer.GetInitializer().Environment.GetFileUploadUrl());

                    try
                    {
                        var uri = new Uri(apiPath);

                        APIPath = string.Concat(APIPath, uri.AbsolutePath);
                    }
                    catch (System.Exception ex)
                    {
                        SDKException excp = new SDKException(ex);

                        SDKLogger.LogError(Constants.INVALID_URL_ERROR + JsonConvert.SerializeObject(excp));

                        throw excp;
                    }
                }
                else
                {
                    if (apiPath.Substring(0, 1).Equals("/"))
                    {
                        apiPath = apiPath.Substring(1);
                    }

                    APIPath = string.Concat(APIPath, apiPath);
                }
            }
            else
            {
                APIPath = string.Concat(APIPath, Initializer.GetInitializer().Environment.GetUrl());

                APIPath = string.Concat(APIPath, apiPath);
            }

            connector.URL = APIPath;
        }
Example #5
0
        ///<summary>
        /// The method to delete fields JSON File of the current user.
        ///</summary>
        public static void DeleteFieldsFile()
        {
            lock (LOCK)
            {
                try
                {
                    string recordFieldDetailsPath = GetDirectory() + Path.DirectorySeparatorChar + GetEncodedFileName();

                    if (System.IO.File.Exists(recordFieldDetailsPath))
                    {
                        System.IO.File.Delete(recordFieldDetailsPath);
                    }
                }
                catch (Exception e)
                {
                    SDKException exception = new SDKException(e);

                    SDKLogger.LogError(Constants.DELETE_FIELD_FILE_ERROR + JsonConvert.SerializeObject(exception));

                    throw exception;
                }
            }
        }
Example #6
0
        /**
         * The method to force-refresh fields of all the available modules.
         * @throws SDKException
         */
        public static void RefreshAllModules()
        {
            lock (LOCK)
            {
                try
                {
                    Utility.RefreshModules();
                }
                catch (SDKException e)
                {
                    SDKLogger.LogError(Constants.REFRESH_ALL_MODULE_FIELDS_ERROR + JsonConvert.SerializeObject(e));

                    throw e;
                }
                catch (Exception e)
                {
                    SDKException exception = new SDKException(e);

                    SDKLogger.LogError(Constants.REFRESH_ALL_MODULE_FIELDS_ERROR + JsonConvert.SerializeObject(exception));

                    throw exception;
                }
            }
        }
Example #7
0
        ///<summary>
        ///This method to get module field data from Zoho CRM.
        ///</summary>
        ///<param name="moduleAPIName">A String containing the CRM module API name.</param>
        ///<returns>A Object representing the Zoho CRM module field details.</returns>
        public static object GetFieldsDetails(string moduleAPIName)
        {
            JObject fieldsDetails = new JObject();

            FieldsOperations fieldOperation = new FieldsOperations(moduleAPIName);

            ParameterMap parameterinstance = new ParameterMap();

            APIResponse <Com.Zoho.Crm.API.Fields.ResponseHandler> response = fieldOperation.GetFields(parameterinstance);

            if (response != null)
            {
                if (response.StatusCode == Constants.NO_CONTENT_STATUS_CODE)
                {
                    return(fieldsDetails);
                }

                //Check if expected response is received
                if (response.IsExpected)
                {
                    Com.Zoho.Crm.API.Fields.ResponseHandler responseHandler = response.Object;

                    if (responseHandler is Com.Zoho.Crm.API.Fields.ResponseWrapper)
                    {
                        Com.Zoho.Crm.API.Fields.ResponseWrapper responseWrapper = (Com.Zoho.Crm.API.Fields.ResponseWrapper)responseHandler;

                        List <Field> fields = (List <Field>)responseWrapper.Fields;

                        foreach (Field field in fields)
                        {
                            string keyName = field.APIName;

                            if (Constants.KEYS_TO_SKIP.Contains(keyName))
                            {
                                continue;
                            }

                            JObject fieldDetail = new JObject();

                            SetDataType(fieldDetail, field, moduleAPIName);

                            fieldsDetails[field.APIName] = fieldDetail;
                        }

                        if (Constants.INVENTORY_MODULES.Contains(moduleAPIName.ToLower()))
                        {
                            JObject fieldDetail = new JObject();

                            fieldDetail.Add(Constants.NAME, Constants.LINE_TAX);

                            fieldDetail.Add(Constants.TYPE, Constants.LIST_NAMESPACE);

                            fieldDetail.Add(Constants.STRUCTURE_NAME, Constants.LINE_TAX_NAMESPACE);

                            fieldDetail.Add(Constants.LOOKUP, true);

                            fieldsDetails.Add(Constants.LINE_TAX, fieldDetail);
                        }

                        if (Constants.NOTES.Equals(moduleAPIName, StringComparison.OrdinalIgnoreCase))
                        {
                            JObject fieldDetail = new JObject();

                            fieldDetail.Add(Constants.NAME, Constants.ATTACHMENTS);

                            fieldDetail.Add(Constants.TYPE, Constants.LIST_NAMESPACE);

                            fieldDetail.Add(Constants.STRUCTURE_NAME, Constants.ATTACHMENTS_NAMESPACE);

                            fieldsDetails.Add(Constants.ATTACHMENTS, fieldDetail);
                        }
                    }
                    else if (responseHandler is Fields.APIException)
                    {
                        Fields.APIException exception = (Fields.APIException)responseHandler;

                        JObject errorResponse = new JObject();

                        errorResponse.Add(Constants.CODE, exception.Code.Value);

                        errorResponse.Add(Constants.STATUS, exception.Status.Value);

                        errorResponse.Add(Constants.MESSAGE, exception.Message.Value);

                        SDKException exception1 = new SDKException(Constants.API_EXCEPTION, errorResponse);

                        if (Utility.moduleAPIName.ToLower().Equals(moduleAPIName.ToLower()))
                        {
                            throw exception1;
                        }

                        SDKLogger.LogError(JsonConvert.SerializeObject(exception1));
                    }
                }
                else
                {
                    JObject errorResponse = new JObject();

                    errorResponse.Add(Constants.CODE, response.StatusCode);

                    throw new SDKException(Constants.API_EXCEPTION, errorResponse);
                }
            }

            return(fieldsDetails);
        }
Example #8
0
        /// <summary>
        /// This method is used in constructing API request and response details. To make the Zoho CRM API calls.
        /// </summary>
        /// <typeparam name="T">A T containing the specified type method.</typeparam>
        /// <param name="className">A Type containing the method return type.</param>
        /// <param name="encodeType">A string containing the expected API response content type.</param>
        /// <returns>A APIResponse&lt;T&gt; representing the Zoho CRM API response instance or null. </returns>
        public APIResponse <T> APICall <T>(Type className, string encodeType)
        {
            if (Initializer.GetInitializer() == null)
            {
                throw new SDKException(Constants.SDK_UNINITIALIZATION_ERROR, Constants.SDK_UNINITIALIZATION_MESSAGE);
            }

            APIHTTPConnector connector = new APIHTTPConnector
            {
                RequestMethod = httpMethod
            };

            try
            {
                SetAPIURL(connector);
            }
            catch (SDKException e)
            {
                SDKLogger.LogError(Constants.SET_API_URL_EXCEPTION + JsonConvert.SerializeObject(e));

                throw e;
            }
            catch (Exception e)
            {
                SDKException exception = new SDKException(e);

                SDKLogger.LogError(Constants.SET_API_URL_EXCEPTION + JsonConvert.SerializeObject(exception));

                throw exception;
            }

            connector.ContentType = contentType;

            if (header != null && header.HeaderMaps.Count > 0)
            {
                connector.Headers = header.HeaderMaps;
            }

            if (param != null && param.ParameterMaps.Count > 0)
            {
                connector.Params = param.ParameterMaps;
            }

            try
            {
                Initializer.GetInitializer().Token.Authenticate(connector);
            }
            catch (SDKException e)
            {
                SDKLogger.LogError(Constants.AUTHENTICATION_EXCEPTION + JsonConvert.SerializeObject(e));

                throw e;
            }
            catch (Exception e)
            {
                SDKException exception = new SDKException(e);

                SDKLogger.LogError(Constants.AUTHENTICATION_EXCEPTION + JsonConvert.SerializeObject(exception));

                throw exception;
            }

            string pack = className.FullName;

            Converter convertInstance = null;

            if (contentType != null && Constants.GENERATE_REQUEST_BODY.Contains(httpMethod.ToUpper()))
            {
                object request;

                try
                {
                    convertInstance = GetConverterClassInstance(contentType.ToLower());

                    request = convertInstance.FormRequest(this.request, this.request.GetType().FullName, null, null);
                }
                catch (SDKException e)
                {
                    SDKLogger.LogError(Constants.FORM_REQUEST_EXCEPTION + JsonConvert.SerializeObject(e));

                    throw e;
                }
                catch (Exception e)
                {
                    SDKException exception = new SDKException(e);

                    SDKLogger.LogError(Constants.FORM_REQUEST_EXCEPTION + JsonConvert.SerializeObject(exception));

                    throw exception;
                }

                connector.RequestBody = request;
            }

            try
            {
                connector.AddHeader(Constants.ZOHO_SDK, Environment.OSVersion.Platform.ToString() + "/" +
                                    Environment.OSVersion.Version.ToString() + "/csharp-2.1/" +
                                    Environment.Version.Major.ToString() + "." +
                                    Environment.Version.Minor.ToString() + ":" + Constants.SDK_VERSION);

                HttpWebResponse response = connector.FireRequest(convertInstance);

                int statusCode = (int)response.StatusCode;

                string statusDescription = response.StatusDescription;

                Dictionary <string, string> headerMap = GetHeaders(response.Headers);

                bool isModel = false;

                string mimeType = response.ContentType;

                Model returnObject = null;

                if (!string.IsNullOrEmpty(mimeType) && !string.IsNullOrWhiteSpace(mimeType))
                {
                    if (mimeType.Contains(";"))
                    {
                        mimeType = mimeType.Split(';')[0];
                    }

                    convertInstance = GetConverterClassInstance(mimeType.ToLower());

                    returnObject = (Model)convertInstance.GetWrappedResponse(response, pack);

                    if (returnObject != null && (pack.Equals(returnObject.GetType().FullName) || IsExpectedType(returnObject, pack)))
                    {
                        isModel = true;
                    }
                }
                else
                {
                    if (response != null)
                    {
                        HttpWebResponse responseEntity = ((HttpWebResponse)response);

                        string responseString = new StreamReader(responseEntity.GetResponseStream()).ReadToEnd();

                        SDKLogger.LogError(Constants.API_ERROR_RESPONSE + responseString);

                        responseEntity.Close();
                    }
                }

                return(new APIResponse <T>(headerMap, statusCode, returnObject, isModel, statusDescription));
            }
            catch (SDKException e)
            {
                SDKLogger.LogError(Constants.API_CALL_EXCEPTION + JsonConvert.SerializeObject(e));

                throw e;
            }
            catch (Exception e)
            {
                SDKException exception = new SDKException(e);

                SDKLogger.LogError(Constants.API_CALL_EXCEPTION + JsonConvert.SerializeObject(exception));

                throw exception;
            }
        }