Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Http;
- using System.Net.Http.Headers;
- namespace SoftEng
- {
- public static class ApiHelper
- {
- public static HttpClient ApiClient { get; set; }
- public static void InitializeClient()
- {
- ApiClient = new HttpClient();
- ApiClient.DefaultRequestHeaders.Accept.Clear();
- ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SoftEng
- {
- public class QuizModel
- {
- public List<Results> QuizResults { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SoftEng
- {
- public class Results
- {
- public string Question { get; set; }
- public string correct_answer { get; set; }
- public List<string> incorrect_answers { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Http;
- namespace SoftEng
- {
- public class ApiProcessor
- {
- public static async Task<List<Results>> LoadQuiz(int category, string difficulty, string type)
- {
- string url = $"https://opentdb.com/api.php?amount=5&category={ category }&difficulty={ difficulty }&type={ type }";
- using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
- {
- if (response.IsSuccessStatusCode)
- {
- QuizModel quiz = await response.Content.ReadAsAsync<QuizModel>();
- return quiz.QuizResults;
- }
- else
- {
- throw new Exception(response.ReasonPhrase);
- }
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace SoftEng
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- ApiHelper.InitializeClient();
- }
- private async void submitButton_Click(object sender, EventArgs e)
- {
- var q = await ApiProcessor.LoadQuiz(9, "Easy", "Multiple");
- foreach (Results a in q)
- {
- MessageBox.Show(a.Question);
- MessageBox.Show(a.correct_answer);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement