-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainPage.xaml.cs
109 lines (84 loc) · 3.57 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using SearchProject.Models;
using System;
using System.Threading.Tasks;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace SearchProject
{
public sealed partial class MainPage : Page
{
public RelatedTopicsViewModel ViewModel { get; set; }
public MainPage()
{
this.InitializeComponent();
this.ViewModel = new RelatedTopicsViewModel();
}
private void ExecuteSearch(string searchString)
{
var t = Task.Run(() => TryGetSearchAsync(searchString));
t.Wait();
var relatedTopicsVM = new RelatedTopicsViewModel(t.Result);
this.ResultsList.ItemsSource = relatedTopicsVM.RelatedTopics;
this.TopicsList.ItemsSource = relatedTopicsVM.Topics;
}
private static bool IsEnterPressed()
{
var enterState = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Enter);
return (enterState & CoreVirtualKeyStates.Down) == CoreVirtualKeyStates.Down;
}
private void ContentPanel_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (IsEnterPressed())
{
ExecuteSearch(searchInput.Text);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ExecuteSearch(searchInput.Text);
}
/* This approach uses view modles backed by POCO
* It takes the results off of the response and
* saerializes them to the view models
*/
private async Task<SearchResults> TryGetSearchAsync(string query)
{
//Create an HTTP client object
Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
//Add a user-agent header to the GET request.
var headers = httpClient.DefaultRequestHeaders;
//The safe way to add a header value is to use the TryParseAdd method and verify the return value is true,
//especially if the header value is coming from user input.
if (!headers.Accept.TryParseAdd("application/json"))
{
throw new Exception("Invalid header value");
}
var baseUri = new UriBuilder("http://api.duckduckgo.com");
string queryToAppend = $"q={query}&format=json&pretty=1&no_html=1&skip_disambig=1";
baseUri.Query = queryToAppend;
//Send the GET request asynchronously and retrieve the response as a string.
Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
string httpResponseBody = "";
SearchResults searchResults = new SearchResults();
try
{
//Send the GET request
httpResponse = await httpClient.GetAsync(baseUri.Uri);
httpResponse.EnsureSuccessStatusCode();
httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
searchResults = Newtonsoft.Json.JsonConvert.DeserializeObject<SearchResults>(httpResponseBody);
}
catch (Exception ex)
{
httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
}
finally
{
httpClient.Dispose();
}
return searchResults;
}
}
}