public string PageInConsole(Action <BoxEnterpriseEvent> print, BoxEventCollection <BoxEnterpriseEvent> collection)
 {
     print(collection.Entries[0]);
     collection.Entries.RemoveAt(0);
     Reporter.WriteInformation("Show next? Enter q to quit. ");
     return(System.Console.ReadLine().Trim().ToLower());
 }
示例#2
0
 protected virtual void PrintEventCollection(BoxEventCollection <BoxEnterpriseEvent> evts)
 {
     foreach (var evt in evts.Entries)
     {
         this.PrintEvent(evt);
     }
 }
示例#3
0
        public async Task UserEvents_LiveSession()
        {
            const string fileId = "16894943599";

            var events = await _client.EventsManager.UserEventsAsync();

            Assert.IsNotNull(events.NextStreamPosition, "Failed to retrieve user next_stream_position");

            var fileLock = await _client.FilesManager.LockAsync(new BoxFileLockRequest()
            {
                Lock = new BoxFileLock()
                {
                    IsDownloadPrevented = false
                }
            }, fileId);

            var result = await _client.FilesManager.UnLock(fileId);

            BoxEventCollection <BoxEnterpriseEvent> newEvents = null;
            bool keepChecking    = true;
            int  maxTimesToCheck = 10;

            while (keepChecking && maxTimesToCheck > 0)
            {
                newEvents = await _client.EventsManager.UserEventsAsync(streamPosition : events.NextStreamPosition);

                if (newEvents.Entries.Count > 0)
                {
                    keepChecking = false;
                }
                else
                {
                    maxTimesToCheck--;
                    Thread.Sleep(1000);
                }
            }

            var commentEvent = newEvents.Entries.SingleOrDefault(e => e.EventType == "LOCK_CREATE");

            Assert.IsNotNull(commentEvent, "Failed to retrieve user events");
        }
        public async Task <BoxEventCollection <BoxEnterpriseEvent> > ReturnAllEvents(Func <string, Task <BoxEventCollection <BoxEnterpriseEvent> > > callBox)
        {
            var keepGoing      = true;
            var fullCollection = new BoxEventCollection <BoxEnterpriseEvent>();

            fullCollection.Entries = new List <BoxEnterpriseEvent>();
            string streamPosition = "";

            do
            {
                var collection = await callBox(streamPosition);

                if (collection.Entries.Count > 0)
                {
                    fullCollection.Entries.AddRange(collection.Entries);
                }
                if (collection != null && !string.IsNullOrEmpty(collection.NextStreamPosition))
                {
                    streamPosition = collection.NextStreamPosition;
                }
                keepGoing = collection.Entries.Count != 0;
            }while (keepGoing);
            return(fullCollection);
        }
        /// <summary>
        /// Used to get real-time notification of activity in a Box account.
        /// </summary>
        /// <param name="streamPosition">The location in the event stream from which you want to start receiving events.</param>
        /// <param name="newEventsCallback">Method to invoke when new events are received.</param>
        /// <param name="cancellationToken">Used to request that the long polling process terminate.</param>
        /// <param name="streamType">Restricts the types of events returned: all returns all events; changes returns events that may cause file tree changes such as file updates or collaborations; sync returns events that may cause file tree changes only for synced folders.</param>
        /// <param name="dedupeEvents">Whether or not to automatically de-duplicate events as they are received. Defaults to true.</param>
        /// <param name="retryTimeoutOverride">Used to override the retry timeout value returned from the long polling OPTIONS request.</param>
        /// <returns></returns>
        public async Task LongPollUserEvents(string streamPosition,
                                             Action <BoxEventCollection <BoxEnterpriseEvent> > newEventsCallback,
                                             CancellationToken cancellationToken,
                                             UserEventsStreamType streamType = UserEventsStreamType.all,
                                             bool dedupeEvents        = true,
                                             int?retryTimeoutOverride = null)
        {
            const string NEW_CHANGE_MESSAGE = "new_change";

            string nextStreamPosition = streamPosition;

            while (true)
            {
                BoxRequest optionsRequest = new BoxRequest(_config.EventsUri)
                                            .Param("stream_type", streamType.ToString())
                                            .Method(RequestMethod.Options);

                IBoxResponse <BoxLongPollInfoCollection <BoxLongPollInfo> > optionsResponse = await ToResponseAsync <BoxLongPollInfoCollection <BoxLongPollInfo> >(optionsRequest).ConfigureAwait(false);

                var longPollInfo = optionsResponse.ResponseObject.Entries[0];
                var numRetries   = Int32.Parse(longPollInfo.MaxRetries);

                bool pollAgain = true;
                do
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                    try
                    {
                        var        timeout     = retryTimeoutOverride.HasValue ? retryTimeoutOverride.Value : longPollInfo.RetryTimeout;
                        BoxRequest pollRequest = new BoxRequest(longPollInfo.Url)
                        {
                            Timeout = TimeSpan.FromSeconds(timeout)
                        };
                        IBoxResponse <BoxLongPollMessage> pollResponse = await ToResponseAsync <BoxLongPollMessage>(pollRequest).ConfigureAwait(false);

                        var message = pollResponse.ResponseObject.Message;
                        if (message == NEW_CHANGE_MESSAGE)
                        {
                            BoxEventCollection <BoxEnterpriseEvent> newEvents = null;
                            do
                            {
                                newEvents = await UserEventsAsync(streamType : streamType, streamPosition : nextStreamPosition, dedupeEvents : dedupeEvents).ConfigureAwait(false);

                                nextStreamPosition = newEvents.NextStreamPosition;
                                if (newEvents.Entries.Count > 0)
                                {
                                    newEventsCallback.Invoke(newEvents);
                                }
                            } while (newEvents.Entries.Count > 0);
                        }
                    }
                    catch
                    {
                        //Most likely request timed out.
                        //If we've reached maximum number of retries then bounce all the way back to the OPTIONS request.
                        pollAgain = numRetries-- > 0;
                    }
                } while (pollAgain);
            }
        }