1+ using System ;
2+ using System . Net ;
3+ using System . Text ;
4+ using ATLauncherAPI . apiobjects ;
5+ using Newtonsoft . Json ;
6+
7+ namespace ATLauncherAPI
8+ {
9+
10+ public class ATLauncherAPIClient
11+ {
12+ /* Private API Variables */
13+ private WebClient client = new WebClient ( ) { Encoding = Encoding . UTF8 } ;
14+ private String apiurl = "https://api.atlauncher.com/v1/" ;
15+ private Logger logger = Logger . getLogger ( "AT-API" ) ;
16+
17+ /***
18+ * Get info about a modpack
19+ * @param safename - The name of the modpack to search for (Case Sensitive and NO SPACES ALLOWED)
20+ * @return
21+ */
22+ public Optional < PackResult > getPackInfo ( string safename )
23+ {
24+ try
25+ {
26+ client . Headers . Add ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ) ;
27+ String response = client . DownloadString ( apiurl + "pack/" + safename + "/" ) ;
28+
29+ if ( ! string . IsNullOrEmpty ( response ) )
30+ {
31+ PackResult packResult = JsonConvert . DeserializeObject < PackResult > ( response ) ;
32+ return new Optional < PackResult > ( packResult ) ;
33+ }
34+ }
35+ catch ( Exception ex )
36+ {
37+ logger . error ( ex . Message ) ;
38+ }
39+
40+ return new Optional < PackResult > ( ) ;
41+ }
42+
43+ /***
44+ * Get basic information about a single version of the modpack
45+ * @param safename - The name of the modpack to search for (Case Sensitive and NO SPACES ALLOWED)
46+ * @param version - Version to lookup (Use latest to get the newest version)
47+ * @return
48+ */
49+ public Optional < PackVersionResult > getPackVersion ( String safename , String version ) {
50+ try {
51+ client . Headers . Add ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ) ;
52+ String response = client . DownloadString ( apiurl + "pack/" + safename + "/" + version + "/" ) ;
53+
54+ if ( ! string . IsNullOrEmpty ( response ) )
55+ {
56+ PackVersionResult packVersionResult = JsonConvert . DeserializeObject < PackVersionResult > ( response ) ;
57+ return new Optional < PackVersionResult > ( packVersionResult ) ;
58+ } else {
59+ throw new Exception ( "Could not retrieve result from API" ) ;
60+ }
61+ } catch ( Exception ex ) {
62+ logger . error ( ex . Message ) ;
63+ }
64+
65+ return new Optional < PackVersionResult > ( ) ;
66+ }
67+
68+ /***
69+ * Get a collection of basic information of all published modpacks
70+ * @return
71+ */
72+ public Optional < SimplePackResult > getSimplePackInfo ( ) {
73+
74+ try {
75+ client . Headers . Add ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ) ;
76+ String response = client . DownloadString ( apiurl + "packs/simple/" ) ;
77+
78+ if ( ! string . IsNullOrEmpty ( response ) )
79+ {
80+ SimplePackResult simplePackResult = JsonConvert . DeserializeObject < SimplePackResult > ( response ) ;
81+ return new Optional < SimplePackResult > ( simplePackResult ) ;
82+ } else {
83+ throw new Exception ( "Could not retrieve result from API" ) ;
84+ }
85+ } catch ( Exception ex ) {
86+ logger . error ( ex . Message ) ;
87+ }
88+
89+ return new Optional < SimplePackResult > ( ) ;
90+ }
91+
92+ /***
93+ * Gets the full info about ALL published modpacks
94+ * @return
95+ */
96+ public Optional < PackArrayResult > getAllPacks ( ) {
97+
98+ try {
99+ client . Headers . Add ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ) ;
100+ String response = client . DownloadString ( apiurl + "packs/full/all/" ) ;
101+
102+ if ( ! string . IsNullOrEmpty ( response ) )
103+ {
104+ PackArrayResult packArrayResult = JsonConvert . DeserializeObject < PackArrayResult > ( response ) ;
105+ return new Optional < PackArrayResult > ( packArrayResult ) ;
106+ } else {
107+ throw new Exception ( "Could not retrieve result from API" ) ;
108+ }
109+ } catch ( Exception ex ) {
110+ logger . error ( ex . Message ) ;
111+ }
112+
113+ return new Optional < PackArrayResult > ( ) ;
114+ }
115+
116+ /***
117+ * Gets the full info about ALL published modpacks marked as public
118+ * @return
119+ */
120+ public Optional < PackArrayResult > getPublicPacks ( ) {
121+
122+ try {
123+ client . Headers . Add ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ) ;
124+ String response = client . DownloadString ( apiurl + "packs/full/public/" ) ;
125+
126+ if ( ! string . IsNullOrEmpty ( response ) )
127+ {
128+ PackArrayResult packArrayResult = JsonConvert . DeserializeObject < PackArrayResult > ( response ) ;
129+ return new Optional < PackArrayResult > ( packArrayResult ) ;
130+ } else {
131+ throw new Exception ( "Could not retrieve result from API" ) ;
132+ }
133+ } catch ( Exception ex ) {
134+ logger . error ( ex . Message ) ;
135+ }
136+
137+ return new Optional < PackArrayResult > ( ) ;
138+ }
139+
140+ /***
141+ * Gets the full info about ALL published modpacks marked as semi-public
142+ * @return
143+ */
144+ public Optional < PackArrayResult > getSemiPublicPacks ( ) {
145+ try {
146+ client . Headers . Add ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ) ;
147+ String response = client . DownloadString ( apiurl + "packs/full/semipublic/" ) ;
148+
149+ if ( ! string . IsNullOrEmpty ( response ) )
150+ {
151+ PackArrayResult packArrayResult = JsonConvert . DeserializeObject < PackArrayResult > ( response ) ;
152+ return new Optional < PackArrayResult > ( packArrayResult ) ;
153+ } else {
154+ throw new Exception ( "Could not retrieve result from API" ) ;
155+ }
156+ } catch ( Exception ex ) {
157+ logger . error ( ex . Message ) ;
158+ }
159+
160+ return new Optional < PackArrayResult > ( ) ;
161+ }
162+
163+ /***
164+ * Gets the full info about ALL published modpacks marked as private
165+ * @return
166+ */
167+ public Optional < PackArrayResult > getPrivatePacks ( ) {
168+ try {
169+ client . Headers . Add ( "user-agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)" ) ;
170+ String response = client . DownloadString ( apiurl + "packs/full/private/" ) ;
171+
172+ if ( ! string . IsNullOrEmpty ( response ) )
173+ {
174+ PackArrayResult packArrayResult = JsonConvert . DeserializeObject < PackArrayResult > ( response ) ;
175+ return new Optional < PackArrayResult > ( packArrayResult ) ;
176+ } else {
177+ throw new Exception ( "Could not retrieve result from API" ) ;
178+ }
179+ } catch ( Exception ex ) {
180+ logger . error ( ex . Message ) ;
181+ }
182+
183+ return new Optional < PackArrayResult > ( ) ;
184+ }
185+ }
186+ }
0 commit comments