Ejemplo n.º 1
0
        /// <inheritdoc />
        async Task <DeviceDetails> IDeviceRegistrations.SaveAsync(DeviceDetails details)
        {
            Validate();

            var request = _restClient.CreateRequest("/push/deviceRegistrations/" + details.Id, HttpMethod.Put);

            AddFullWaitIfNecessary(request);
            var localDevice = _restClient.Device;

            if (localDevice != null && localDevice.Id == details.Id)
            {
                AddDeviceAuthenticationToRequest(request, localDevice);
            }

            request.PostData = details;
            var result = await _restClient.ExecuteRequest <DeviceDetails>(request);

            return(result);

            void Validate()
            {
                if (details is null || details.Id.IsEmpty())
                {
                    throw new AblyException("Please provide a non-null DeviceDetails including a valid Id", ErrorCodes.BadRequest);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Update device recipient information
        /// The public api doesn't expose this method but it's much easier to put it here than to manually call it when needed.
        /// </summary>
        /// <param name="details">Device details which contain the update.</param>
        internal async Task PatchDeviceRecipient(DeviceDetails details)
        {
            var body = JObject.FromObject(new
            {
                push = new { recipient = details.Push.Recipient },
            });

            ValidateDeviceDetails();
            var request = _restClient.CreateRequest($"/push/deviceRegistrations/{details.Id}", new HttpMethod("PATCH"));

            AddFullWaitIfNecessary(request);
            request.PostData = body;
            _ = await _restClient.ExecuteRequest(request);

            void ValidateDeviceDetails()
            {
                if (details is null)
                {
                    throw new AblyException("DeviceDetails is null.", ErrorCodes.BadRequest);
                }

                if (details.Id.IsEmpty())
                {
                    throw new AblyException("DeviceDetails needs an non empty Id parameter.", ErrorCodes.BadRequest);
                }

                if (details.Push?.Recipient is null)
                {
                    throw new AblyException("A valid recipient is required to patch device recipient.", ErrorCodes.BadRequest);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Register a new device
        /// The public api doesn't expose this method but it's much easier to put it here than to manually call it when needed.
        /// </summary>
        /// <param name="details">Device details needed for registration.</param>
        /// <returns>Updated device including a deviceIdentityToken assigned by the Push service.</returns>
        internal async Task <LocalDevice> RegisterDevice(DeviceDetails details)
        {
            ValidateDeviceDetails();
            var request = _restClient.CreateRequest("/push/deviceRegistrations", HttpMethod.Post);

            AddFullWaitIfNecessary(request);
            request.PostData = details;

            return(await ExecuteRequest());

            void ValidateDeviceDetails()
            {
                if (details is null)
                {
                    throw new AblyException("DeviceDetails is null.", ErrorCodes.BadRequest);
                }

                if (details.Id.IsEmpty())
                {
                    throw new AblyException("DeviceDetails needs an non empty Id parameter.", ErrorCodes.BadRequest);
                }

                if (details.Platform.IsEmpty())
                {
                    throw new AblyException("DeviceDetails needs a valid Platform. Supported values are 'ios', 'android' or 'browser'.", ErrorCodes.BadRequest);
                }

                if (details.FormFactor.IsEmpty())
                {
                    throw new AblyException(
                              "DeviceDetails needs a valid FormFactor. Supporter values are 'phone', 'tablet', 'desktop', 'tv', 'watch', 'car' or 'embedded'.", ErrorCodes.BadRequest);
                }

                if (details.Push?.Recipient is null)
                {
                    throw new AblyException("A valid recipient is required to register a device.", ErrorCodes.BadRequest);
                }
            }

            async Task <LocalDevice> ExecuteRequest()
            {
                try
                {
                    var response = await _restClient.ExecuteRequest(request);

                    var jsonResponse = JObject.Parse(response.TextResponse);
                    var localDevice  = jsonResponse.ToObject <LocalDevice>();
                    var deviceToken  = (string)jsonResponse["deviceIdentityToken"]?["token"];
                    if (deviceToken != null)
                    {
                        localDevice.DeviceIdentityToken = deviceToken;
                    }

                    return(localDevice);
                }
                catch (JsonReaderException jsonEx)
                {
                    _logger.Error("Error registering device. Invalid response", jsonEx);
                    var error = new ErrorInfo("Error registering device. Invalid response.", ErrorCodes.InternalError);
                    throw new AblyException(error, jsonEx);
                }
                catch (AblyException e)
                {
                    _logger.Error("Error registering Device", e);
                    throw;
                }
            }
        }
Ejemplo n.º 4
0
 /// <inheritdoc />
 async Task IDeviceRegistrations.RemoveAsync(DeviceDetails details)
 {
     await((IDeviceRegistrations)this).RemoveAsync(details?.Id);
 }