Beispiel #1
0
        public static string GenerateApiUrl(uint amount,
                                            QuestionCategory category     = QuestionCategory.Any,
                                            QuestionDifficulty difficulty = QuestionDifficulty.Any,
                                            QuestionType type             = QuestionType.Any,
                                            ResponseEncoding encoding     = ResponseEncoding.Default,
                                            string sessionToken           = "")
        {
            if (amount < 1 || amount > 50)
            {
                throw new ArgumentOutOfRangeException(nameof(amount), amount, "Amount must be between 1 and 50.");
            }

            string query = $"amount={amount}";

            if (category != QuestionCategory.Any)
            {
                query += $"&category={category:D}";
            }

            if (difficulty != QuestionDifficulty.Any)
            {
                query += $"&difficulty={difficulty.ToString().ToLowerInvariant()}";
            }

            if (type != QuestionType.Any)
            {
                query += $"&type={type.ToString().ToLowerInvariant()}";
            }

            if (encoding != ResponseEncoding.Default)
            {
                query += $"&encode={encoding}";
            }

            if (!string.IsNullOrEmpty(sessionToken))
            {
                query += $"&token={sessionToken}";
            }

            return($"{ApiEndpoint}?{query}");
        }
        /** 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));
        }
        public static async Task <IReadOnlyList <QuizQuestion>?> GetQuestionsAsync(int category, int amount, QuestionDifficulty difficulty)
        {
            if (category < 0)
            {
                return(null);
            }

            if (amount is < 1 or > 20)
            {
                amount = 10;
            }

            QuizData?data = null;
            string   url  = $"{ApiUrl}/api.php?amount={amount}&category={category}&difficulty={difficulty.ToString().ToLower()}&type=multiple&encode=url3986";

            try {
                string response = await _http.GetStringAsync(url).ConfigureAwait(false);

                data = JsonConvert.DeserializeObject <QuizData>(response);
            } catch (Exception e) {
                Log.Error(e, "Failed to fetch {QuizQuestionAmount} quiz questions from category {QuizCategoryId}", amount, category);
            }

            if (data?.ResponseCode == 0)
            {
                IEnumerable <QuizQuestion> questions = data.Questions.Select(q => {
                    q.Content          = WebUtility.UrlDecode(q.Content);
                    q.Category         = WebUtility.UrlDecode(q.Category);
                    q.CorrectAnswer    = WebUtility.UrlDecode(q.CorrectAnswer);
                    q.Difficulty       = difficulty;
                    q.IncorrectAnswers = q.IncorrectAnswers.Select(ans => WebUtility.UrlDecode(ans)).ToList();
                    return(q);
                });
                return(questions.ToList().AsReadOnly());
            }
            else
            {
                return(null);
            }
        }