/// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader" /> to read from.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>
        /// The object value.
        /// </returns>
        /// <exception cref="System.Exception">Unable to determine the field type.</exception>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.StartArray)
            {
                var fields = new List <Models.Legacy.Field>();
                var jArray = JArray.Load(reader);

                foreach (var item in jArray)
                {
                    var id    = item.GetPropertyValue <int?>("id");
                    var name  = item.GetPropertyValue <string>("name");
                    var type  = item.GetPropertyValue <string>("type");
                    var field = (Models.Legacy.Field)null;

                    switch (type)
                    {
                    case "date":
                        var unixTime = item.GetPropertyValue <long?>("value");
                        if (unixTime.HasValue)
                        {
                            field = new Models.Legacy.Field <DateTime>(name, unixTime.Value.FromUnixTime());
                        }
                        else
                        {
                            field = new Models.Legacy.Field <DateTime?>(name, null);
                        }
                        break;

                    case "text":
                        field = new Models.Legacy.Field <string>(name, item.GetPropertyValue <string>("value"));
                        break;

                    case "number":
                        var numericValue = item.GetPropertyValue <long?>("value");
                        if (numericValue.HasValue)
                        {
                            field = new Models.Legacy.Field <long>(name, numericValue.Value);
                        }
                        else
                        {
                            field = new Models.Legacy.Field <long?>(name, null);
                        }
                        break;

                    default:
                        throw new Exception($"{type} is an unknown field type");
                    }

                    if (id.HasValue)
                    {
                        field.Id = id.Value;
                    }
                    fields.Add(field);
                }

                return(fields.ToArray());
            }

            return(Array.Empty <Models.Legacy.Field>());
        }
