Ejemplo n.º 1
0
        // ALERT : THIS CODE IS AWESOME :D

        /// <summary>
        /// Execute a task asynchronously with Tweetinvi
        /// </summary>
        public static async Task ExecuteTaskAsync(Action action)
        {
            // We store the credentials at the time of the call within the local memory
            var credentialsAtInvokeTime = CredentialsAccessor.CurrentThreadCredentials;

            // We are cloning to avoid changes to the settings before the async operation starts
            var sourceThreadSettingsClone = TweetinviConfig.CurrentThreadSettings.Clone();

            // The lambda expression will store 'credentialsAtInvokeTime' within a generated class
            // In order to keep the reference to the credentials at the time of invocation
            var operationRunWithSpecificCredentials = new Action(() =>
            {
                TweetinviConfig.CurrentThreadSettings.InitialiseFrom(sourceThreadSettingsClone);

                // We get the newly created credentialsAccessor for the async thread (CredentialsAccessor are Thread specific)
                var credentialsAccessor = TweetinviContainer.Resolve <ICredentialsAccessor>();

                // We now use credentials of the lambda expression local variables to perform our operation
                credentialsAccessor.ExecuteOperationWithCredentials(credentialsAtInvokeTime, action);
            });

            AsyncContext.Run(async() =>
            {
                await _taskFactory.ExecuteTaskAsync(operationRunWithSpecificCredentials);
            });

            await Task.CompletedTask;
        }
Ejemplo n.º 2
0
        // ALERT : THIS CODE IS AWESOME :D
        public static async Task ExecuteTaskAsync(Action action)
        {
            // We store the credentials at the time of the call within the local memory
            var credentialsAtInvokeTime = CredentialsAccessor.CurrentThreadCredentials;

            // The lambda expression will store 'credentialsAtInvokeTime' within a generated class
            // In order to keep the reference to the credentials at the time of invocation
            var operationRunWithSpecificCredentials = new Action(() =>
            {
                // We get the newly created credentialsAccessor for the async thread (CredentialsAccessor are Thread specific)
                var credentialsAccessor = TweetinviContainer.Resolve <ICredentialsAccessor>();

                // We now use credentials of the lambda expression local variables to perform our operation
                credentialsAccessor.ExecuteOperationWithCredentials(credentialsAtInvokeTime, action);
            });

            await _taskFactory.ExecuteTaskAsync(operationRunWithSpecificCredentials);
        }
Ejemplo n.º 3
0
        public async Task StartStreamAsync()
        {
            _loggedUser = await _taskFactory.ExecuteTaskAsync(() => _userFactory.GetLoggedUser());

            if (_loggedUser == null)
            {
                StopStream(_exceptionHandler.LastExceptionInfos.WebException);
                return;
            }

            Func <HttpWebRequest> generateWebRequest = delegate
            {
                var query = Resources.Stream_UserStream;
                if (_filterLanguage != null)
                {
                    query = query.AddParameterToQuery("language", _filterLanguage);
                }

                return(_twitterRequestGenerator.GetQueryWebRequest(query, HttpMethod.GET));
            };

            Action <string> eventReceived = json =>
            {
                RaiseJsonObjectReceived(json);

                // We analyze the different types of message from the stream
                if (TryGetEvent(json))
                {
                    return;
                }
                if (TryGetTweet(json))
                {
                    return;
                }
                if (TryGetMessage(json))
                {
                    return;
                }
                if (TryGetWarning(json))
                {
                    return;
                }
                if (TryGetFriends(json))
                {
                    return;
                }

                TryInvokeGlobalStreamMessages(json);
            };

            await _streamResultGenerator.StartStreamAsync(eventReceived, generateWebRequest);
        }
Ejemplo n.º 4
0
        public async Task StartStreamAsync()
        {
            _loggedUser = await _taskFactory.ExecuteTaskAsync(() => _userFactory.GetLoggedUser());

            if (_loggedUser == null)
            {
                StopStream(new WebException("Could not receive information related with currently logged user."));
                return;
            }

            Func <HttpWebRequest> generateWebRequest = delegate
            {
                var queryBuilder = new StringBuilder(Resources.Stream_UserStream);
                AddBaseParametersToQuery(queryBuilder);

                var streamQuery = _twitterQueryFactory.Create(queryBuilder.ToString());
                return(_twitterRequestGenerator.GetQueryWebRequest(streamQuery));
            };

            Action <string> eventReceived = json =>
            {
                RaiseJsonObjectReceived(json);

                // We analyze the different types of message from the stream
                if (TryGetEvent(json))
                {
                    return;
                }
                if (TryGetTweet(json))
                {
                    return;
                }
                if (TryGetMessage(json))
                {
                    return;
                }
                if (TryGetWarning(json))
                {
                    return;
                }
                if (TryGetFriends(json))
                {
                    return;
                }

                TryInvokeGlobalStreamMessages(json);
            };

            await _streamResultGenerator.StartStreamAsync(eventReceived, generateWebRequest);
        }
Ejemplo n.º 5
0
        public async Task StartStreamAsync()
        {
            _authenticatedUser = await _taskFactory.ExecuteTaskAsync(() => _userFactory.GetAuthenticatedUser(Credentials));

            if (_authenticatedUser == null)
            {
                StopStream(new UserStreamFailedToInitializeException("Could not receive information related with currently authenticated user."));
                return;
            }

            Func <ITwitterQuery> generateTwitterQuery = delegate
            {
                var queryBuilder = new StringBuilder(Resources.Stream_UserStream);
                AddBaseParametersToQuery(queryBuilder);

                return(_twitterQueryFactory.Create(queryBuilder.ToString(), HttpMethod.GET, Credentials));
            };

            Action <string> eventReceived = json =>
            {
                RaiseJsonObjectReceived(json);

                // We analyze the different types of message from the stream
                if (TryGetEvent(json))
                {
                    return;
                }
                if (TryGetTweet(json))
                {
                    return;
                }
                if (TryGetMessage(json))
                {
                    return;
                }
                if (TryGetWarning(json))
                {
                    return;
                }
                if (TryGetFriends(json))
                {
                    return;
                }

                TryInvokeGlobalStreamMessages(json);
            };

            await _streamResultGenerator.StartStreamAsync(eventReceived, generateTwitterQuery);
        }
Ejemplo n.º 6
0
 public async Task <IEnumerable <long> > GetFriendIdsAsync(int maxFriendsToRetrieve = 5000)
 {
     return(await _taskFactory.ExecuteTaskAsync(() => GetFriendIds(maxFriendsToRetrieve)));
 }
Ejemplo n.º 7
0
 public async Task <bool> PublishAsync()
 {
     return(await _taskFactory.ExecuteTaskAsync(() => Publish()));
 }
Ejemplo n.º 8
0
 public async Task <bool> UpdateAsync(ITwitterListUpdateParameters parameters)
 {
     return(await _taskFactory.ExecuteTaskAsync(() => Update(parameters)));
 }
Ejemplo n.º 9
0
 public async Task <ITweet> PublishRetweetAsync()
 {
     return(await _taskFactory.ExecuteTaskAsync(() => PublishRetweet()));
 }
Ejemplo n.º 10
0
 public async Task <bool> DestroyAsync()
 {
     return(await _taskFactory.ExecuteTaskAsync(() => Destroy()));
 }
Ejemplo n.º 11
0
 public async Task <IEnumerable <ITweet> > GetTweetsAsync()
 {
     return(await _taskFactory.ExecuteTaskAsync(() => GetTweets()));
 }