Advertisement
Guest User

Untitled

a guest
Jul 26th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net.Http;
  7. using System.Net.Http.Headers;
  8.  
  9. namespace SoftEng
  10. {
  11. public static class ApiHelper
  12. {
  13. public static HttpClient ApiClient { get; set; }
  14.  
  15. public static void InitializeClient()
  16. {
  17. ApiClient = new HttpClient();
  18. ApiClient.DefaultRequestHeaders.Accept.Clear();
  19. ApiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  20. }
  21. }
  22. }
  23.  
  24.  
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Linq;
  28. using System.Text;
  29. using System.Threading.Tasks;
  30.  
  31. namespace SoftEng
  32. {
  33. public class QuizModel
  34. {
  35. public List<Results> QuizResults { get; set; }
  36.  
  37. }
  38. }
  39.  
  40.  
  41. using System;
  42. using System.Collections.Generic;
  43. using System.Linq;
  44. using System.Text;
  45. using System.Threading.Tasks;
  46.  
  47. namespace SoftEng
  48. {
  49. public class Results
  50. {
  51. public string Question { get; set; }
  52.  
  53. public string correct_answer { get; set; }
  54.  
  55. public List<string> incorrect_answers { get; set; }
  56.  
  57. }
  58. }
  59.  
  60.  
  61. using System;
  62. using System.Collections.Generic;
  63. using System.Linq;
  64. using System.Text;
  65. using System.Threading.Tasks;
  66. using System.Net.Http;
  67.  
  68.  
  69. namespace SoftEng
  70. {
  71. public class ApiProcessor
  72. {
  73. public static async Task<List<Results>> LoadQuiz(int category, string difficulty, string type)
  74. {
  75. string url = $"https://opentdb.com/api.php?amount=5&category={ category }&difficulty={ difficulty }&type={ type }";
  76.  
  77. using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
  78. {
  79. if (response.IsSuccessStatusCode)
  80. {
  81. QuizModel quiz = await response.Content.ReadAsAsync<QuizModel>();
  82.  
  83. return quiz.QuizResults;
  84. }
  85. else
  86. {
  87. throw new Exception(response.ReasonPhrase);
  88. }
  89. }
  90. }
  91. }
  92. }
  93.  
  94.  
  95. using System;
  96. using System.Collections.Generic;
  97. using System.ComponentModel;
  98. using System.Data;
  99. using System.Drawing;
  100. using System.Linq;
  101. using System.Text;
  102. using System.Threading.Tasks;
  103. using System.Windows.Forms;
  104.  
  105. namespace SoftEng
  106. {
  107. public partial class Form1 : Form
  108. {
  109. public Form1()
  110. {
  111. InitializeComponent();
  112. ApiHelper.InitializeClient();
  113. }
  114.  
  115. private async void submitButton_Click(object sender, EventArgs e)
  116. {
  117. var q = await ApiProcessor.LoadQuiz(9, "Easy", "Multiple");
  118. foreach (Results a in q)
  119. {
  120. MessageBox.Show(a.Question);
  121. MessageBox.Show(a.correct_answer);
  122. }
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement