OpenTDB-Wrapper
1.8.1
See the version list below for details.
dotnet add package OpenTDB-Wrapper --version 1.8.1
NuGet\Install-Package OpenTDB-Wrapper -Version 1.8.1
<PackageReference Include="OpenTDB-Wrapper" Version="1.8.1" />
paket add OpenTDB-Wrapper --version 1.8.1
#r "nuget: OpenTDB-Wrapper, 1.8.1"
// Install OpenTDB-Wrapper as a Cake Addin #addin nuget:?package=OpenTDB-Wrapper&version=1.8.1 // Install OpenTDB-Wrapper as a Cake Tool #tool nuget:?package=OpenTDB-Wrapper&version=1.8.1
OpenTDB-Wrapper
An async C# wrapper for the Open Trivia DB API.
For a more detailed README checkout the GitHub.
Add to Project
Via dotnet
:
dotnet add package OpenTDB-Wrapper --version 1.8.0
Via PackageReference
in your .csproj
file:
<PackageReference Include="OpenTDB-Wrapper" Version="1.8.0" />
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();
If you want to pass in your own HttpClient
you can do it like so:
HttpClient httpClient = new();
OpenTDB.OpenTDB openTDB = new(httpClient);
Using Tokens
The Open Trivia Database API provides session tokens for ensuring no duplicate questions are retrieved. You can start usage of a token like so:
await openTDB.InitializeTokenAsync();
// Initializes the session Token.
After one of the following happens you will need to call ResetTokenAsync()
, this will get a new session token:
- After 6 hours your session token is automatically deleted by Open Trivia Database.
- If you ever exhaust all questions in the database.
You will know for certain that you should call this method if you get one of the following exceptions, these indicate your session token is dead:
Code 3: Token Not Found | Session Token does not exist.
Code 4: Token Empty | Session Token has returned all possible questions for the specified query. Resetting the Token is necessary.
await openTDB.ResetTokenAsync();
// Sets the Token to a newly created one.
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.
Added support for session tokens and user-given HttpClients.