Exemplo n.º 1
0
        /// <summary>
        /// Publish a message to the channel
        /// </summary>
        /// <param name="name">The event name of the message to publish</param>
        /// <param name="data">The message payload. Allowed payloads are string, objects and byte[]</param>
        /// <param name="clientId">Explicit message clientId</param>
        public Task PublishAsync(string name, object data, string clientId = null)
        {
            var request = _ablyRest.CreatePostRequest(_basePath + "/messages", Options);

            request.PostData = new List <Message> {
                new Message(name, data, clientId)
            };
            return(_ablyRest.ExecuteRequest(request));
        }
Exemplo n.º 2
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;
                }
            }
        }