public void SaveConfig(CampaignMonitorConfiguration config) { tracer.Trace("Saving configuration"); var entity = new Entity("campmon_configuration"); entity["campmon_clientid"] = config.ClientId; entity["campmon_clientname"] = config.ClientName; entity["campmon_listid"] = config.ListId; entity["campmon_listname"] = config.ListName; entity["campmon_syncduplicateemails"] = config.SyncDuplicateEmails; entity["campmon_syncfields"] = string.Join(",", config.SyncFields); entity["campmon_syncviewid"] = config.SyncViewId.ToString(); entity["campmon_syncviewname"] = config.SyncViewName; entity["campmon_subscriberemail"] = new OptionSetValue((int)config.SubscriberEmail); entity["campmon_bulksyncinprogress"] = config.BulkSyncInProgress; entity["campmon_bulksyncdata"] = config.BulkSyncData; if (config.Id == Guid.Empty) { tracer.Trace("Creating new configuration record."); orgService.Create(entity); } else { tracer.Trace("Updating existing configuration record."); entity.Id = config.Id; orgService.Update(entity); } }
public static AuthenticationDetails GetAuthentication(CampaignMonitorConfiguration config, IOrganizationService orgService) { var auth = new OAuthAuthenticationDetails(config.AccessToken, config.RefreshToken); if (config.TokenValidTo.HasValue && (config.TokenValidTo.Value.AddHours(12) > DateTime.UtcNow)) { return(auth); } else { // token has expired var general = new General(auth); var newToken = general.RefreshToken(); var updatedAuth = new Entity("campmon_configuration", config.Id) { Attributes = { { "campmon_accesstoken", newToken.access_token }, { "campmon_refreshtoken", newToken.refresh_token }, { "campmon_expireson", DateTime.UtcNow.AddMinutes(-1).AddSeconds(newToken.expires_in) }, } }; orgService.Update(updatedAuth); return(new OAuthAuthenticationDetails(newToken.access_token, newToken.refresh_token)); } }
public CampaignMonitorService(CampaignMonitorConfiguration config, IOrganizationService orgService) { if (config == null) { throw new ArgumentNullException("config"); } _config = config; auth = Authenticator.GetAuthentication(config, orgService); }
public CampaignMonitorConfiguration VerifyAndLoadConfig() { var query = new QueryExpression("campmon_configuration"); query.NoLock = true; query.TopCount = 1; query.ColumnSet = new ColumnSet("campmon_accesstoken", "campmon_refreshtoken", "campmon_bulksyncdata", "campmon_bulksyncinprogress", "campmon_clientid", "campmon_clientname", "campmon_listid", "campmon_listname", "campmon_setuperror", "campmon_syncduplicateemails", "campmon_syncfields", "campmon_syncviewid", "campmon_syncviewname", "campmon_subscriberemail", "campmon_configurationid", "campmon_expireson"); var result = orgService.RetrieveMultiple(query); if (!result.Entities.Any()) { return(null); } var configEntity = result.Entities.First(); var config = new CampaignMonitorConfiguration { AccessToken = configEntity.GetAttributeValue <string>("campmon_accesstoken"), RefreshToken = configEntity.GetAttributeValue <string>("campmon_refreshtoken"), TokenValidTo = configEntity.GetAttributeValue <DateTime?>("campmon_expireson"), BulkSyncData = configEntity.GetAttributeValue <string>("campmon_bulksyncdata"), ClientId = configEntity.GetAttributeValue <string>("campmon_clientid"), ClientName = configEntity.GetAttributeValue <string>("campmon_clientname"), Id = configEntity.GetAttributeValue <Guid>("campmon_configurationid"), ListId = configEntity.GetAttributeValue <string>("campmon_listid"), ListName = configEntity.GetAttributeValue <string>("campmon_listname"), SetUpError = configEntity.GetAttributeValue <string>("campmon_setuperror"), SyncDuplicateEmails = configEntity.GetAttributeValue <bool>("campmon_syncduplicateemails"), SyncFields = configEntity.Contains("campmon_syncfields") && !string.IsNullOrWhiteSpace(configEntity.GetAttributeValue <string>("campmon_syncfields")) ? configEntity.GetAttributeValue <string>("campmon_syncfields").Split(',') : Enumerable.Empty <string>(), SyncViewId = configEntity.Contains("campmon_syncviewid") ? Guid.Parse(configEntity.GetAttributeValue <string>("campmon_syncviewid")) : Guid.Empty, SyncViewName = configEntity.GetAttributeValue <string>("campmon_syncviewname"), SubscriberEmail = configEntity.Contains("campmon_subscriberemail") ? (SubscriberEmailValues)(configEntity.GetAttributeValue <OptionSetValue>("campmon_subscriberemail").Value) : SubscriberEmailValues.EmailAddress1 }; if (string.IsNullOrWhiteSpace(config.AccessToken)) { tracer.Trace("Configuration record does not contain AccessToken"); return(null); } return(config); }