Exemple #1
0
        public static async Task <GetPublishedFileDetailsResponse> GetPublishedFileDetailsAsync(HttpClient webClient, List <string> fileIds)
        {
            var parsedResponse = new GetPublishedFileDetailsResponse();

            if (fileIds.Count < 1)
            {
                return(parsedResponse);
            }
            CheckCache();
            if (cacheFile.Exists && (!cacheFile.LastWriteTime.ExpiredSince(10)))
            {
                foreach (var fileId in fileIds)
                {
                    var item = cache.FileDetails.FirstOrDefault(x => x.publishedfileid == fileId);
                    if (item != null)
                    {
                        parsedResponse.response.publishedfiledetails.Add(item);
                    }
                }
                if (parsedResponse.response.publishedfiledetails.Count >= fileIds.Count)
                {
                    return(parsedResponse);
                }
            }

            /*SteamRequest request = new SteamRequest("ISteamRemoteStorage/GetPublishedFileDetails/v1/");
             * request.AddParameter("itemcount", fileIds.Count);
             * request.AddParameter("publishedfileids", fileIds.ToArray());
             *          var response = steam.Execute(request);
             * Console.WriteLine(response.Content);
             */
            var values = new Dictionary <string, string> {
                { "itemcount", fileIds.Count.ToString() }
            };

            for (int i = 0; i < fileIds.Count; i++)
            {
                values.Add($"publishedfileids[{i}]", fileIds[i].ToString());
            }
            var content = new FormUrlEncodedContent(values);
            var url     = new Uri("https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1/");

            Console.WriteLine($"[Steam] POST to {url} with payload {content.ToJson(false)} and values {values.ToJson(false)}");
            var response = await webClient.PostAsync(url, content);

            var responseString = await response.Content.ReadAsStringAsync();

            try { parsedResponse = JsonConvert.DeserializeObject <GetPublishedFileDetailsResponse>(responseString); }
            catch (Exception ex) { Console.WriteLine($"[Steam] Could not deserialize response ({ex.Message})\n{responseString}"); } // {response.ReasonPhrase} ({response.StatusCode})\n

            foreach (var item in parsedResponse.response.publishedfiledetails)
            {
                cache.FileDetails.RemoveAll(x => x.publishedfileid == item.publishedfileid);
                cache.FileDetails.Add(CacheFileDetail.FromPublishedfiledetail(item));
            }
            File.WriteAllText(cacheFile.FullName, JsonConvert.SerializeObject(cache));
            return(parsedResponse);
        }
Exemple #2
0
        public async Task <GetPublishedFileDetailsResponse> GetPublishedFileDetailsAsync(IReadOnlyList <string> fileIds)
        {
            var parsedResponse = new GetPublishedFileDetailsResponse();

            if (fileIds.Count < 1)
            {
                return(parsedResponse);
            }
            if (this._cached)
            {
                this.CheckCache(ref this._publishedFileDetailsCached, this._publishedFileDetailsCacheFile);
                if (this._publishedFileDetailsCacheFile.Exists &&
                    (!this._publishedFileDetailsCacheFile.LastWriteTime.ExpiredSince(10)))
                {
                    foreach (var item in fileIds.Select(fileId => this._publishedFileDetailsCached.FirstOrDefault(x =>
                                                                                                                  $"{x.Response.PublishedFileId}" == fileId)).Where(item => item != null))
                    {
                        parsedResponse.Response.PublishedFileDetails.Add(item.Response);
                    }

                    if (parsedResponse.Response.PublishedFileDetails.Count >= fileIds.Count)
                    {
                        return(parsedResponse);
                    }
                }
            }

            var values = new Dictionary <string, string> {
                { "itemcount", fileIds.Count.ToString() }
            };

            for (var i = 0; i < fileIds.Count; i++)
            {
                values.Add($"publishedfileids[{i}]", fileIds[i]);
            }
            var content = new FormUrlEncodedContent(values);
            var url     = new Uri("https://api.steampowered.com/ISteamRemoteStorage/GetPublishedFileDetails/v1/");

            this.Log().Debug($"[Steam] POST to {url} with payload {content.ToJson(false)} and values {values.ToJson(false)}");
            var response = await this._httpClient.PostAsync(url, content).ConfigureAwait(false);

            var responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            try
            {
                parsedResponse = JsonConvert.DeserializeObject <GetPublishedFileDetailsResponse>(responseString);
            }
            catch (Exception ex)
            {
                this.Log().Error(ex, $"[Steam] Could not deserialize response: {responseString}");
            }

            if (!this._cached)
            {
                return(parsedResponse);
            }
            foreach (var item in parsedResponse.Response.PublishedFileDetails)
            {
                this._publishedFileDetailsCached.RemoveAll(x => x.Response.PublishedFileId == item.PublishedFileId);
                this._publishedFileDetailsCached.Add(CachedResponse <PublishedFileDetail> .FromResponse(item));
            }

            File.WriteAllText(this._publishedFileDetailsCacheFile.FullName,
                              JsonConvert.SerializeObject(this._publishedFileDetailsCached));

            return(parsedResponse);
        }