Skip to content

Commit 7ce0182

Browse files
authored
Add files via upload
1 parent 6feff05 commit 7ce0182

27 files changed

+986
-0
lines changed

JavaDestinyAPI/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.dec4234</groupId>
8+
<artifactId>JavaDestinyAPI</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>1.14</maven.compiler.source>
13+
<maven.compiler.target>1.14</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.google.code.gson</groupId>
19+
<artifactId>gson</artifactId>
20+
<version>2.8.0</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.jetbrains</groupId>
24+
<artifactId>annotations</artifactId>
25+
<version>16.0.1</version>
26+
<scope>compile</scope>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.apache.maven.plugins</groupId>
30+
<artifactId>maven-compiler-plugin</artifactId>
31+
<version>3.8.1</version>
32+
</dependency>
33+
</dependencies>
34+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package exceptions;
2+
3+
/**
4+
* Thrown whenever a 401 error is detected in the HTTP response
5+
* This error indicates that the OAuth access token used by the request was not accepted by the server
6+
*/
7+
public class AccessTokenInvalidException extends Exception {
8+
9+
public AccessTokenInvalidException() {
10+
}
11+
12+
public AccessTokenInvalidException(String message) {
13+
super(message);
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package jdaSrc;
2+
3+
public class JavaDestinyAPIMain {
4+
5+
6+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package material;
2+
3+
import com.google.gson.*;
4+
import material.clan.Clan;
5+
import material.user.BungieUser;
6+
7+
import java.io.IOException;
8+
import java.io.InputStreamReader;
9+
import java.net.HttpURLConnection;
10+
import java.net.URL;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
public class DestinyAPI {
15+
16+
private static String apiKey = null;
17+
private static String clientId = null;
18+
private static String clientSecret = null;
19+
private static String oauthCode = null;
20+
21+
public DestinyAPI setApiKey(String apiKey) {
22+
DestinyAPI.apiKey = apiKey;
23+
return this;
24+
}
25+
26+
public DestinyAPI setClientID(String clientId) {
27+
DestinyAPI.clientId = clientId;
28+
return this;
29+
}
30+
31+
public DestinyAPI setClientSecret(String clientSecret) {
32+
DestinyAPI.clientSecret = clientSecret;
33+
return this;
34+
}
35+
36+
public DestinyAPI setOauthCode(String oauthCode) {
37+
DestinyAPI.oauthCode = oauthCode;
38+
return this;
39+
}
40+
41+
public BungieUser getUser(String id) { return new BungieUser(id); }
42+
43+
public List<BungieUser> getUsersWithName(String name) {
44+
List<BungieUser> temp = new ArrayList<>();
45+
try {
46+
String url = "https://www.bungie.net/platform/Destiny2/SearchDestinyPlayer/-1/" + name.replace(" ", "%20");
47+
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
48+
connection.setRequestMethod("GET");
49+
connection.addRequestProperty("X-API-KEY", apiKey);
50+
51+
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
52+
JsonElement parse = new JsonParser().parse(reader);
53+
JsonObject obj = parse.getAsJsonObject();
54+
if (obj.get("Response").isJsonArray()) {
55+
for (JsonElement objj : obj.getAsJsonArray("Response")) {
56+
JsonObject us = objj.getAsJsonObject();
57+
temp.add(new BungieUser(us.get("membershipId").getAsString()));
58+
}
59+
}
60+
return temp;
61+
} catch (IOException ioException) {
62+
ioException.printStackTrace();
63+
}
64+
return null;
65+
}
66+
67+
public Clan getClan(long id) {
68+
return new Clan(id);
69+
}
70+
public Clan getClan(String name) {
71+
return new Clan(name);
72+
}
73+
74+
public static String getApiKey() { return apiKey; }
75+
public static String getClientId() { return clientId; }
76+
public static String getClientSecret() { return clientSecret; }
77+
public static String getOauthCode() { return oauthCode; }
78+
79+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package material.clan;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import com.google.gson.JsonObject;
6+
import material.user.BungieUser;
7+
import material.DestinyAPI;
8+
import utils.HttpUtils;
9+
10+
import java.util.ArrayList;
11+
import java.util.Date;
12+
import java.util.List;
13+
14+
public class Clan {
15+
16+
private String apiKey = DestinyAPI.getApiKey();
17+
private HttpUtils hu = new HttpUtils();
18+
private JsonObject jo; // The entire Clan response
19+
private JsonObject cjo; // "detail"
20+
21+
private long clanId;
22+
private String clanName = null;
23+
24+
// Details about the clan (more or less in the order they appear in the JSON response)
25+
private String clanDescription;
26+
private Date creationDate;
27+
private int memberCount;
28+
private boolean isPublic;
29+
private String motto;
30+
31+
private boolean allowChat;
32+
33+
private BungieUser founder;
34+
private List<BungieUser> admins;
35+
private List<BungieUser> members = new ArrayList<>();
36+
37+
public Clan(long clanId) {
38+
this.clanId = clanId;
39+
cjo = hu.urlRequestGET("https://www.bungie.net/platform/GroupV2/" + clanId +"/?components=200").get("Response").getAsJsonObject().get("detail").getAsJsonObject();
40+
assignValues();
41+
}
42+
43+
public Clan(String clanName) {
44+
this.clanName = clanName;
45+
jo = hu.urlRequestGET("https://www.bungie.net/Platform/GroupV2/Name/" + clanName.replace(" ", "%20") +"/1/?components=200").get("Response").getAsJsonObject();
46+
cjo = jo.get("detail").getAsJsonObject();
47+
assignValues();
48+
}
49+
50+
private void assignValues() {
51+
if(clanName == null) { // If the clan object was created via ID then the clanName would be null by default
52+
clanName = cjo.get("name").getAsString();
53+
} else { // Opposite of previous reason
54+
clanId = cjo.get("groupId").getAsLong();
55+
}
56+
// creationDate = StringUtils.valueOfZTime(cjo.get("creationDate").getAsString()); // The date the clan was created
57+
clanDescription = cjo.get("about").getAsString();
58+
memberCount = cjo.get("memberCount").getAsInt();
59+
isPublic = cjo.get("isPublic").getAsBoolean();
60+
motto = cjo.get("motto").getAsString();
61+
allowChat = cjo.get("allowChat").getAsBoolean();
62+
63+
// founder = new BungieUser(jo.get("founder").getAsJsonObject().get("destinyUserInfo").getAsJsonObject().get("membershipId").getAsString());
64+
}
65+
66+
public String getClanID() {
67+
return clanId + "";
68+
}
69+
70+
public String getClanName() {
71+
return clanName;
72+
}
73+
74+
public String getClanDescription() { return clanDescription; }
75+
76+
public Date getCreationDate() { return creationDate; }
77+
78+
public int getMemberCount() { return memberCount; }
79+
80+
public boolean isPublic() { return isPublic; }
81+
82+
public String getMotto() { return motto; }
83+
84+
public boolean isAllowChat() { return allowChat; }
85+
86+
public BungieUser getFounder() { return founder; }
87+
88+
/**
89+
* Returns a list of the founder and the admins of the clan
90+
* The founder is always the first in this list?
91+
* Followed by the admins in the order they were promoted
92+
*/
93+
public List<BungieUser> getAdmins() {
94+
if(admins != null) return admins;
95+
96+
List<BungieUser> temp = new ArrayList<>();
97+
JsonArray ja = hu.urlRequestGET("https://www.bungie.net/Platform/GroupV2/" + clanId + "/AdminsAndFounder/?componenets=200").get("Response").getAsJsonObject().get("results").getAsJsonArray();
98+
99+
for(JsonElement je : ja) {
100+
temp.add(new BungieUser(je.getAsJsonObject().get("destinyUserInfo").getAsJsonObject().get("membershipId").getAsString()));
101+
}
102+
103+
return temp;
104+
}
105+
106+
public List<BungieUser> getMembers() {
107+
if(!members.isEmpty()) return members;
108+
109+
JsonObject jo = hu.urlRequestGET("https://www.bungie.net/Platform/GroupV2/" + clanId + "/Members/").get("Response").getAsJsonObject();
110+
111+
for(JsonElement je : jo.get("results").getAsJsonArray()) {
112+
members.add(new BungieUser(je.getAsJsonObject().get("destinyUserInfo").getAsJsonObject().get("membershipId").getAsString()));
113+
}
114+
115+
return members;
116+
}
117+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package material.clan;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonElement;
5+
import material.user.BungieUser;
6+
import utils.HttpUtils;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* Managing clans like kicking/accepting players and accessing sensitive information
13+
* Requires OAuth to be set up
14+
* Requires the user who authenticated the app to be an administrator of the clan they are trying to manage
15+
*/
16+
public class ClanManagement {
17+
18+
HttpUtils hu = new HttpUtils();
19+
20+
private Clan clan;
21+
private List<BungieUser> bannedMembers;
22+
private List<BungieUser> pendingMembers;
23+
24+
public ClanManagement(Clan clan) {
25+
this.clan = clan;
26+
}
27+
28+
/**
29+
* Kicks this user from the clan
30+
*/
31+
public void kickPlayer(BungieUser bungieUser) {
32+
hu.urlRequestPOSTOauth("https://www.bungie.net/Platform/GroupV2/" + clan.getClanID() + "/Members/" + bungieUser.getMembershipType() + "/" + bungieUser.getBungieMembershipID() + "/Kick/", "");
33+
}
34+
35+
/**
36+
* Bans the user from the clan
37+
*/
38+
public void banUser(BungieUser bungieUser) {
39+
40+
}
41+
42+
/**
43+
* Approves this user's request to join the clan if and only if they have requested to join
44+
*/
45+
public void approvePendingMember(BungieUser bungieUser) {
46+
hu.urlRequestPOSTOauth("https://www.bungie.net/Platform/GroupV2/" + clan.getClanID() + "/Members/Approve/" + bungieUser.getMembershipType() + "/" + bungieUser.getBungieMembershipID() + "/", "");
47+
}
48+
49+
/**
50+
* Approves all requests to join the clan
51+
*/
52+
public void approveAllPendingMembers() {
53+
hu.urlRequestPOSTOauth("https://www.bungie.net/Platform/GroupV2/" + clan.getClanID() + "/Members/ApproveAll/", "");
54+
}
55+
56+
/**
57+
* Denies all pending requests to join the clan :)
58+
*/
59+
public void denyAllPendingMembers() {
60+
hu.urlRequestPOSTOauth("https://www.bungie.net/Platform/GroupV2/" + clan.getClanID() + "/Members/DenyAll/?components=200", "");
61+
}
62+
63+
/**
64+
* Abdicates foundership to the next admin in the line of succession
65+
* @param bungieUser The user who will be the new founder (leader) of the clan
66+
*/
67+
public void abdicateFoundership(BungieUser bungieUser) {
68+
hu.urlRequestPOST("https://www.bungie.net/Platform/GroupV2/" + clan.getClanID() + "/Admin/AbdicateFoundership/" + bungieUser.getMembershipType() + "/" + bungieUser.getBungieMembershipID() + "/", "");
69+
}
70+
71+
/**
72+
* Gets a list of members who have been banned from the clan
73+
* @return The list of banned users
74+
*/
75+
public List<BungieUser> getBannedMembers() {
76+
if(bannedMembers != null) return bannedMembers;
77+
78+
List<BungieUser> temp = new ArrayList<BungieUser>();
79+
80+
JsonArray jo = hu.urlRequestGETOauth("https://www.bungie.net/Platform/GroupV2/" + clan.getClanID() + "/Banned/?componenets=200").getAsJsonObject("Response").get("results").getAsJsonArray();
81+
for(JsonElement je : jo) {
82+
temp.add(new BungieUser(je.getAsJsonObject().get("destinyUserInfo").getAsJsonObject().get("membershipId").getAsString()));
83+
}
84+
return temp;
85+
}
86+
87+
/**
88+
* Returns a list of pending members to the clan
89+
*/
90+
public List<BungieUser> getPendingMembers() {
91+
if(pendingMembers != null) return pendingMembers;
92+
93+
List<BungieUser> temp = new ArrayList<>();
94+
JsonArray ja = hu.urlRequestGETOauth("https://www.bungie.net/Platform/GroupV2/" + clan.getClanID() + "/Members/Pending/?components=200").get("Response").getAsJsonObject().get("results").getAsJsonArray();
95+
96+
for(JsonElement je : ja) {
97+
temp.add(new BungieUser(je.getAsJsonObject().get("destinyUserInfo").getAsJsonObject().get("membershipId").getAsString()));
98+
}
99+
100+
return temp;
101+
}
102+
}

0 commit comments

Comments
 (0)