Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hypherionmc committed Mar 22, 2021
0 parents commit 5b07311
Show file tree
Hide file tree
Showing 14 changed files with 687 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
bin
obj
12 changes: 12 additions & 0 deletions ATLauncherAPI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Company>HypherionMC</Company>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions ATLauncherAPI.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ATLauncherAPI", "ATLauncherAPI.csproj", "{CF3E4D1D-61D7-40E1-8BD6-FC51D66DC36D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CF3E4D1D-61D7-40E1-8BD6-FC51D66DC36D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CF3E4D1D-61D7-40E1-8BD6-FC51D66DC36D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CF3E4D1D-61D7-40E1-8BD6-FC51D66DC36D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CF3E4D1D-61D7-40E1-8BD6-FC51D66DC36D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
186 changes: 186 additions & 0 deletions ATLauncherAPIClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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>();
}
}
}
53 changes: 53 additions & 0 deletions apiobjects/PackArrayResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using ATLauncherAPI.apiobjects.helpers;

namespace ATLauncherAPI.apiobjects
{
public class PackArrayResult
{
private bool error;
private int code;
private String message;
private List<PackObject> data;

public PackArrayResult(bool error, int code, String message, List<PackObject> data) {
this.error = error;
this.code = code;
this.message = message;
this.data = data;
}

/***
* Get the error code returned by the API in the case of failure
* @return
*/
public int getCode() {
return code;
}

/***
* Check if the API call encountered an error
* @return
*/
public bool isError() {
return error;
}

/***
* Returns a PackObject containing the modpack info
* @return
*/
public List<PackObject> getData() {
return data;
}

/***
* Get the error message returned by the api. (NULL when no error)
* @return
*/
public String getMessage() {
return message;
}
}
}
36 changes: 36 additions & 0 deletions apiobjects/PackResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using ATLauncherAPI.apiobjects.helpers;

namespace ATLauncherAPI.apiobjects
{
public class PackResult
{
private bool error;
private int code;
private String message;
private PackObject data;

public PackResult(bool error, int code, String message, PackObject data) {
this.error = error;
this.code = code;
this.message = message;
this.data = data;
}

public int getCode() {
return code;
}

public bool isError() {
return error;
}

public PackObject getData() {
return data;
}

public String getMessage() {
return message;
}
}
}
36 changes: 36 additions & 0 deletions apiobjects/PackVersionResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using ATLauncherAPI.apiobjects.helpers;

namespace ATLauncherAPI.apiobjects
{
public class PackVersionResult
{
private bool error;
private int code;
private String message;
private PackVersionObject data;

public PackVersionResult(bool error, int code, String message, PackVersionObject data) {
this.error = error;
this.code = code;
this.message = message;
this.data = data;
}

public int getCode() {
return code;
}

public bool isError() {
return error;
}

public PackVersionObject getData() {
return data;
}

public String getMessage() {
return message;
}
}
}
37 changes: 37 additions & 0 deletions apiobjects/SimplePackResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using ATLauncherAPI.apiobjects.helpers;

namespace ATLauncherAPI.apiobjects
{
public class SimplePackResult
{
private bool error;
private int code;
private String message;
private List<SimplePackObject> data;

public SimplePackResult(bool error, int code, String message, List<SimplePackObject> data) {
this.error = error;
this.code = code;
this.message = message;
this.data = data;
}

public int getCode() {
return code;
}

public bool isError() {
return error;
}

public List<SimplePackObject> getData() {
return data;
}

public String getMessage() {
return message;
}
}
}
Loading

0 comments on commit 5b07311

Please sign in to comment.