Пример #1
0
        public override DataTableStore GetDataTable(DataTableStore dt)
        {
            dt.AddIdentifierColumn(typeof(string));

            var uriHelper = new MailChimpUriHelper(APIKey);
            var mapping   = new DataSchemaMapping(SchemaMap, Side);
            var schema    = MailChimpDataSchema.MailChimpListSchema();

            helper.SetAuthorizationHeader(APIKey);
            var result = helper.GetRequestAsJson(uriHelper.ListServiceUrl);

            if (result["lists"] != null)
            {
                foreach (var item_row in result["lists"])
                {
                    var newRow = dt.NewRow();

                    foreach (DataSchemaItem item in SchemaMap.GetIncludedColumns())
                    {
                        string columnName = mapping.MapColumnToDestination(item);

                        var schemaItem = schema[columnName];

                        if (schemaItem.IsSubValue)
                        {
                            foreach (var six in item_row[schemaItem.ObjectName])
                            {
                                foreach (var sub_item in item_row[schemaItem.ObjectName].Children <JProperty>())
                                {
                                    if (sub_item.Name.Equals(schemaItem.FieldName))
                                    {
                                        newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(sub_item.Value, item.DataType);
                                    }
                                }
                            }
                        }
                        else
                        {
                            newRow[item.ColumnName] = DataSchemaTypeConverter.ConvertTo(item_row[schemaItem.FieldName], item.DataType);
                        }
                    }

                    if (dt.Rows.AddWithIdentifier(newRow, DataSchemaTypeConverter.ConvertTo <string>(item_row["id"])) == DataTableStore.ABORT)
                    {
                        break;
                    }
                }
            }

            return(dt);
        }
        public override DataTableStore GetDataTable(DataTableStore dt, DataTableKeySet keyset)
        {
            dt.AddIdentifierColumn(typeof(string));

            var uriHelper        = new MailChimpUriHelper(APIKey);
            var mapping          = new DataSchemaMapping(SchemaMap, Side);
            var schema           = MailChimpDataSchema.MailChimpMemberSchema();
            var hash_helper      = new HashHelper(HashHelper.HashType.MD5);
            var included_columns = SchemaMap.GetIncludedColumns();

            helper.SetAuthorizationHeader(APIKey);

            var target_index = mapping.MapColumnToDestination(keyset.KeyColumn);

            foreach (var key in keyset.KeyValues)
            {
                var index = key;
                if (target_index.Equals("email_address"))
                {
                    index = hash_helper.GetHashAsString(DataSchemaTypeConverter.ConvertTo <string>(key)).ToLower();
                }

                try
                {
                    var result = helper.GetRequestAsJson($"{uriHelper.ListServiceUrl}/{ListId}/members/{index}");

                    var newRow = dt.NewRow();
                    var id     = ProcessRow(mapping, schema, included_columns, result, newRow);

                    if (dt.Rows.AddWithIdentifier(newRow, id) == DataTableStore.ABORT)
                    {
                        break;
                    }
                }
                catch (WebException e)
                {
                    var response = e.Response as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.NotFound)
                    {
                        continue;
                    }

                    throw;
                }
            }


            return(dt);
        }
        public override DataTableStore GetDataTable(DataTableStore dt)
        {
            dt.AddIdentifierColumn(typeof(string));

            var uriHelper        = new MailChimpUriHelper(APIKey);
            var mapping          = new DataSchemaMapping(SchemaMap, Side);
            var schema           = MailChimpDataSchema.MailChimpMemberSchema();
            var included_columns = SchemaMap.GetIncludedColumns();

            int  total_items = 0;
            int  count       = 0;
            bool abort       = false;

            helper.SetAuthorizationHeader(APIKey);

            do
            {
                var result = helper.GetRequestAsJson($"{uriHelper.ListServiceUrl}/{ListId}/members?count={PageSize}&offset={count}");

                total_items = result["total_items"].ToObject <int>();

                if (result["members"] != null)
                {
                    foreach (var item_row in result["members"])
                    {
                        count++;

                        var newRow = dt.NewRow();
                        var id     = ProcessRow(mapping, schema, included_columns, item_row, newRow);

                        if (dt.Rows.AddWithIdentifier(newRow, id) == DataTableStore.ABORT)
                        {
                            abort = true;
                            break;
                        }
                    }
                }
            } while (!abort && count < total_items);

            return(dt);
        }
