Table of Contents

Interface IOpenTriviaClient

Namespace
Tudormobile.OpenTrivia
Assembly
Tudormobile.OpenTrivia.dll

Defines methods for interacting with the Open Trivia Database API.

public interface IOpenTriviaClient
Extension Methods

Methods

Create(HttpClient, ILogger?, IApiDataSerializer?, bool, bool, ApiEncodingType?)

Creates and initializes an instance of the IOpenTriviaClient interface.

public static IOpenTriviaClient Create(HttpClient httpClient, ILogger? logger = null, IApiDataSerializer? serializer = null, bool manageRateLimit = false, bool autoDecode = false, ApiEncodingType? encodingType = null)

Parameters

httpClient HttpClient

The HttpClient class to use.

logger ILogger

Optional logger instance for logging diagnostic information. If null, a NullLogger will be used.

serializer IApiDataSerializer

Optional serializer instance for serializing and deserializing API data. If null, the internal serializer will be used.

manageRateLimit bool

Automatically manage rate limits.

autoDecode bool

Automatically decode API responses.

encodingType ApiEncodingType?

The encoding type to use for API requests.

Returns

IOpenTriviaClient

Remarks

If an encodingType is provided, it will be used when not explicitly provided to method calls. If autoDecode is enabled, the client will attempt to decode API responses using the specified encoding type or the default encoding if none is specified. If autoDecode is disabled, the client will return raw API responses without decoding. When autoDecode is not enabled (default behavior), the client will return raw API responses without decoding, regardless of the specified encoding type. This allows you to specify the encoding and manage rate limits without automatically decoding responses, giving you more control over how you handle API data.

GetCategoriesAsync(CancellationToken)

Retrieves a list of all available trivia categories from the API.

Task<ApiResponse<List<TriviaCategory>>> GetCategoriesAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ApiResponse<List<TriviaCategory>>>

An ApiResponse<T> containing the list of categories.

GetQuestionCountAsync(TriviaCategory, CancellationToken)

Asynchronously retrieves the total number of questions available in the system for a specific category.

Task<ApiResponse<TriviaQuestionCount>> GetQuestionCountAsync(TriviaCategory category, CancellationToken cancellationToken = default)

Parameters

category TriviaCategory

The category to filter by.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ApiResponse<TriviaQuestionCount>>

An ApiResponse<T> containing the total count of questions for the specified category.

GetQuestionsAsync(int, TriviaCategory?, TriviaQuestionDifficulty?, TriviaQuestionType?, ApiEncodingType?, ApiSessionToken?, CancellationToken)

Retrieves trivia questions from the API with optional filtering.

Task<ApiResponse<List<TriviaQuestion>>> GetQuestionsAsync(int amount, TriviaCategory? category = null, TriviaQuestionDifficulty? difficulty = null, TriviaQuestionType? type = null, ApiEncodingType? encoding = null, ApiSessionToken? token = null, CancellationToken cancellationToken = default)

Parameters

amount int

The number of questions to retrieve (1-50).

category TriviaCategory

The category to filter by, or null for any category.

difficulty TriviaQuestionDifficulty?

The difficulty level to filter by, or null for any difficulty.

type TriviaQuestionType?

The question type to filter by, or null for any type.

encoding ApiEncodingType?

The encoding type for the response, or null for default encoding.

token ApiSessionToken

The session token to use, or null for no session.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ApiResponse<List<TriviaQuestion>>>

An ApiResponse<T> containing the list of trivia questions.

GetSessionTokenAsync(CancellationToken)

Retrieves a new session token from the API.

Task<ApiResponse<ApiSessionToken>> GetSessionTokenAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ApiResponse<ApiSessionToken>>

An ApiResponse<T> containing the session token.

ResetSessionTokenAsync(ApiSessionToken, CancellationToken)

Resets an existing session token, clearing its question history.

Task<ApiResponse<ApiSessionToken>> ResetSessionTokenAsync(ApiSessionToken token, CancellationToken cancellationToken = default)

Parameters

token ApiSessionToken

The session token to reset.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<ApiResponse<ApiSessionToken>>

An ApiResponse<T> containing the reset session token.