Example #1
0
        /// <inheritdoc />
        async Task <PushChannelSubscription> IPushChannelSubscriptions.SaveAsync(PushChannelSubscription subscription)
        {
            Validate();

            var request = _restClient.CreatePostRequest(ChannelSubUrl);

            AddFullWaitIfNecessary(request);

            if (subscription.DeviceId.IsNotEmpty() && subscription.DeviceId == _restClient.Device?.Id)
            {
                AddDeviceAuthenticationToRequest(request, _restClient.Device);
            }

            request.PostData = subscription;

            return(await _restClient.ExecuteRequest <PushChannelSubscription>(request));

            void Validate()
            {
                if (subscription is null)
                {
                    throw new AblyException("Subscription cannot be null", ErrorCodes.BadRequest);
                }

                if (subscription.Channel.IsEmpty())
                {
                    throw new AblyException("Please provide a non-empty channel name.", ErrorCodes.BadRequest);
                }
            }
        }
Example #2
0
        /// <inheritdoc />
        async Task IPushChannelSubscriptions.RemoveAsync(PushChannelSubscription subscription)
        {
            Validate();

            var request = _restClient.CreateRequest(ChannelSubUrl, HttpMethod.Delete);

            AddFullWaitIfNecessary(request);
            request.AddQueryParameters(GetQueryParams());

            if (subscription.DeviceId.IsNotEmpty() && subscription.DeviceId == _restClient.Device?.Id)
            {
                AddDeviceAuthenticationToRequest(request, _restClient.Device);
            }

            _ = await _restClient.ExecuteRequest(request);

            void Validate()
            {
                if (subscription is null)
                {
                    throw new AblyException("Subscription should not be null", ErrorCodes.BadRequest);
                }

                if (subscription.Channel.IsEmpty())
                {
                    throw new AblyException(
                              "Please provide a subscription with a non-empty channel name.",
                              ErrorCodes.BadRequest);
                }
            }

            IEnumerable <KeyValuePair <string, string> > GetQueryParams()
            {
                yield return(new KeyValuePair <string, string>("channel", subscription.Channel));

                if (subscription.DeviceId.IsNotEmpty())
                {
                    yield return(new KeyValuePair <string, string>("deviceId", subscription.DeviceId));
                }

                if (subscription.ClientId.IsNotEmpty())
                {
                    yield return(new KeyValuePair <string, string>("clientId", subscription.ClientId));
                }
            }
        }