Пример #4
0
 public override DataSchema GetDefaultDataSchema()
 {
     return(MailChimpDataSchema.MailChimpDataSyncSchema(MailChimpDataSchema.MailChimpListSchema()));
 }
        public override void AddItems(List <DataCompareItem> items, IDataSynchronizationStatus status)
        {
            if (items != null && items.Count > 0)
            {
                int currentItem = 0;

                foreach (var item in items)
                {
                    if (!status.ContinueProcessing)
                    {
                        break;
                    }

                    var itemInvariant = new DataCompareItemInvariant(item);

                    try
                    {
                        //Call the Automation BeforeAddItem (Optional only required if your supporting Automation Item Events)
                        Automation?.BeforeAddItem(this, itemInvariant, null);

                        if (itemInvariant.Sync)
                        {
                            #region Add Item

                            //Get the Target Item Data
                            var targetItem       = AddItemToDictionary(Mapping, itemInvariant);
                            var targetItemToSend = new Dictionary <string, object>();

                            foreach (var k in targetItem.Keys)
                            {
                                if (!MailChimpDataSchema.ContainsKey(k))
                                {
                                    continue;
                                }

                                var mc = MailChimpDataSchema[k];

                                if (mc.ReadOnly)
                                {
                                    continue;
                                }

                                if (mc.IsSubValue)
                                {
                                    if (mc.IsArray)
                                    {
                                        if (mc.ObjectName == "tags")
                                        {
                                            // If Tags - create an array of Tags to add.
                                            targetItemToSend[mc.ObjectName] = DataSchemaTypeConverter.ConvertTo <string[]>(targetItem[k]);
                                        }
                                    }
                                    else
                                    {
                                        if (!targetItemToSend.ContainsKey(mc.ObjectName))
                                        {
                                            targetItemToSend[mc.ObjectName] = new Dictionary <string, object>();
                                        }

                                        var subValue = targetItemToSend[mc.ObjectName] as Dictionary <string, object>;

                                        subValue[mc.FieldName] = targetItem[k];
                                    }
                                }
                                else
                                {
                                    targetItemToSend[mc.FieldName] = targetItem[k];
                                }
                            }

                            var json    = JsonConvert.SerializeObject(targetItemToSend, Formatting.None);
                            var result  = WebRequestHelper.PostRequestAsJson(json, $"{ListServiceUrl}/{DataSourceReader.ListId}/members");
                            var item_id = result["id"].ToObject <string>();

                            //Call the Automation AfterAddItem (pass the created item identifier if possible)
                            Automation?.AfterAddItem(this, itemInvariant, item_id);
                        }

                        #endregion

                        ClearSyncStatus(item); //Clear the Sync Flag on Processed Rows
                    }
                    catch (WebException e)
                    {
                        Automation?.ErrorItem(this, itemInvariant, null, e);
                        HandleError(status, e);
                    }
                    catch (SystemException e)
                    {
                        Automation?.ErrorItem(this, itemInvariant, null, e);
                        HandleError(status, e);
                    }
                    finally
                    {
                        status.Progress(items.Count, ++currentItem); //Update the Sync Progress
                    }
                }
            }
        }
        public override void UpdateItems(List <DataCompareItem> items, IDataSynchronizationStatus status)
        {
            if (items != null && items.Count > 0)
            {
                int currentItem = 0;

                foreach (var item in items)
                {
                    if (!status.ContinueProcessing)
                    {
                        break;
                    }

                    var itemInvariant = new DataCompareItemInvariant(item);

                    // Get the item ID from the Target Identifier Store
                    var item_id = itemInvariant.GetTargetIdentifier <string>();

                    try
                    {
                        //Call the Automation BeforeUpdateItem (Optional only required if your supporting Automation Item Events)
                        Automation?.BeforeUpdateItem(this, itemInvariant, item_id);

                        if (itemInvariant.Sync)
                        {
                            #region Update Item

                            //Get the Target Item Data
                            var targetItem       = UpdateItemToDictionary(Mapping, itemInvariant);
                            var targetItemToSend = new Dictionary <string, object>();

                            foreach (var k in targetItem.Keys)
                            {
                                if (!MailChimpDataSchema.ContainsKey(k))
                                {
                                    continue;
                                }

                                var mc = MailChimpDataSchema[k];

                                if (mc.ReadOnly)
                                {
                                    continue;
                                }

                                if (mc.IsSubValue)
                                {
                                    if (mc.IsArray)
                                    {
                                        if (mc.ObjectName == "tags")
                                        {
                                            // If Tags - get the original list of tags and work out which ones to add/remove

                                            var source_item = itemInvariant.Row.First(p => Mapping.MapColumnToDestination(p) == k);

                                            var existing = GetHashSet <string>(source_item.BeforeColumnValue);
                                            var toadd    = GetHashSet <string>(source_item.AfterColumnValue);

                                            var arrayOfDictionary = new List <Dictionary <string, object> >();

                                            // Tags to Add
                                            foreach (var v in toadd.Except(existing))
                                            {
                                                arrayOfDictionary.Add(new Dictionary <string, object>()
                                                {
                                                    { "name", v }, { "status", "active" }
                                                });
                                            }

                                            // Tags to Remove
                                            foreach (var v in existing.Except(toadd))
                                            {
                                                arrayOfDictionary.Add(new Dictionary <string, object>()
                                                {
                                                    { "name", v }, { "status", "inactive" }
                                                });
                                            }

                                            var tags_json   = JsonConvert.SerializeObject(new { tags = arrayOfDictionary }, Formatting.None);
                                            var tags_result = WebRequestHelper.PostRequestAsJson(tags_json, $"{ListServiceUrl}/{DataSourceReader.ListId}/members/{item_id}/tags");
                                        }
                                    }
                                    else
                                    {
                                        if (!targetItemToSend.ContainsKey(mc.ObjectName))
                                        {
                                            targetItemToSend[mc.ObjectName] = new Dictionary <string, object>();
                                        }

                                        var subValue = targetItemToSend[mc.ObjectName] as Dictionary <string, object>;

                                        subValue[mc.FieldName] = targetItem[k];
                                    }
                                }
                                else
                                {
                                    targetItemToSend[mc.FieldName] = targetItem[k];
                                }
                            }

                            if (targetItemToSend.Any())
                            {
                                var json   = JsonConvert.SerializeObject(targetItemToSend, Formatting.None);
                                var result = WebRequestHelper.PutRequestAsJson(json, $"{ListServiceUrl}/{DataSourceReader.ListId}/members/{item_id}");
                            }


                            //Call the Automation AfterUpdateItem
                            Automation?.AfterUpdateItem(this, itemInvariant, item_id);


                            #endregion
                        }

                        ClearSyncStatus(item); //Clear the Sync Flag on Processed Rows
                    }
                    catch (WebException e)
                    {
                        Automation?.ErrorItem(this, itemInvariant, item_id, e);
                        HandleError(status, e);
                    }
                    catch (SystemException e)
                    {
                        Automation?.ErrorItem(this, itemInvariant, item_id, e);
                        HandleError(status, e);
                    }
                    finally
                    {
                        status.Progress(items.Count, ++currentItem); //Update the Sync Progress
                    }
                }
            }
        }