Skip to content

Commit 2f7648b

Browse files
committed
Fix API error 26 and minor fixes
1 parent faa093c commit 2f7648b

3 files changed

Lines changed: 84 additions & 51 deletions

File tree

src/com/namelessmc/NamelessAPI/NamelessAPI.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public final class NamelessAPI {
2020

2121
/**
2222
*
23-
* @param apiUrl
23+
* @param apiUrl URL of API to connect to
2424
* @param debug If debug is set to true, debug messages are enabled for <i>every</i> NamelessAPI instance.
2525
*/
2626
public NamelessAPI(URL apiUrl, boolean debug) {
@@ -57,9 +57,8 @@ public NamelessException checkWebAPIConnection() {
5757

5858
/**
5959
* Get all announcements
60-
* @param apiUrl
61-
* @return
62-
* @throws NamelessException
60+
* @return list of current announcements
61+
* @throws NamelessException if there is an error in the request
6362
*/
6463
public List<Announcement> getAnnouncements() throws NamelessException {
6564
Request request = new Request(apiUrl, Action.GET_ANNOUNCEMENTS);
@@ -83,9 +82,9 @@ public List<Announcement> getAnnouncements() throws NamelessException {
8382

8483
/**
8584
* Get all announcements visible for the player with the specified uuid
86-
* @param apiUrl
87-
* @return
88-
* @throws NamelessException
85+
* @param uuid UUID of player to get visibile announcements for
86+
* @return list of current announcements visible to the player
87+
* @throws NamelessException if there is an error in the request
8988
*/
9089
public List<Announcement> getAnnouncements(UUID uuid) throws NamelessException {
9190
Request request = new Request(apiUrl, Action.GET_ANNOUNCEMENTS, new ParameterBuilder().add("uuid", uuid).build());

src/com/namelessmc/NamelessAPI/NamelessPlayer.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public final class NamelessPlayer {
2121
private boolean exists;
2222
private boolean validated;
2323
private boolean banned;
24+
private String groupName;
2425

2526
private URL baseUrl;
2627

@@ -72,10 +73,12 @@ private void init(Request request) throws NamelessException {
7273
userName = response.get("username").getAsString();
7374
displayName = response.get("displayname").getAsString();
7475
uuid = UUID.fromString(addDashesToUUID(response.get("uuid").getAsString()));
76+
groupName = response.get("group_name").getAsString();
7577
groupID = response.get("group_id").getAsInt();
7678
registeredDate = registered;
7779
validated = response.get("validated").getAsBoolean();
78-
reputation = response.get("reputation").getAsInt();
80+
//reputation = response.get("reputation").getAsInt();
81+
reputation = 0; // temp until reputation is added to API
7982
banned = response.get("banned").getAsBoolean();
8083
}
8184

@@ -139,6 +142,17 @@ public int getGroupID() {
139142
return groupID;
140143
}
141144

145+
/**
146+
* @return The user's primary group name
147+
*/
148+
public String getGroupName() {
149+
if (!exists) {
150+
throw new UnsupportedOperationException("This player does not exist.");
151+
}
152+
153+
return groupName;
154+
}
155+
142156
/**
143157
* @return The user's site reputation.
144158
*/
@@ -163,7 +177,7 @@ public Date getRegisteredDate() {
163177

164178
/**
165179
* @return Whether an account associated with the UUID exists.
166-
* @see #getUUID()
180+
* @see #getUniqueId()
167181
*/
168182
public boolean exists() {
169183
return exists;
@@ -250,11 +264,12 @@ public void setGroup(int groupId) throws NamelessException {
250264
* Registers a new account. The player will be sent an email to set a password.
251265
* @param minecraftName In-game name for this player
252266
* @param email Email address
267+
* @param uuid UUID of player
253268
* @return Email verification disabled: A link which the user needs to click to complete registration
254269
* <br>Email verification enabled: An empty string (the user needs to check their email to complete registration)
255270
* @throws NamelessException
256271
*/
257-
public String register(String minecraftName, String email) throws NamelessException {
272+
public String register(String minecraftName, String email, UUID uuid) throws NamelessException {
258273
final String[] parameters = new ParameterBuilder().add("username", minecraftName).add("uuid", uuid).add("email", email).build();
259274
final Request request = new Request(baseUrl, Action.REGISTER, parameters);
260275
request.connect();

src/com/namelessmc/NamelessAPI/Request.java

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,124 +20,143 @@
2020
import com.google.gson.JsonParser;
2121

2222
public class Request {
23-
23+
2424
private URL url;
2525
private RequestMethod method;
2626
private String parameters;
2727
private Action action;
28-
28+
2929
private JsonObject response;
3030
private boolean hasError;
3131
private int errorCode = -1;
32-
32+
3333
public Request(URL baseUrl, Action action, String... parameters) {
3434
try {
35-
url = new URL(appendCharacter(baseUrl.toString(), '/') + action.toString());
35+
if(action.method == RequestMethod.GET){
36+
// Check for ? to see if we need ? or & (non-friendly URL support)
37+
Character appendChar = '?';
38+
if(baseUrl.toString().contains("?")){
39+
appendChar = '&';
40+
}
41+
42+
url = new URL(appendCharacter(appendCharacter(baseUrl.toString(), '/') + action.toString(), appendChar) + String.join("&", parameters));
43+
44+
} else {
45+
url = new URL(appendCharacter(baseUrl.toString(), '/') + action.toString());
46+
}
3647
} catch (MalformedURLException e) {
3748
throw new IllegalArgumentException("URL or action is malformed (" + e.getMessage() + ")");
3849
}
3950
this.action = action;
4051
this.method = action.method;
4152
this.parameters = String.join("&", parameters);
4253
}
43-
54+
4455
public boolean hasError() {
4556
return hasError;
4657
}
47-
58+
4859
public int getError() throws NamelessException {
4960
if (!hasError) {
5061
throw new NamelessException("Requested error code but there is no error.");
5162
}
52-
63+
5364
return errorCode;
5465
}
55-
56-
public JsonObject getResponse() throws NamelessException {
66+
67+
public JsonObject getResponse() throws NamelessException {
5768
return response;
5869
}
59-
70+
6071
public void connect() throws NamelessException {
61-
72+
6273
if (NamelessAPI.DEBUG_MODE) {
6374
System.out.printf("NamelessAPI > Making %s request (%s)\n", action.toString(), method.toString());
6475
System.out.printf("NamelessAPI > URL: %s\n", url);
6576
System.out.printf("NamelessAPI > Parameters: %s\n", parameters);
6677
}
67-
78+
6879
try {
6980
if (url.toString().startsWith("https://")){
7081
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
71-
82+
7283
connection.setRequestMethod(method.toString());
73-
//connection.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
84+
connection.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
7485
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
7586
connection.setDoOutput(true);
7687
connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
77-
88+
89+
if(method == RequestMethod.GET) {
90+
connection.setRequestMethod("GET");
91+
}
92+
7893
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
7994
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
8095
writer.write(parameters);
8196
writer.flush();
8297
writer.close();
8398
outputStream.close();
84-
99+
85100
// Initialize input stream
86101
InputStream inputStream = connection.getInputStream();
87-
102+
88103
// Handle response
89104
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
90105
StringBuilder responseBuilder = new StringBuilder();
91-
106+
92107
String responseString;
93108
while ((responseString = streamReader.readLine()) != null)
94109
responseBuilder.append(responseString);
95-
110+
96111
JsonParser parser = new JsonParser();
97-
112+
98113
response = parser.parse(responseBuilder.toString()).getAsJsonObject();
99114

100115
inputStream.close();
101-
116+
102117
// Disconnect
103118
connection.disconnect();
104119
} else {
105120
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
106-
121+
107122
connection.setRequestMethod(method.toString());
108-
//connection.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
123+
connection.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
109124
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
110125
connection.setDoOutput(true);
111126
connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
112-
127+
128+
if(method == RequestMethod.GET) {
129+
connection.setRequestMethod("GET");
130+
}
131+
113132
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
114133
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
115134
writer.write(parameters);
116135
writer.flush();
117136
writer.close();
118137
outputStream.close();
119-
138+
120139
// Initialize input stream
121140
InputStream inputStream = connection.getInputStream();
122-
141+
123142
// Handle response
124143
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
125144
StringBuilder responseBuilder = new StringBuilder();
126-
145+
127146
String responseString;
128147
while ((responseString = streamReader.readLine()) != null)
129148
responseBuilder.append(responseString);
130-
149+
131150
JsonParser parser = new JsonParser();
132-
151+
133152
response = parser.parse(responseBuilder.toString()).getAsJsonObject();
134153

135154
inputStream.close();
136-
155+
137156
// Disconnect
138157
connection.disconnect();
139158
}
140-
159+
141160
hasError = response.get("error").getAsBoolean();
142161
if (hasError) {
143162
errorCode = response.get("code").getAsInt();
@@ -146,17 +165,17 @@ public void connect() throws NamelessException {
146165
throw new NamelessException(e);
147166
}
148167
}
149-
168+
150169
private static String appendCharacter(String string, char c) {
151170
if (string.endsWith(c + "")) {
152171
return string;
153172
} else {
154173
return string + c;
155174
}
156175
}
157-
176+
158177
public static enum Action {
159-
178+
160179
INFO("info", GET),
161180
GET_ANNOUNCEMENTS("getAnnouncements", GET),
162181
REGISTER("register", POST),
@@ -166,17 +185,17 @@ public static enum Action {
166185
GET_NOTIFICATIONS("getNotifications", GET),
167186
SERVER_INFO("serverInfo", POST),
168187
VALIDATE_USER("validateUser", POST),
169-
188+
170189
;
171-
190+
172191
RequestMethod method;
173192
String name;
174-
193+
175194
Action(String name, RequestMethod method){
176195
this.name = name;
177196
this.method = method;
178197
}
179-
198+
180199
@Override
181200
public String toString() {
182201
return name;
@@ -192,11 +211,11 @@ public String toString() {
192211
}*/
193212

194213
}
195-
214+
196215
public static enum RequestMethod {
197-
216+
198217
GET, POST
199-
218+
200219
}
201220

202221
}

0 commit comments

Comments
 (0)