internal static IEnumerable <String> ConvertFlagsToString(String method, VndbFlags flags) { var type = typeof(VndbFlags); var typeInfo = type.GetTypeInfo(); foreach (Enum value in Enum.GetValues(type)) { if (!flags.HasFlag(value)) { continue; } var fi = typeInfo.GetDeclaredField(value.ToString()); var identity = fi.GetCustomAttribute <FlagIdentityAttribute>(); if (identity == null) { continue; } if ((method == Constants.GetStaffCommand || method == Constants.GetCharacterCommand) && (VndbFlags)value == VndbFlags.VisualNovels) { yield return($"{identity.Identity}s"); // Ugly hack to work around *two* vn(s) flags } else { yield return(identity.Identity); } } }
/// <summary> /// Validate Flags by Method /// </summary> /// <param name="method">The method to check the flags for</param> /// <param name="flags">The flags being checked</param> /// <param name="invalidFlags">Any invalid flags found within the flags provided</param> /// <returns>True if there are no invalid flags, otherwise false</returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static Boolean ValidateFlagsByMethod(String method, VndbFlags flags, out VndbFlags invalidFlags) { VndbFlags fullFlags; switch (method) { case Constants.GetVisualNovelCommand: fullFlags = VndbFlags.FullVisualNovel; break; case Constants.GetReleaseCommand: fullFlags = VndbFlags.FullRelease; break; case Constants.GetProducerCommand: fullFlags = VndbFlags.FullProducer; break; case Constants.GetCharacterCommand: fullFlags = VndbFlags.FullCharacter; break; case Constants.GetUserCommand: fullFlags = VndbFlags.FullUser; break; case Constants.GetVotelistCommand: fullFlags = VndbFlags.FullVotelist; break; case Constants.GetVisualNovelListCommand: fullFlags = VndbFlags.FullVisualNovelList; break; case Constants.GetWishlistCommand: fullFlags = VndbFlags.FullWishlist; break; case Constants.GetStaffCommand: fullFlags = VndbFlags.FullStaff; break; case Constants.GetUserListCommand: fullFlags = VndbFlags.FullUserList; break; case Constants.GetUserListLabelsCommand: fullFlags = VndbFlags.FullUserListLabels; break; default: throw new ArgumentOutOfRangeException(nameof(method)); } invalidFlags = flags & ~fullFlags; return(invalidFlags == 0); }
// 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)); }
/// <summary> /// Returns the User Information as a C# Object /// </summary> /// <param name="filters">The IFilter for the request</param> /// <param name="flags">The flags for the request</param> /// <param name="options">The IRequestOptions for the request</param> /// <returns>The results from Vndb</returns> public async Task <VndbResponse <User> > GetUserAsync(IFilter filters, VndbFlags flags = VndbFlags.Basic, IRequestOptions options = null) => await this.GetInternalAsync <VndbResponse <User> >(Constants.GetUserCommand, filters, flags, options) .ConfigureAwait(false);
private void InvalidFlagsCallback(String method, VndbFlags providedFlags, VndbFlags invalidFlags) { Console.WriteLine($"Attempted to use method \"{method}\" with flags {providedFlags}, but {invalidFlags} are not permitted on that method."); }
// Should likely be in a unique class...but meh internal static String AsString(this VndbFlags flags, String method) => String.Join(",", VndbUtils.ConvertFlagsToString(method, flags).Distinct());