internal static async Task <IEnumerable <Vote> > GetAndParseVotesAsync(VoteDumpVersion version) { var url = version == VoteDumpVersion.One ? Constants.VotesDump : Constants.VotesDump2; Debug.WriteLine($"Requesting Votes Dump via {url}"); // .Net Core removed WebClient and Http/WebRequests, so we need to use HttpClient. var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)); // Manually add the headers every request rather then using the default headers, // incase the client was rebuilt with a new name / version mid-application session request.Headers.Add("User-Agent", $"{VndbUtils.ClientName} (v{VndbUtils.ClientVersion})"); var response = await VndbUtils.HttpClient.SendAsync(request); response.EnsureSuccessStatusCode(); // Ensure we got data var gzipStream = await response.Content.ReadAsStreamAsync(); var rawContents = await VndbUtils.UnGzip(gzipStream); response.Dispose(); request.Dispose(); var results = new List <Vote>(); var votes = rawContents.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); var expectedValues = version == VoteDumpVersion.One ? 3 : 4; // Resharper "Loop can be converted to LINQ-expression won't work due to inline "out var" declaration foreach (var vote in votes) { var values = vote.Split(new [] { ' ' }, expectedValues, StringSplitOptions.RemoveEmptyEntries); if (values.Length != expectedValues) { continue; } SimpleDate date = null; if (!UInt32.TryParse(values[0], out var vnId) || !UInt32.TryParse(values[1], out var uid) || !Byte.TryParse(values[2], out var value)) { continue; } if (version == VoteDumpVersion.Two && (date = (SimpleDate)SimpleDateConverter.ParseString(values[3])) == null) { continue; } results.Add(new Vote(version, vnId, uid, value, date)); } return(results); }
// todo: Move this to Vndb.Helper.cs /// <summary> /// Method for processing the Get Methods /// </summary> /// <param name="method">Which API method to use</param> /// <param name="filter">The IFilter for the request</param> /// <param name="flags">The flags for the request</param> /// <param name="options">The IRequestOptions for the request</param> /// <typeparam name="T">The type of response expected</typeparam> /// <returns>The results from Vndb</returns> protected async Task <T> GetInternalAsync <T>(String method, IFilter filter, VndbFlags flags, IRequestOptions options = null) where T : class { // Need a way to communicate to the end user that these null values are not from the API? if (this.CheckFlags && !VndbUtils.ValidateFlagsByMethod(method, flags, out var invalidFlags)) { this._invalidFlags?.Invoke(method, flags, invalidFlags); this.LastError = new LibraryError("CheckFlags is enabled and VndbSharp detected invalid flags"); return(null); } if (!filter.IsFilterValid()) { this.LastError = new LibraryError($"A filter was not considered valid. The filter is of the type {filter.GetType().Name}"); return(null); } var requestData = this.FormatRequest($"{method} {flags.AsString(method)} ({filter})", options, false); return(await this.SendGetRequestInternalAsync <T>(requestData).ConfigureAwait(false)); }
internal static async Task <T> GetDumpAsync <T>(String url) where T : class { // .Net Core removed WebClient and Http/WebRequests, so we need to use HttpClient. var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url)); // Manually add the headers every request rather then using the default headers, // incase the client was rebuilt with a new name / version mid-application session request.Headers.Add("User-Agent", $"{VndbUtils.ClientName} (v{VndbUtils.ClientVersion})"); var response = await VndbUtils.HttpClient.SendAsync(request); response.EnsureSuccessStatusCode(); // Ensure we got data var gzipStream = await response.Content.ReadAsStreamAsync(); var rawContents = await VndbUtils.UnGzip(gzipStream); return(JsonConvert.DeserializeObject <T>(rawContents, new JsonSerializerSettings { ContractResolver = VndbContractResolver.Instance, })); }
/// <summary> /// Downloads and PArses the Votes Dump from Vndb /// </summary> /// <param name="version">The version of the Votes Dump to grab</param> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="Vote"/></returns> /// <exception cref="HttpRequestException">Occurs when the votes.gz file returns a non-success status</exception> public static async Task <IEnumerable <Vote> > GetVotesDumpAsync(VoteDumpVersion version = VoteDumpVersion.Two) => await VndbUtils.GetAndParseVotesAsync(version).ConfigureAwait(false);
/// <summary> /// Downloads and Parses the Traits Dump from Vndb /// </summary> /// <returns>An <see cref="IEnumerable{T}"/> of <see cref="Trait"/></returns> /// <exception cref="HttpRequestException">Occurs when the traits.json.gz file returns a non-success status</exception> public static async Task <IEnumerable <Trait> > GetTraitsDumpAsync() => await VndbUtils.GetDumpAsync <IEnumerable <Trait> >(Constants.TraitsDump).ConfigureAwait(false);