-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATLauncherAPIClient.cs
186 lines (160 loc) · 6.7 KB
/
ATLauncherAPIClient.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Net;
using System.Text;
using ATLauncherAPI.apiobjects;
using Newtonsoft.Json;
namespace ATLauncherAPI
{
public class ATLauncherAPIClient
{
/* Private API Variables */
private WebClient client = new WebClient() { Encoding = Encoding.UTF8 };
private String apiurl = "https://api.atlauncher.com/v1/";
private Logger logger = Logger.getLogger("AT-API");
/***
* Get info about a modpack
* @param safename - The name of the modpack to search for (Case Sensitive and NO SPACES ALLOWED)
* @return
*/
public Optional<PackResult> getPackInfo(string safename)
{
try
{
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
String response = client.DownloadString(apiurl + "pack/" + safename + "/");
if (!string.IsNullOrEmpty(response))
{
PackResult packResult = JsonConvert.DeserializeObject<PackResult>(response);
return new Optional<PackResult>(packResult);
}
}
catch (Exception ex)
{
logger.error(ex.Message);
}
return new Optional<PackResult>();
}
/***
* Get basic information about a single version of the modpack
* @param safename - The name of the modpack to search for (Case Sensitive and NO SPACES ALLOWED)
* @param version - Version to lookup (Use latest to get the newest version)
* @return
*/
public Optional<PackVersionResult> getPackVersion(String safename, String version) {
try {
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
String response = client.DownloadString(apiurl + "pack/" + safename + "/" + version + "/");
if (!string.IsNullOrEmpty(response))
{
PackVersionResult packVersionResult = JsonConvert.DeserializeObject<PackVersionResult>(response);
return new Optional<PackVersionResult>(packVersionResult);
} else {
throw new Exception("Could not retrieve result from API");
}
} catch (Exception ex) {
logger.error(ex.Message);
}
return new Optional<PackVersionResult>();
}
/***
* Get a collection of basic information of all published modpacks
* @return
*/
public Optional<SimplePackResult> getSimplePackInfo() {
try {
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
String response = client.DownloadString(apiurl + "packs/simple/");
if (!string.IsNullOrEmpty(response))
{
SimplePackResult simplePackResult = JsonConvert.DeserializeObject<SimplePackResult>(response);
return new Optional<SimplePackResult>(simplePackResult);
} else {
throw new Exception("Could not retrieve result from API");
}
} catch (Exception ex) {
logger.error(ex.Message);
}
return new Optional<SimplePackResult>();
}
/***
* Gets the full info about ALL published modpacks
* @return
*/
public Optional<PackArrayResult> getAllPacks() {
try {
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
String response = client.DownloadString(apiurl + "packs/full/all/");
if (!string.IsNullOrEmpty(response))
{
PackArrayResult packArrayResult = JsonConvert.DeserializeObject<PackArrayResult>(response);
return new Optional<PackArrayResult>(packArrayResult);
} else {
throw new Exception("Could not retrieve result from API");
}
} catch (Exception ex) {
logger.error(ex.Message);
}
return new Optional<PackArrayResult>();
}
/***
* Gets the full info about ALL published modpacks marked as public
* @return
*/
public Optional<PackArrayResult> getPublicPacks() {
try {
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
String response = client.DownloadString(apiurl + "packs/full/public/");
if (!string.IsNullOrEmpty(response))
{
PackArrayResult packArrayResult = JsonConvert.DeserializeObject<PackArrayResult>(response);
return new Optional<PackArrayResult>(packArrayResult);
} else {
throw new Exception("Could not retrieve result from API");
}
} catch (Exception ex) {
logger.error(ex.Message);
}
return new Optional<PackArrayResult>();
}
/***
* Gets the full info about ALL published modpacks marked as semi-public
* @return
*/
public Optional<PackArrayResult> getSemiPublicPacks() {
try {
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
String response = client.DownloadString(apiurl + "packs/full/semipublic/");
if (!string.IsNullOrEmpty(response))
{
PackArrayResult packArrayResult = JsonConvert.DeserializeObject<PackArrayResult>(response);
return new Optional<PackArrayResult>(packArrayResult);
} else {
throw new Exception("Could not retrieve result from API");
}
} catch (Exception ex) {
logger.error(ex.Message);
}
return new Optional<PackArrayResult>();
}
/***
* Gets the full info about ALL published modpacks marked as private
* @return
*/
public Optional<PackArrayResult> getPrivatePacks() {
try {
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
String response = client.DownloadString(apiurl + "packs/full/private/");
if (!string.IsNullOrEmpty(response))
{
PackArrayResult packArrayResult = JsonConvert.DeserializeObject<PackArrayResult>(response);
return new Optional<PackArrayResult>(packArrayResult);
} else {
throw new Exception("Could not retrieve result from API");
}
} catch (Exception ex) {
logger.error(ex.Message);
}
return new Optional<PackArrayResult>();
}
}
}