/// <summary>
        /// Serializer
        /// </summary>
        /// <remarks>
        /// Creates a <c>string</c>, whose content represents the mode and the collection of
        ///     individualEnrollments in a JSON format.
        /// </remarks>
        /// <param name="mode">the <see cref="BulkOperationMode"/> that defines the single operation to do over the
        ///     individualEnrollments.</param>
        /// <param name="individualEnrollments">the collection of <see cref="IndividualEnrollment"/> that contains the description
        ///     of each individualEnrollment.</param>
        /// <returns>The <c>string</c> with the content of this class.</returns>
        /// <exception cref="ArgumentNullException">if the individualEnrollments is null.</exception>
        /// <exception cref="ArgumentException">if the individualEnrollments is invalid.</exception>
        public static string ToJson(BulkOperationMode mode, IEnumerable <IndividualEnrollment> individualEnrollments)
        {
            if (!(individualEnrollments ?? throw new ArgumentNullException(nameof(individualEnrollments))).Any())
            {
                throw new ArgumentException("The collection is null or empty.", nameof(individualEnrollments));
            }

            var bulkOperation = new BulkOperation
            {
                Mode        = mode,
                Enrollments = individualEnrollments,
            };

            return(JsonConvert.SerializeObject(bulkOperation));
        }
        /// <summary>
        /// Serializer
        /// </summary>
        /// <remarks>
        /// Creates a <code>string</code>, whose content represents the mode and the collection of
        ///     individualEnrollments in a JSON format.
        /// </remarks>
        /// <param name="mode">the <see cref="BulkOperationMode"/> that defines the single operation to do over the
        ///     individualEnrollments.</param>
        /// <param name="individualEnrollments">the collection of <see cref="IndividualEnrollment"/> that contains the description
        ///     of each individualEnrollment.</param>
        /// <returns>The <code>string</code> with the content of this class.</returns>
        /// <exception cref="ArgumentNullException">if the individualEnrollments is null.</exception>
        /// <exception cref="ArgumentException">if the individualEnrollments is invalid.</exception>
        public static string ToJson(BulkOperationMode mode, IEnumerable <IndividualEnrollment> individualEnrollments)
        {
            /* SRS_BULK_OPERATION_21_001: [The toJsonElement shall throw ArgumentException if the provided collection of
             *                              individualEnrollments is null or empty.] */
            if (!(individualEnrollments ?? throw new ArgumentNullException("individualEnrollments")).Any())
            {
                throw new ArgumentException("individualEnrollments");
            }

            /* SRS_BULK_OPERATION_21_002: [The toJson shall return a String with the mode and the collection of individualEnrollments
             *                              using a JSON format.] */
            BulkOperation bulkOperation = new BulkOperation()
            {
                Mode = mode, Enrollments = individualEnrollments
            };

            return(Newtonsoft.Json.JsonConvert.SerializeObject(bulkOperation));
        }
        public override Task <BulkOperationResult> AddEnrollmentsAsync(IEnumerable <Enrollment> enrollments, CancellationToken cancellationToken)
        {
            ThrowIfClosed();

            ValidateRegistrationAndDeviceIdForEnrollmentList(enrollments);

            var operation = new BulkOperation()
            {
                Mode        = BulkOperationMode.Create,
                Enrollments = enrollments
            };

            return(this.httpClientHelper.PostAsync <BulkOperation, BulkOperationResult>(
                       GetBulkEnrollmentUri(),
                       operation,
                       new Dictionary <HttpStatusCode,
                                       Func <HttpResponseMessage, Task <Exception> > >(),
                       null,
                       cancellationToken));
        }
        public override Task RemoveEnrollmentsAsync(IEnumerable <Enrollment> enrollments, CancellationToken cancellationToken)
        {
            ThrowIfClosed();

            foreach (Enrollment enrollment in enrollments)
            {
                if (string.IsNullOrWhiteSpace(enrollment.ETag))
                {
                    throw new ArgumentException(ApiResources.ETagNotSetWhileDeletingEnrollment);
                }
            }

            var operation = new BulkOperation()
            {
                Mode        = BulkOperationMode.Delete,
                Enrollments = enrollments
            };

            return(this.httpClientHelper.PostAsync <BulkOperation, BulkOperationResult>(GetBulkEnrollmentUri(), operation,
                                                                                        new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >(), null, cancellationToken));
        }
        public override Task <BulkOperationResult> UpdateEnrollmentsAsync(IEnumerable <Enrollment> enrollments, bool forceUpdate, CancellationToken cancellationToken)
        {
            ThrowIfClosed();

            ValidateRegistrationAndDeviceIdForEnrollmentList(enrollments);

            foreach (Enrollment enrollment in enrollments)
            {
                if (string.IsNullOrWhiteSpace(enrollment.ETag) && !forceUpdate)
                {
                    throw new ArgumentException(ApiResources.ETagNotSetWhileUpdatingEnrollment);
                }
            }

            var operation = new BulkOperation()
            {
                Mode        = forceUpdate ? BulkOperationMode.Update : BulkOperationMode.UpdateIfMatchETag,
                Enrollments = enrollments
            };

            return(this.httpClientHelper.PostAsync <BulkOperation, BulkOperationResult>(GetBulkEnrollmentUri(), operation,
                                                                                        new Dictionary <HttpStatusCode, Func <HttpResponseMessage, Task <Exception> > >(), null, cancellationToken));
        }