Beispiel #2
0
        public async Task RunAsync(ILegacyClient client, TextWriter log, CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            await log.WriteLineAsync("\n***** LEGACY CONTACTS AND CUSTOM FIELDS *****\n").ConfigureAwait(false);

            // GET ALL FIELDS
            var fields = await client.CustomFields.GetAllAsync(null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"All custom fields retrieved. There are {fields.Length} fields").ConfigureAwait(false);

            // CLEANUP PREVIOUS INTEGRATION TESTS THAT MIGHT HAVE BEEN INTERRUPTED BEFORE THEY HAD TIME TO CLEANUP AFTER THEMSELVES
            var cleanUpTasks = fields
                               .Where(f => f.Name.StartsWith("stronggrid_"))
                               .Select(async oldField =>
            {
                await client.CustomFields.DeleteAsync(oldField.Id, null, cancellationToken).ConfigureAwait(false);
                await log.WriteLineAsync($"Field {oldField.Id} deleted").ConfigureAwait(false);
                await Task.Delay(250).ConfigureAwait(false);                            // Brief pause to ensure SendGrid has time to catch up
            });
            await Task.WhenAll(cleanUpTasks).ConfigureAwait(false);

            var nicknameField = await client.CustomFields.CreateAsync("stronggrid_nickname", FieldType.Text, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Field '{nicknameField.Name}' Id: {nicknameField.Id}").ConfigureAwait(false);

            var ageField = await client.CustomFields.CreateAsync("stronggrid_age", FieldType.Number, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Field '{ageField.Name}' Id: {ageField.Id}").ConfigureAwait(false);

            var customerSinceField = await client.CustomFields.CreateAsync("stronggrid_customer_since", FieldType.Date, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Field '{customerSinceField.Name}' Id: {customerSinceField.Id}").ConfigureAwait(false);

            fields = await client.CustomFields.GetAllAsync(null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"All custom fields retrieved. There are {fields.Length} fields").ConfigureAwait(false);

            var email        = "*****@*****.**";
            var firstName    = "Robert";
            var lastName     = "Unknown";
            var customFields = new Models.Legacy.Field[]
            {
                new Models.Legacy.Field <string>("stronggrid_nickname", "Bob"),
                new Models.Legacy.Field <long?>("stronggrid_age", 42),
                new Models.Legacy.Field <DateTime>("stronggrid_customer_since", new DateTime(2000, 12, 1))
            };
            var contactId = await client.Contacts.CreateAsync(email, firstName, lastName, customFields, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Contact {contactId} created: {firstName} {lastName}").ConfigureAwait(false);

            var newLastName = "Smith";
            await client.Contacts.UpdateAsync(email, null, newLastName, cancellationToken : cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Contact {contactId} updated: {firstName} {newLastName}").ConfigureAwait(false);

            var contact = await client.Contacts.GetAsync(contactId, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Retrieved contact {contactId}").ConfigureAwait(false);

            await log.WriteLineAsync($"\tEmail: {contact.Email}").ConfigureAwait(false);

            await log.WriteLineAsync($"\tFirst Name: {contact.FirstName}").ConfigureAwait(false);

            await log.WriteLineAsync($"\tLast Name: {contact.LastName}").ConfigureAwait(false);

            await log.WriteLineAsync($"\tCreated On:{contact.CreatedOn}").ConfigureAwait(false);

            await log.WriteLineAsync($"\tModified On: {contact.ModifiedOn}").ConfigureAwait(false);

            await log.WriteLineAsync($"\tLast Clicked On: {contact.LastClickedOn}").ConfigureAwait(false);

            await log.WriteLineAsync($"\tLast Emailed On: {contact.LastEmailedOn}").ConfigureAwait(false);

            await log.WriteLineAsync($"\tLast Opened On: {contact.LastOpenedOn}").ConfigureAwait(false);

            foreach (var customField in contact.CustomFields.OfType <Models.Legacy.Field <string> >())
            {
                await log.WriteLineAsync($"\t{customField.Name}: {customField.Value}").ConfigureAwait(false);
            }
            foreach (var customField in contact.CustomFields.OfType <Models.Legacy.Field <long?> >())
            {
                await log.WriteLineAsync($"\t{customField.Name}: {customField.Value}").ConfigureAwait(false);
            }
            foreach (var customField in contact.CustomFields.OfType <Models.Legacy.Field <DateTime?> >())
            {
                await log.WriteLineAsync($"\t{customField.Name}: {customField.Value}").ConfigureAwait(false);
            }

            var recordsPerPage = 5;
            var contacts       = await client.Contacts.GetAsync(recordsPerPage, 1, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync(contacts.Length < recordsPerPage?$"Found {contacts.Length} contacts" : $"Retrieved the first {recordsPerPage} contacts").ConfigureAwait(false);

            foreach (var record in contacts)
            {
                await log.WriteLineAsync($"\t{record.FirstName} {record.LastName}").ConfigureAwait(false);
            }

            var firstNameCondition = new SearchCondition
            {
                Field           = "first_name",
                Value           = "Robert",
                Operator        = ConditionOperator.Equal,
                LogicalOperator = LogicalOperator.None
            };
            var LastNameCondition = new SearchCondition
            {
                Field           = "last_name",
                Value           = "Smith",
                Operator        = ConditionOperator.Equal,
                LogicalOperator = LogicalOperator.And
            };
            var searchResult = await client.Contacts.SearchAsync(new[] { firstNameCondition, LastNameCondition }, null, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Found {searchResult.Length} contacts named Robert Smith").ConfigureAwait(false);

            var billableCount = await client.Contacts.GetBillableCountAsync(null, cancellationToken).ConfigureAwait(false);

            var totalCount = await client.Contacts.GetTotalCountAsync(null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync("Record counts").ConfigureAwait(false);

            await log.WriteLineAsync($"\tBillable: {billableCount}").ConfigureAwait(false);

            await log.WriteLineAsync($"\tTotal: {totalCount}").ConfigureAwait(false);

            await client.Contacts.DeleteAsync(contactId, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Contact {contactId} deleted: {firstName} {newLastName}").ConfigureAwait(false);

            await client.CustomFields.DeleteAsync(nicknameField.Id, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Field {nicknameField.Id} deleted").ConfigureAwait(false);

            await client.CustomFields.DeleteAsync(ageField.Id, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Field {ageField.Id} deleted").ConfigureAwait(false);

            await client.CustomFields.DeleteAsync(customerSinceField.Id, null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"Field {customerSinceField.Id} deleted").ConfigureAwait(false);

            fields = await client.CustomFields.GetAllAsync(null, cancellationToken).ConfigureAwait(false);

            await log.WriteLineAsync($"All custom fields retrieved. There are {fields.Length} fields").ConfigureAwait(false);
        }
Beispiel #3
0
        public override Models.Legacy.Field[] Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            if (JsonDocument.TryParseValue(ref reader, out var doc))
            {
                if (doc.RootElement.ValueKind == JsonValueKind.Array)
                {
                    var fields = new List <Models.Legacy.Field>();

                    foreach (var item in doc.RootElement.EnumerateArray())
                    {
                        var id    = item.TryGetProperty("id", out JsonElement idProperty) ? idProperty.GetInt32() : (int?)null;
                        var name  = item.TryGetProperty("name", out JsonElement nameProperty) ? nameProperty.GetString() : null;
                        var type  = item.TryGetProperty("type", out JsonElement typeProperty) ? typeProperty.GetString() : null;
                        var field = (Models.Legacy.Field)null;

                        switch (type)
                        {
                        case "date":
                            if (!item.TryGetProperty("value", out JsonElement dateProperty))
                            {
                                field = new Models.Legacy.Field <DateTime?>(name, null);
                            }
                            else if (dateProperty.ValueKind == JsonValueKind.Number)
                            {
                                field = new Models.Legacy.Field <DateTime>(name, dateProperty.GetInt64().FromUnixTime());
                            }
                            else
                            {
                                field = new Models.Legacy.Field <DateTime>(name, long.Parse(dateProperty.GetString()).FromUnixTime());
                            }
                            break;

                        case "text":
                            if (!item.TryGetProperty("value", out JsonElement textProperty))
                            {
                                field = new Models.Legacy.Field <string>(name, null);
                            }
                            else
                            {
                                field = new Models.Legacy.Field <string>(name, textProperty.GetString());
                            }
                            break;

                        case "number":
                            if (!item.TryGetProperty("value", out JsonElement numericProperty))
                            {
                                field = new Models.Legacy.Field <long?>(name, null);
                            }
                            else if (numericProperty.ValueKind == JsonValueKind.Number)
                            {
                                field = new Models.Legacy.Field <long>(name, numericProperty.GetInt64());
                            }
                            else
                            {
                                field = new Models.Legacy.Field <long>(name, long.Parse(numericProperty.GetString()));
                            }
                            break;

                        default:
                            throw new Exception($"{type} is an unknown field type");
                        }

                        if (id.HasValue)
                        {
                            field.Id = id.Value;
                        }
                        fields.Add(field);
                    }

                    return(fields.ToArray());
                }
            }

            return(Array.Empty <Models.Legacy.Field>());
        }