public async Task <NexusModsGameViewModel?> GetAsync(uint gameId, CancellationToken ct = default)
        {
            HttpResponseMessage response;

            try
            {
                response = await _httpClientFactory.CreateClient("Metadata.API").GetAsync(
                    $"game/id?gameId={gameId}",
                    HttpCompletionOption.ResponseHeadersRead,
                    ct);
            }
            catch (Exception e) when(e is TaskCanceledException)
            {
                return(null);
            }

            try
            {
                if (response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NoContent)
                {
                    var content = await response.Content.ReadAsStreamAsync(ct);

                    if (await _jsonSerializer.DeserializeAsync <GameDTO?>(content, ct) is { } tuple)
                    {
                        var(id, name, forumUrl, url, domainName) = tuple;
                        return(new NexusModsGameViewModel(id, name, forumUrl, url, domainName));
                    }
                }
                return(null);
            }
            finally
            {
                response.Dispose();
            }
        }
Exemple #2
0
        public async Task <ModViewModel?> GetAsync(uint gameId, uint modId, CancellationToken ct = default)
        {
            var gameDomain = (await _nexusModsGameQueries.GetAsync(gameId, ct))?.DomainName ?? "ERROR";

            var key = $"mod({gameId}, {modId})";

            if (!_cache.TryGetValue(key, _jsonSerializer, out ModViewModel? cacheEntry))
            {
                var response = await _httpClientFactory.CreateClient("NexusMods.API").GetAsync(
                    $"v1/games/{gameDomain}/mods/{modId}.json",
                    HttpCompletionOption.ResponseHeadersRead,
                    ct);

                if (response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NoContent)
                {
                    var content = await response.Content.ReadAsStreamAsync(ct);

                    var mod = await _jsonSerializer.DeserializeAsync <ModDTO?>(content, ct);

                    if (mod is not null)
                    {
                        cacheEntry = new ModViewModel((uint)mod.ModId, mod.Name);
                        var cacheEntryOptions = new DistributedCacheEntryOptions().SetSize(1).SetAbsoluteExpiration(TimeSpan.FromHours(8));
                        await _cache.SetAsync(key, cacheEntry, cacheEntryOptions, _jsonSerializer, ct);
                    }
                }
            }

            return(cacheEntry);
        }
        public async IAsyncEnumerable <NexusModsCommentRootViewModel> GetAllAsync(uint gameIdRequest, uint modIdRequest, [EnumeratorCancellation] CancellationToken ct = default)
        {
            HttpResponseMessage response;

            try
            {
                response = await _httpClientFactory.CreateClient("Metadata.API").GetAsync(
                    $"comments/id?gameId={gameIdRequest}&modId={modIdRequest}",
                    HttpCompletionOption.ResponseHeadersRead,
                    ct);
            }
            catch (Exception e) when(e is TaskCanceledException)
            {
                yield break;
            }

            if (response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NoContent)
            {
                var contentStream = await response.Content.ReadAsStreamAsync(ct);

                var data = await _jsonSerializer.DeserializeAsync <CommentsDTO[]?>(contentStream, ct) ?? Array.Empty <CommentsDTO>();

                foreach (var(gameDomain, gameId, modId, gameName, modName, id, author, authorUrl, avatarUrl, s, isSticky, isLocked, instant, nexusModsCommentReplyViewModels) in data)
                {
                    yield return(new NexusModsCommentRootViewModel(gameDomain, gameId, modId, gameName, modName, new NexusModsCommentViewModel(id, author, authorUrl, avatarUrl, s, isSticky, isLocked, instant, nexusModsCommentReplyViewModels)));
                }
            }

            response.Dispose();
        }
        public async IAsyncEnumerable <SubscriptionViewModel> GetAllAsync([EnumeratorCancellation] CancellationToken ct = default)
        {
            HttpResponseMessage response;

            try
            {
                response = await _httpClientFactory.CreateClient("Subscriptions.API").GetAsync(
                    "all",
                    HttpCompletionOption.ResponseHeadersRead,
                    ct);
            }
            catch (Exception e) when(e is TaskCanceledException)
            {
                yield break;
            }

            if (response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NoContent)
            {
                var content = await response.Content.ReadAsStreamAsync(ct);

                foreach (var tuple in await _jsonSerializer.DeserializeAsync <SubscriptionDTO[]?>(content, ct) ?? Array.Empty <SubscriptionDTO>())
                {
                    var(nexusModsGameId, nexusModsModId) = tuple;
                    yield return(new SubscriptionViewModel(nexusModsGameId, nexusModsModId));
                }
            }

            response.Dispose();
        }
