Exemple #1
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);
        }
Exemple #2
0
        public async IAsyncEnumerable <CommentViewModel> GetAllAsync(uint gameId, uint modId, [EnumeratorCancellation] CancellationToken ct = default)
        {
            var game = await _nexusModsGameQueries.GetAsync(gameId, ct);

            if (game is null)
            {
                yield break;
            }
            var gameDomain = game.DomainName;
            var gameName   = game.Name;

            var mod = await _nexusModsModQueries.GetAsync(gameDomain, modId, ct);

            if (mod is null)
            {
                yield break;
            }
            var modName = mod.Name;

            var threadViewModel = await _nexusModsThreadQueries.GetAsync(gameId, modId, ct);

            if (threadViewModel is null)
            {
                yield break;
            }
            var threadId = threadViewModel.ThreadId;

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

            if (!_cache.TryGetValue(key, _jsonSerializer, out CommentViewModel[]? cacheEntry))
Exemple #3
0
        public async IAsyncEnumerable <IssueViewModel> GetAllAsync(uint gameId, uint modId, [EnumeratorCancellation] CancellationToken ct = default)
        {
            var game = await _nexusModsGameQueries.GetAsync(gameId, ct);

            var gameDomain = game?.DomainName ?? "ERROR";
            var gameName   = game?.Name ?? "ERROR";

            var mod = await _nexusModsModQueries.GetAsync(gameDomain, modId, ct);

            var modName = mod?.Name ?? "ERROR";

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

            if (!_cache.TryGetValue(key, _jsonSerializer, out IssueViewModel[]? cacheEntry))
Exemple #4
0
        public async Task <ThreadViewModel?> GetAsync(uint gameId, uint modId, CancellationToken ct = default)
        {
            var key = $"thread_id({gameId}, {modId})";

            if (!_cache.TryGetValue(key, _jsonSerializer, out ThreadViewModel? cacheEntry))
            {
                var gameDomain = (await _nexusModsGameQueries.GetAsync(gameId, ct))?.DomainName ?? "ERROR";

                using var response = await _httpClientFactory.CreateClient("NexusMods").GetAsync(
                          $"{gameDomain}/mods/{modId}",
                          HttpCompletionOption.ResponseHeadersRead,
                          ct);

                var content = await response.Content.ReadAsStreamAsync(ct);

                var config   = Configuration.Default.WithDefaultLoader();
                var context  = BrowsingContext.New(config);
                var document = await context.OpenAsync(request => request.Content(content), ct);

                var element    = document.GetElementById("mod-page-tab-posts");
                var dataTarget = element?.Children[0].GetAttribute("data-target");
                var split      = dataTarget?.Split("thread_id=", StringSplitOptions.RemoveEmptyEntries);
                if (split?.Length > 1)
                {
                    var split2 = split[1].Split('&', StringSplitOptions.RemoveEmptyEntries);
                    if (split2.Length > 0 && uint.TryParse(split2[0], out var threadId))
                    {
                        cacheEntry = new ThreadViewModel(gameId, modId, threadId);
                    }
                }

                var cacheEntryOptions = new DistributedCacheEntryOptions().SetSize(1).SetAbsoluteExpiration(TimeSpan.FromHours(8));
                await _cache.SetAsync(key, cacheEntry, cacheEntryOptions, _jsonSerializer, ct);
            }

            return(cacheEntry);
        }
 public async Task <IActionResult> GetGameAsync([FromQuery, BindRequired] string gameDomain, [FromServices] IGameQueries gameQueries, CancellationToken ct) =>
 Ok(await gameQueries.GetAsync(gameDomain, ct));