public async Task <string> DownloadAnonymous(string url, IEnumerable <KeyValuePair <string, string> > postBody = null)
 {
     using var request = CreateRequest(url, method: postBody == null ? HttpMethod.Get : HttpMethod.Post,
                                       requestBody: postBody);
     return(await _anonymousHttpClientLock.WithReadLock(nameof(DownloadAnonymous), async() =>
     {
         using var response = await _anonymousHttpClient.SendAsync(request);
         return await response.Content.ReadAsStringAsync();
     }));
Exemplo n.º 2
0
        public async Task <int> GetUserIdFromName(string userName)
        {
            await _userIdCacheLock.WithWriteLock(nameof(GetUserIdFromName) + userName, () =>
            {
                if (_cacheExpire < DateTime.UtcNow)
                {
                    _cacheExpire = DateTime.UtcNow.AddHours(CACHE_TTL_HOURS);
                    _userNameToIdMapCache.Clear();
                }
            });

            var id = await _userIdCacheLock.WithReadLock(nameof(GetUserIdFromName) + userName, () =>
            {
                if (_userNameToIdMapCache.TryGetValue(userName, out var cachedId))
                {
                    return((int?)cachedId);
                }
                else
                {
                    return(default(int?));
                }
            });

            if (id.HasValue)
            {
                return(id.Value);
            }

            var results = await _searchParser.Search(string.Empty, userName, string.Empty, string.Empty, 0);

            if (results.Results.Count > 0)
            {
                var threadId = results.Results[0].Id;
                var thread   = await _threadParser.GetThread(threadId);

                var userPost = thread.Posts.FirstOrDefault(p => p.Author.Equals(userName, System.StringComparison.OrdinalIgnoreCase));
                if (userPost != null)
                {
                    await _userIdCacheLock.WithWriteLock(nameof(GetUserIdFromName) + userName, () =>
                    {
                        _userNameToIdMapCache[userName] = userPost.AuthorId;
                    });

                    return(userPost.AuthorId);
                }
            }

            throw new System.Exception($"Cannot find user id for {userName}");
        }
        public async Task <string> DownloadWithSharedLogin(string url, bool verifyLoginStatus = true,
                                                           IEnumerable <KeyValuePair <string, string> > postBody = null)
        {
            var html = await _sharedHttpClientLock.WithReadLock(nameof(DownloadWithSharedLogin), () =>
                                                                DownloadWithExistingSharedLoginCookies(url, verifyLoginStatus, postBody));

            if (html != null)
            {
                return(html);
            }

            await _sharedHttpClientLock.WithWriteLock(nameof(DownloadWithSharedLogin), async() =>
            {
                // Another concurrent task might have successfully logged in while we were waiting for the lock, so
                // let's try it one more time without logging in.
                html = await DownloadWithExistingSharedLoginCookies(url, verifyLoginStatus, postBody);
                if (html != null)
                {
                    return;
                }

                // Nope, it's up to us to log in.
                await LogIntoSharedAccount();
            });

            html = await _sharedHttpClientLock.WithReadLock(nameof(DownloadWithSharedLogin), () =>
                                                            DownloadWithExistingSharedLoginCookies(url, verifyLoginStatus, postBody));

            if (html != null)
            {
                return(html);
            }

            // Give up.
            throw new Exception("Unable to log into the shared user account.");
        }
        public async Task<UserData> GetUserData(string username)
        {
            if (string.IsNullOrEmpty(username))
                throw new Api400Exception(Api400Exception.Codes.INVALID_LOGIN, "Username must be provided.");

            try
            {
                return await _lock.WithReadLock(nameof(GetUserData),
                    funcAsync: async () =>
                    {
                        var filePath = GetFilePath(username);
                        if (!File.Exists(filePath))
                            return new UserData();

                        using var stream = File.OpenRead(filePath);
                        return await JsonSerializer.DeserializeAsync<UserData>(stream);
                    });
            }
 public async Task <List <EventModel> > CloneEventsList()
 {
     return(await _lock.WithReadLock(nameof(CloneEventsList),
                                     func : () => new List <EventModel>(_events)));
 }