Exemplo n.º 1
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;
        }
Exemplo n.º 2
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;
            }
        }