public override IEnumerable <Clue> DoProcess(ExecutionContext context, WebhookDataCommand command) { try { if (ConfigurationManager.AppSettings.GetFlag("Feature.Webhooks.Log.Posts", false)) { context.Log.LogDebug(command.HttpPostData); } var configurationDataStore = context.ApplicationContext.Container.Resolve <IConfigurationRepository>(); if (command.WebhookDefinition.ProviderDefinitionId != null) { var providerDefinition = context.Organization.Providers.GetProviderDefinition(context, command.WebhookDefinition.ProviderDefinitionId.Value); var jobDataCheck = context.ApplicationContext.Container.ResolveAll <IProvider>().FirstOrDefault(providerInstance => providerDefinition != null && providerInstance.Id == providerDefinition.ProviderId); var configStoreData = configurationDataStore.GetConfigurationById(context, command.WebhookDefinition.ProviderDefinitionId.Value); // If you have stopped the provider then don't process the webhooks if (providerDefinition?.WebHooks != null) { if (providerDefinition.WebHooks == false || providerDefinition.IsEnabled == false) { return(new List <Clue>()); } } if (jobDataCheck != null) { var crawlJobData = new SalesforceCrawlJobData(); var clues = new List <Clue>(); IAgentJobProcessorArguments jobArgs = new QueuedJob(new AgentJob(Guid.NewGuid(), AgentJobPriority.Normal, "CluedIn" + SalesforceConstants.ProviderName, ProcessingRestriction.Any, null, null)) { TaskScheduler = TaskScheduler.Default, }; var processorState = new AgentJobProcessorState <SalesforceCrawlJobData>(jobArgs, AppContext) { JobData = crawlJobData, Status = new AgentJobStatus { Statistics = new AgentJobStatusStatistics() } }; throw new NotImplementedException($"TODO: Implement this to populate '{clues.GetType()}' with '{processorState}'"); } } } catch (Exception exception) { using (context.Log.BeginScope(new { command.HttpHeaders, command.HttpQueryString, command.HttpPostData, command.WebhookDefinitionId })) { context.Log.LogError(exception, "Could not process web hook message"); } } return(new List <Clue>()); }
public SalesforceClient(ILogger <SalesforceClient> log, SalesforceCrawlJobData salesforceCrawlJobData) // TODO: pass on any extra dependencies { if (salesforceCrawlJobData == null) { throw new ArgumentNullException(nameof(salesforceCrawlJobData)); } //if (client == null) //{ // throw new ArgumentNullException(nameof(client)); //} _jobData = salesforceCrawlJobData; _log = log ?? throw new ArgumentNullException(nameof(log)); //salesforceClient = client ?? throw new ArgumentNullException(nameof(client)); AuthenticationClient auth = Auth().Result; token = auth.AccessToken; salesforceClient = new ForceClient(auth.InstanceUrl, auth.AccessToken, auth.ApiVersion); }
public override async Task <CrawlJobData> GetCrawlJobData( ProviderUpdateContext context, IDictionary <string, object> configuration, Guid organizationId, Guid userId, Guid providerDefinitionId) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } #region oldauth //var salesforceproviderid = id; //var tokenstore = context.organization.datastores.getdatastore<token>(); //if (tokenstore == null) // return null; //var configurationdatastore = context.applicationcontext.container.resolve<iconfigurationrepository>(); //var accounts = tokenstore.select(context, account => account.providerid == salesforceproviderid && account.organizationid == organizationid && account.userid == userid && account.configurationid == providerdefinitionid).tolist(); //if (!accounts.any()) //{ // accounts.foreach (t => t.context = context.applicationcontext) // ; // // todo: i should not be able to have more than one provider per user per organization // accounts = tokenstore.select(context, // account => // account.providerid == salesforceproviderid && account.organizationid == organizationid && // account.userid == userid && account.configurationid == null).tolist(); // if (accounts.count() == 1) // { // if (accounts.first().configurationid == null) // { // accounts.first().configurationid = providerdefinitionid; // tokenstore.update(context, accounts.first()); // } // } //} ////add authetnication for all accounts here //var agenttoken = accounts.select(i => new extendedagenttoken() { accesstoken = i.accesstoken, refreshtoken = i.refreshtoken, expiresin = i.expiresin, data = i.data, accountid = i.accountid }).firstordefault(); //if (agenttoken == null) // return null; //if (!configuration.containskey("accounts")) // configuration.add("accounts", agenttoken); //if (!configuration.containskey("baseuri")) // configuration.add("baseuri", "https://www.salesforce.com"); //var client = new forceclient(accounts.first().data, agenttoken.accesstoken, "v26.0"); //try //{ // // todo: make async : yourkit shows that it takes up quite a lot of cycles. // const string qry = "select id, name from account"; // var results = client.queryasync<account>(qry).result; // //authentication failed // if (results == null) // { // var newtoken = await this.refreshtoken(agenttoken.refreshtoken); // if (configuration.containskey("accounts")) // configuration["accounts"] = new extendedagenttoken() { accesstoken = newtoken.accesstoken, expiresin = null, refreshtoken = agenttoken.refreshtoken, accountid = agenttoken.accountid, data = agenttoken.data }; // var token = accounts.first(); // token.accesstoken = newtoken.accesstoken; // token.refreshtoken = agenttoken.refreshtoken; // token.accesstokencreationdate = datetimeoffset.utcnow; // token.data = agenttoken.data; // token.accountid = agenttoken.accountid; // tokenstore.update(context, token); // results = await client.queryasync<account>(qry); // var providerdefinition = context.organization.providers.getproviderdefinition(context, providerdefinitionid); // if (providerdefinition != null) // { // configurationdatastore.updateconfiguration(context, providerdefinition.id, configuration); // } // else // throw new notfoundexception("provider definition was not found: " + providerdefinitionid); // } // else // { // context.organization.providers.clearproviderdefinitionauthenticationerror(context, providerdefinitionid); // } //} //catch (exception) //{ // if (agenttoken.refreshtoken == null) // { // return new crawljobdata() { errors = new dictionary<string, string>() { { "error", "please contact cluedin support in the top menu to help you authenticate with salesforce." } } }; // } // var newtoken = this.refreshtoken(agenttoken.refreshtoken).result; // if (configuration.containskey("accounts")) // configuration["accounts"] = new extendedagenttoken() { accesstoken = newtoken.accesstoken, expiresin = null, refreshtoken = agenttoken.refreshtoken, accountid = agenttoken.accountid, data = agenttoken.data }; // var token = accounts.first(); // token.accesstoken = newtoken.accesstoken; // token.refreshtoken = agenttoken.refreshtoken; // token.accesstokencreationdate = datetimeoffset.utcnow; // token.data = agenttoken.data; // token.accountid = agenttoken.accountid; // try // { // if (newtoken.accesstoken == null) // { // context.organization.providers.setproviderdefinitionauthenticationerror(context, providerdefinitionid, "failed to refresh the access token for salesforce"); // return new crawljobdata() { errors = new dictionary<string, string>() { { "error", "please contact cluedin support in the top menu to help you setup with salesforce." } } }; // } // client = new forceclient(agenttoken.data, newtoken.accesstoken, "v26.0"); // // todo: make async : yourkit shows that it takes up quite a lot of cycles. // const string qry = "select id, name from account"; // var results = await client.queryasync<account>(qry); // } // catch (forceexception ex) // { // context.organization.providers.setproviderdefinitionauthenticationerror(context, providerdefinitionid, ex); // } // catch (exception ex) // { // context.organization.providers.setproviderdefinitionauthenticationerror(context, providerdefinitionid, ex); // } //} #endregion var jobData = new SalesforceCrawlJobData(); if (configuration.ContainsKey(SalesforceConstants.KeyName.ApiKey)) { jobData.ApiKey = configuration[SalesforceConstants.KeyName.ApiKey].ToString(); } if (configuration.ContainsKey(SalesforceConstants.KeyName.GrantType)) { jobData.GrantType = configuration[SalesforceConstants.KeyName.GrantType].ToString(); } if (configuration.ContainsKey(SalesforceConstants.KeyName.ClientId)) { jobData.ClientId = configuration[SalesforceConstants.KeyName.ClientId].ToString(); } if (configuration.ContainsKey(SalesforceConstants.KeyName.ClientSecret)) { jobData.ClientSecret = configuration[SalesforceConstants.KeyName.ClientSecret].ToString(); } if (configuration.ContainsKey(SalesforceConstants.KeyName.UserName)) { jobData.UserName = configuration[SalesforceConstants.KeyName.UserName].ToString(); } if (configuration.ContainsKey(SalesforceConstants.KeyName.Password)) { jobData.Password = configuration[SalesforceConstants.KeyName.Password].ToString(); } //if (!jobData.IsAuthenticated) // return null; return(await Task.FromResult(jobData)); }