Exemple #5
0
        public async IAsyncEnumerable <NexusModsIssueRootViewModel> GetAllAsync(uint gameIdRequest, uint modIdRequest, [EnumeratorCancellation] CancellationToken ct)
        {
            HttpResponseMessage response;

            try
            {
                response = await _httpClientFactory.CreateClient("Metadata.API").GetAsync(
                    $"issues/id?gameId={gameIdRequest}&modId={modIdRequest}",
                    HttpCompletionOption.ResponseHeadersRead,
                    ct);
            }
            catch (Exception e) when(e is TaskCanceledException)
            {
                yield break;
            }

            if (response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NoContent)
            {
                var contentStream = await response.Content.ReadAsStreamAsync(ct);

                var data = await _jsonSerializer.DeserializeAsync <IssueDTO[]?>(contentStream, ct) ?? Array.Empty <IssueDTO>();

                foreach (var tuple in data)
                {
                    var(gameDomain, gameId, modId, gameName, modName, id, title, isPrivate, isClosed, (statusId, statusName), replyCount, modVersion, (priorityId, priorityName), lastPost) = tuple;
                    yield return(new NexusModsIssueRootViewModel(
                                     gameDomain, gameId, modId, gameName, modName,
                                     new NexusModsIssueViewModel(id, title, isPrivate, isClosed,
                                                                 new NexusModsIssueStatus(statusId, statusName),
                                                                 replyCount, modVersion,
                                                                 new NexusModsIssuePriority(priorityId, priorityName),
                                                                 lastPost)));
                }
            }

            response.Dispose();
        }
Exemple #6
0
        public async Task <bool> IsAuthorizedAsync(CancellationToken ct = default)
        {
            using var response = await _httpClientFactory.CreateClient("Metadata.API").GetAsync(
                      "authorization-status",
                      HttpCompletionOption.ResponseHeadersRead,
                      ct);

            if (response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NoContent)
            {
                var content = await response.Content.ReadAsStreamAsync(ct);

                if (await _jsonSerializer.DeserializeAsync <AuthorizationStatusDTO?>(content, ct) is { } dto)
                {
                    return(dto.IsAuthorized);
                }
            }
            return(false);
        }
        public async Task <RateLimitViewModel?> GetAsync(CancellationToken ct = default)
        {
            using var response = await _httpClientFactory.CreateClient("Metadata.API").GetAsync(
                      "ratelimits",
                      HttpCompletionOption.ResponseHeadersRead,
                      ct);

            if (response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NoContent)
            {
                var content = await response.Content.ReadAsStreamAsync(ct);

                if (await _jsonSerializer.DeserializeAsync <RateLimitDTO?>(content, ct) is { } tuple)
                {
                    var((hourlyLimit, hourlyRemaining, hourlyReset, dailyLimit, dailyRemaining, dailyReset), siteLimitDTO) = tuple;
                    return(new RateLimitViewModel(new APILimitViewModel(hourlyLimit, hourlyRemaining, hourlyReset, dailyLimit, dailyRemaining, dailyReset), new SiteLimitViewModel(siteLimitDTO.RetryAfter)));
                }
            }
            return(null);
        }
        public async IAsyncEnumerable <SubscriptionViewModel> GetAllAsync([EnumeratorCancellation] CancellationToken ct = default)
        {
            using var response = await _httpClientFactory.CreateClient("Subscriptions.API").GetAsync(
                      "all",
                      HttpCompletionOption.ResponseHeadersRead,
                      ct);

            if (response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NoContent)
            {
                var contentStream = await response.Content.ReadAsStreamAsync(ct);

                var data = await _jsonSerializer.DeserializeAsync <SubscriptionDTO[]?>(contentStream, ct) ?? Array.Empty <SubscriptionDTO>();

                foreach (var(subscriberId, nexusModsGameId, nexusModsModId, nexusModsGameName, nexusModsModName) in data)
                {
                    if (!subscriberId.StartsWith("Discord:"))
                    {
                        continue;
                    }

                    yield return(new SubscriptionViewModel(ulong.Parse(subscriberId.Remove(0, 8)), nexusModsGameId, nexusModsModId, nexusModsGameName, nexusModsModName));
                }
            }
        }