OpenTDB-Wrapper
1.5.0
See the version list below for details.
dotnet add package OpenTDB-Wrapper --version 1.5.0
NuGet\Install-Package OpenTDB-Wrapper -Version 1.5.0
<PackageReference Include="OpenTDB-Wrapper" Version="1.5.0" />
paket add OpenTDB-Wrapper --version 1.5.0
#r "nuget: OpenTDB-Wrapper, 1.5.0"
// Install OpenTDB-Wrapper as a Cake Addin #addin nuget:?package=OpenTDB-Wrapper&version=1.5.0 // Install OpenTDB-Wrapper as a Cake Tool #tool nuget:?package=OpenTDB-Wrapper&version=1.5.0
OpenTDB-Wrapper
An async C# wrapper for the Open Trivia DB API.
For a more detailed README checkout the GitHub.
Usage
Setup
As a good rule of thumb, add these using
statements.
using OpenTDB; // For fetching questions from the API
using OpenTDB.Enumerators; // For specifying optional paramters
using OpenTDB.Exceptions; // For handling OpenTDB exceptions
using OpenTDB.Models; // For interacting with the Question class
Create an instance of OpenTDB
.
OpenTDB.OpenTDB openTDB = new(); // This contains an HttpClient and it is good practice to not duplicate it.
Getting Questions
Use GetQuestionsAsync()
to get a list of questions. This method has default values of Category.Any
, Difficulty.Any
, and QuestionType.Any
.
await openTDB.GetQuestionsAsync(10);
// returns a List<Question> with a Count of 10.
Here is a more complicated example. If you wanted to get 10 Nature questions, that were of easy difficulty, and in the form of multiple-choice questions, it would look like so:
await openTDB.GetQuestionsAsync(10, Category.Nature, Difficulty.Easy, QuestionType.MultipleChoice);
// returns a List<Question> with a Count of 10.
Here is a more complicated example. If you wanted to get 10 Nature questions, that were of easy difficulty, in the form of a multiple-choice questions, and had legacy URL encoding it would look like so:
await openTDB.GetQuestionsWithEncodingAsync(10, Category.Nature, Difficulty.Easy, QuestionType.MultipleChoice, Encoding.LegacyURL);
// returns a List<Question> with a Count of 10.
Question
The Question
class looks like this:
public class Question
{
public string Type { get; set; }
public string Difficulty { get; set; }
public string Category { get; set; }
public string QuestionTitle { get; set; }
public string CorrectAnswer { get; set; }
public string[] IncorrectAnswers { get; set; }
}
Category Question Count Lookup
If you need to determine how many questions are in a specific category you can do so with GetCategoryQuestionTotalsAsync()
This can be done with a specific Category
:
await GetCategoryQuestionTotalsAsync(Category.Nature);
// returns a CategoryCount object.
Or with an integer representing the Category
ID:
await GetCategoryQuestionTotalsAsync(17);
// returns a CategoryCount object.
CategoryCount
The CategoryCount
class looks like this:
public class CategoryCount
{
public int CategoryId { get; set; }
public int TotalQuestions { get; set; }
public int TotalEasyQuestions { get; set; }
public int TotalMediumQuestions { get; set; }
public int TotalHardQuestions { get; set; }
}
Global Question Count Lookup
If you need to find out things like how many questions there are in the entire database you should use GetGlobalQuestionTotalsAsync()
.
await GetGlobalQuestionTotalsAsync();
// returns a GlobalCount object.
GlobalCount
The GlobalCount
class looks like this:
public class GlobalCount
{
public int TotalQuestions { get; set; }
public int TotalPendingQuestions { get; set; }
public int TotalVerifiedQuestions { get; set; }
public int TotalRejectedQuestions { get; set; }
public List<GlobalCategoryCount> Categories { get; set; }
}
Legal Stuff
- Open Trivia DB API uses Creative Commons Attribution-ShareAlike 4.0 International License.
- OpenTDB-Wrapper uses GPL-3.0 License and is not affiliated with the Open Trivia Database.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Add GetCategoryQuestionTotalsAsync() and GetGlobalQuestionTotalsAsync().