-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoolNameAPI.cs
59 lines (54 loc) · 2.29 KB
/
CoolNameAPI.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Windows.UI.Xaml.Controls;
namespace Cool_Text_Generator
{
class CoolNameAPI
{
Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();
const string BaseURI = "https://cool-name-api.glitch.me/";
const string CoolServiceURI = "coolify?name=";
const string UncoolServiceURI = "uncoolify?name=";
public async void CoolifyAsync(string s, ObservableCollection<CoolName> coolNames, ProgressBar progbar)
{
Uri coolURI = new Uri(BaseURI + CoolServiceURI + s);
//List<CoolName> coolNames = new List<CoolName>();
Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
string httpResponseBody = "";
try
{
//Send the GET request
httpResponse = await httpClient.GetAsync(coolURI);
httpResponse.EnsureSuccessStatusCode();
httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
JObject jsonObject = JObject.Parse(httpResponseBody);
coolNames.Clear();
foreach (var x in jsonObject)
{
coolNames.Add(new CoolName(x.Key, x.Value.ToString()));
}
progbar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
catch (Exception ex)
{
coolNames.Clear();
coolNames.Add(new CoolName("Error", "Maybe the remote server's down 😭"));
coolNames.Add(new CoolName("Error", "Check if https://cool-name-api.glitch.me/ is working."));
coolNames.Add(new CoolName("Error", "Check your internet connection"));
coolNames.Add(new CoolName("Error", ex.Message.ToString()));
progbar.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
}
public void Uncoolify(string s)
{
Uri uncoolURI = new Uri(BaseURI + UncoolServiceURI + s);
System.Diagnostics.Debug.WriteLine(uncoolURI);
}
}
}