private async Task <AsyncConsumer <string> > CreateAsyncRequestAsync(string deviceId, DeviceRequest request)
        {
            if (autostartNotifications)
            {
                await StartNotificationsAsync();
            }

            if (!NotificationsStarted)
            {
                throw new CloudApiException(400, "StartNotifications() needs to be called before creating an async request.");
            }

            var asyncId = $"async-{Guid.NewGuid():N}";

            try
            {
                await DeviceRequestsApi.CreateAsyncRequestAsync(deviceId, asyncId, request);
            }
            catch (mds.Client.ApiException e)
            {
                throw new CloudApiException(e.ErrorCode, e.Message, e.ErrorContent);
            }

            var collection = new AsyncCollection <string>();

            AsyncResponses.TryAdd(asyncId, collection);

            return(new AsyncConsumer <string>(asyncId, collection));
        }
        private string ExecuteSynchronously(Task <AsyncConsumer <string> > task, int?timeout)
        {
            var consumer = task
                           .GetAwaiter()
                           .GetResult();

            if (!AsyncResponses.ContainsKey(consumer.AsyncId))
            {
                throw new CloudApiException(404, "AsyncId not found.");
            }

            var cancellationToken = new CancellationTokenSource();

            if (timeout.HasValue)
            {
                cancellationToken.CancelAfter(timeout.Value);
            }

            try
            {
                return(AsyncResponses[consumer.AsyncId]
                       .TakeAsync(cancellationToken.Token)
                       .ConfigureAwait(false)
                       .GetAwaiter()
                       .GetResult());
            }
            catch (TaskCanceledException)
            {
                throw new CloudApiException(500, $"Timeout getting async value. Timeout {timeout} ms");
            }
        }