public void Authenticate(APIHTTPConnector urlConnection) { lock (this) { try { Initializer initializer = Initializer.GetInitializer(); TokenStore store = initializer.Store; UserSignature user = initializer.User; OAuthToken oauthToken = null; if (this.accessToken == null) { if (this.id != null) { oauthToken = (OAuthToken)store.GetTokenById(this.id, this); } else { oauthToken = (OAuthToken)store.GetToken(user, this); } } else { oauthToken = this; } string token = ""; if (oauthToken == null)//first time { token = this.refreshToken != null?this.RefreshAccessToken(user, store).AccessToken : this.GenerateAccessToken(user, store).AccessToken; } else if (oauthToken.ExpiresIn != null && GetExpiryLapseInMillis(oauthToken.ExpiresIn) < 5L)//access token will expire in next 5 seconds or less { SDKLogger.LogInfo(Constants.REFRESH_TOKEN_MESSAGE); token = oauthToken.RefreshAccessToken(user, store).AccessToken; } else { token = oauthToken.AccessToken; } urlConnection.AddHeader(Constants.AUTHORIZATION, Constants.OAUTH_HEADER_PREFIX + token); } catch (System.Exception ex) when(!(ex is SDKException)) { throw new SDKException(ex); } } }
/// <summary> /// This method to initialize the SDK. /// </summary> /// <param name="user">A User class instance represents the CRM user.</param> /// <param name="environment">A Environment class instance containing the CRM API base URL and Accounts URL.</param> /// <param name="token">A Token class instance containing the OAuth client application information.</param> /// <param name="store">A TokenStore class instance containing the token store information.</param> /// <param name="sdkConfig">A SDKConfig class instance containing the configuration.</param> /// <param name="sdkConfig">A SDKConfig class instance containing the configuration.</param> /// <param name="logger">A Logger class instance containing the log file path and Logger type.</param> /// <param name="proxy">An RequestProxy class instance containing the proxy properties of the user.</param> private static void Initialize(UserSignature user, Dc.DataCenter.Environment environment, Token token, TokenStore store, SDKConfig sdkConfig, string resourcePath, Logger.Logger logger, RequestProxy proxy) { try { SDKLogger.Initialize(logger); try { string result = ""; using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(Constants.JSON_DETAILS_FILE_PATH)) { using (StreamReader reader = new StreamReader(stream)) { result = reader.ReadToEnd(); } } jsonDetails = JObject.Parse(result); } catch (System.Exception e) { throw new SDKException(Constants.JSON_DETAILS_ERROR, e); } initializer = new Initializer(); initializer.user = user; initializer.environment = environment; initializer.token = token; initializer.store = store; initializer.sdkConfig = sdkConfig; initializer.resourcePath = resourcePath; initializer.requestProxy = proxy; SDKLogger.LogInfo(Constants.INITIALIZATION_SUCCESSFUL + initializer.ToString()); } catch (SDKException e) { throw e; } catch (Exception e) { throw new SDKException(Constants.INITIALIZATION_EXCEPTION, e); } }
/// <summary> /// The method to switch the different user in SDK environment. /// </summary> /// <param name="user">A User class instance represents the CRM user.</param> /// <param name="environment">A Environment class instance containing the CRM API base URL and Accounts URL.</param> /// <param name="token">A Token class instance containing the OAuth client application information.</param> /// <param name="sdkConfig">A SDKConfig class instance containing the configuration.</param> /// <param name="proxy">An RequestProxy class instance containing the proxy properties of the user. public static void SwitchUser(UserSignature user, Dc.DataCenter.Environment environment, Token token, SDKConfig sdkConfig, RequestProxy proxy) { if (user == null) { throw new SDKException(Constants.SWITCH_USER_ERROR, Constants.USERSIGNATURE_ERROR_MESSAGE); } if (environment == null) { throw new SDKException(Constants.SWITCH_USER_ERROR, Constants.ENVIRONMENT_ERROR_MESSAGE); } if (token == null) { throw new SDKException(Constants.SWITCH_USER_ERROR, Constants.TOKEN_ERROR_MESSAGE); } if (sdkConfig == null) { throw new SDKException(Constants.SWITCH_USER_ERROR, Constants.SDK_CONFIG_ERROR_MESSAGE); } Initializer initializer = new Initializer(); initializer.user = user; initializer.environment = environment; initializer.token = token; initializer.store = Initializer.initializer.store; initializer.sdkConfig = sdkConfig; initializer.resourcePath = Initializer.initializer.resourcePath; initializer.requestProxy = proxy; LOCAL.Value = initializer; SDKLogger.LogInfo(Constants.INITIALIZATION_SWITCHED + initializer.ToString()); }
/// <summary> /// The method to switch the different user in SDK environment. /// </summary> /// <param name="user">A User class instance represents the CRM user.</param> /// <param name="environment">A Environment class instance containing the CRM API base URL and Accounts URL.</param> /// <param name="token">A Token class instance containing the OAuth client application information.</param> /// <param name="sdkConfig">A SDKConfig class instance containing the configuration.</param> /// <param name="proxy">An RequestProxy class instance containing the proxy properties of the user. private static void SwitchUser(UserSignature user, Dc.DataCenter.Environment environment, Token token, SDKConfig sdkConfig, RequestProxy proxy) { Initializer initializer = new Initializer(); initializer.user = user; initializer.environment = environment; initializer.token = token; initializer.store = Initializer.initializer.store; initializer.sdkConfig = sdkConfig; initializer.requestProxy = proxy; initializer.resourcePath = Initializer.initializer.resourcePath; LOCAL.Value = initializer; SDKLogger.LogInfo(Constants.INITIALIZATION_SWITCHED + initializer.ToString()); }
/// <summary> /// This method to initialize the SDK. /// </summary> /// <param name="user">A User class instance represents the CRM user.</param> /// <param name="environment">A Environment class instance containing the CRM API base URL and Accounts URL.</param> /// <param name="token">A Token class instance containing the OAuth client application information.</param> /// <param name="store">A TokenStore class instance containing the token store information.</param> /// <param name="sdkConfig">A SDKConfig class instance containing the configuration.</param> /// <param name="sdkConfig">A SDKConfig class instance containing the configuration.</param> /// <param name="logger">A Logger class instance containing the log file path and Logger type.</param> /// <param name="proxy">An RequestProxy class instance containing the proxy properties of the user.</param> public static void Initialize(UserSignature user, Dc.DataCenter.Environment environment, Token token, TokenStore store, SDKConfig sdkConfig, string resourcePath, Logger.Logger logger, RequestProxy proxy) { try { if (user == null) { throw new SDKException(Constants.INITIALIZATION_ERROR, Constants.USERSIGNATURE_ERROR_MESSAGE); } if (environment == null) { throw new SDKException(Constants.INITIALIZATION_ERROR, Constants.ENVIRONMENT_ERROR_MESSAGE); } if (token == null) { throw new SDKException(Constants.INITIALIZATION_ERROR, Constants.TOKEN_ERROR_MESSAGE); } if (store == null) { throw new SDKException(Constants.INITIALIZATION_ERROR, Constants.STORE_ERROR_MESSAGE); } if (sdkConfig == null) { throw new SDKException(Constants.INITIALIZATION_ERROR, Constants.SDK_CONFIG_ERROR_MESSAGE); } if (string.IsNullOrEmpty(resourcePath) || string.IsNullOrWhiteSpace(resourcePath)) { throw new SDKException(Constants.INITIALIZATION_ERROR, Constants.RESOURCE_PATH_ERROR_MESSAGE); } if (!Directory.Exists(resourcePath)) { throw new SDKException(Constants.INITIALIZATION_ERROR, Constants.RESOURCE_PATH_INVALID_ERROR_MESSAGE); } if (logger == null) { logger = Logger.Logger.GetInstance(Logger.Logger.Levels.INFO, Path.GetDirectoryName(Assembly.GetCallingAssembly().Location) + Path.DirectorySeparatorChar + Constants.LOG_FILE_NAME); } SDKLogger.Initialize(logger); try { string result = ""; using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(Constants.JSON_DETAILS_FILE_PATH)) { using (StreamReader reader = new StreamReader(stream)) { result = reader.ReadToEnd(); } } jsonDetails = JObject.Parse(result); } catch (System.Exception e) { throw new SDKException(Constants.JSON_DETAILS_ERROR, e); } initializer = new Initializer(); initializer.user = user; initializer.environment = environment; initializer.token = token; initializer.store = store; initializer.sdkConfig = sdkConfig; initializer.resourcePath = resourcePath; initializer.requestProxy = proxy; SDKLogger.LogInfo(Constants.INITIALIZATION_SUCCESSFUL + initializer.ToString()); } catch (SDKException e) { throw e; } catch (Exception e) { throw new SDKException(Constants.INITIALIZATION_EXCEPTION, e); } }
/// <summary> /// This method makes a Zoho CRM Rest API request. /// </summary> /// <param name="converterInstance">A Converter class instance to call appendToRequest method.</param> /// <returns>HttpWebResponse class instance or null</returns> public HttpWebResponse FireRequest(Converter converterInstance) { SetQueryParams(); HttpWebRequest requestObj = (HttpWebRequest)WebRequest.Create(url); RequestProxy requestProxy = Initializer.GetInitializer().RequestProxy; if (requestProxy != null) { //Validate proxy address var proxyURI = new Uri(string.Format("{0}:{1}", requestProxy.Host, requestProxy.Port)); ICredentials credentials = null; if (requestProxy.User != null) { //Set credentials credentials = new NetworkCredential(requestProxy.User, requestProxy.Password, requestProxy.UserDomain); } //Set proxy requestObj.Proxy = new WebProxy(proxyURI, true, null, credentials); SDKLogger.LogInfo(this.ProxyLog(requestProxy)); } requestObj.Timeout = Initializer.GetInitializer().SDKConfig.Timeout; SetRequestMethod(requestObj); if (contentType != null) { this.SetContentTypeHeader(ref requestObj); } SetQueryHeaders(ref requestObj); if (requestBody != null) { converterInstance.AppendToRequest(requestObj, requestBody); } SDKLogger.LogInfo(ToString()); HttpWebResponse response; try { response = (HttpWebResponse)requestObj.GetResponse(); } catch (WebException e) { if (e.Response == null) { throw; } response = (HttpWebResponse)e.Response; } return(response); }