/** Public methods **/
        public QuestionsResponse RequestQuestions(uint amount,
                                                  QuestionDifficulty difficulty = QuestionDifficulty.Undefined,
                                                  QuestionType type             = QuestionType.Undefined,
                                                  ResponseEncoding encoding     = ResponseEncoding.Undefined,
                                                  uint categoryID     = 0,
                                                  string sessionToken = "")
        {
            string Url = "https://opentdb.com/api.php?amount=" + amount;

            if (categoryID != 0)
            {
                Url += "&category=" + categoryID;
            }

            if (difficulty != QuestionDifficulty.Undefined)
            {
                Url += "&difficulty=" + difficulty.ToString().ToLower();
            }

            if (type != QuestionType.Undefined)
            {
                Url += "&type=" + type.ToString().ToLower();
            }

            if (encoding != ResponseEncoding.Undefined)
            {
                Url += "&encode=" + encoding.ToString();
            }

            if (sessionToken != "")
            {
                Url += "&token=" + sessionToken;
            }

            string JsonString = SendAPIRequest(Url);

            return(JsonConvert.DeserializeObject <QuestionsResponse>(JsonString));
        }
Exemplo n.º 2
0
        /// <summary>
        ///		Retrieves random <see cref="TriviaQuestion"/>.
        /// </summary>
        /// <param name="amount">
        ///		 Amount of questions to be retrieved.
        /// </param>
        /// <param name="category">
        ///		Category of the questions to be retrieved.
        /// </param>
        /// <param name="type">
        ///		Type of the questions to be retrieved.
        /// </param>
        /// <param name="difficulty">
        ///		 Difficulty of the questions to be retrieved.
        /// </param>
        /// <param name="encoding">
        ///		 Encoding of the API response to be used.
        /// </param>
        /// <param name="sessionToken">
        ///		Session token to be used.
        /// </param>
        /// <returns>
        ///		Array of <see cref="TriviaQuestion"/> based on specified parameters.
        /// </returns>
        /// <exception cref="OpenTriviaException">
        ///		Unexpected error occured.
        /// </exception>
        /// <exception cref="HttpRequestException">
        ///		The request failed due to an underlying issue such as network connectivity, DNS
        ///     failure, server certificate validation or timeout.
        /// </exception>
        /// <exception cref="JsonException">
        ///		The JSON is invalid.
        /// </exception>
        public async Task <TriviaQuestion[]> GetQuestionAsync(byte amount = 10, Category category = Category.Any, TriviaType type = TriviaType.Any, Difficulty difficulty = Difficulty.Any, ResponseEncoding encoding = ResponseEncoding.Default, string sessionToken = "")
        {
            if (amount <= 0)
            {
                amount = 1;
            }
            else if (amount > 50)             // hard limit
            {
                amount = 50;
            }

            var url = new StringBuilder(this._BaseApiUrl + $"amount={ amount }");

            if (category != Category.Any)
            {
                url.Append($"&category={ (byte)category }");
            }

            if (difficulty != Difficulty.Any)
            {
                url.Append($"&difficulty={ difficulty.ToString().ToLower() }");
            }

            if (type != TriviaType.Any)
            {
                url.Append($"&type={ type.ToString().ToLower() }");
            }

            if (encoding != ResponseEncoding.Default)
            {
                url.Append($"&encode={ encoding.ToString().ToLower() }");
            }

            if (sessionToken != "")
            {
                url.Append($"&token={ sessionToken }");
            }
            else
            {
                if (this._SessionToken != "")
                {
                    url.Append($"&token={ this._SessionToken }");
                }
            }

            using (var doc = await this.GetJsonResponseAsync <JsonDocument>(url.ToString()))
            {
                var responseCode = doc.RootElement.GetProperty("response_code").GetByte();

                if (responseCode != 0)
                {
                    throw new OpenTriviaException(this.ResponseError(responseCode));
                }

                var jsonArray = doc.RootElement.GetProperty("results");

                var questions = new List <TriviaQuestion>();

                if (encoding == ResponseEncoding.Default)
                {
                    foreach (var item in jsonArray.EnumerateArray())
                    {
                        questions.Add(this.ReadTriviaQuestion(item));
                    }
                }
                else if (encoding == ResponseEncoding.Url3986)
                {
                    foreach (var item in jsonArray.EnumerateArray())
                    {
                        questions.Add(this.ReadTriviaQuestionURL(item));
                    }
                }
                else
                {
                    foreach (var item in jsonArray.EnumerateArray())
                    {
                        questions.Add(this.ReadTriviaQuestionBase64(item));
                    }
                }

                return(questions.ToArray());
            }
        }