OpenTDB-Wrapper 1.5.0

There is a newer version of this package available.
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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenTDB-Wrapper" Version="1.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenTDB-Wrapper --version 1.5.0                
#r "nuget: OpenTDB-Wrapper, 1.5.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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; }
}
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
1.9.1 106 5/16/2024
1.8.1 131 4/3/2024
1.5.0 125 2/5/2024
1.0.0 117 2/3/2024
0.0.1 112 2/1/2024

Add GetCategoryQuestionTotalsAsync() and GetGlobalQuestionTotalsAsync().