Skip to content

Commit c409e85

Browse files
committed
Make request code more readable
1 parent 2f7648b commit c409e85

1 file changed

Lines changed: 47 additions & 43 deletions

File tree

src/com/namelessmc/NamelessAPI/Request.java

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,39 @@ public class Request {
3131
private int errorCode = -1;
3232

3333
public Request(URL baseUrl, Action action, String... parameters) {
34+
this.action = action;
35+
this.method = action.method;
36+
this.parameters = String.join("&", parameters);
37+
3438
try {
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());
39+
/*
40+
* If the url contains a question mark, it's most likely not a friendly url.
41+
* Friendly urls have the normal url as a parameter like this:
42+
* https://example.com/index.php?route=/api/v2/key/action
43+
* Parameters should be added after the route parameter (&param1=value1&param2=value2)
44+
*
45+
* If the url does not contain a question mark, it's most likely a friendly url
46+
* https://example.com/api/v2/key/action
47+
* Parameters need to be prefixed with a question mark (?param1&value1&param2=value2)
48+
*
49+
* For post method, parameters need to be sent like this: param1=value1&param2=value2
50+
* regardless of the friendly url option.
51+
*/
52+
53+
String base = baseUrl.toString();
54+
base = base.endsWith("/") ? base : base + "/"; // Append trailing slash if not present
55+
56+
if (action.method == RequestMethod.GET){
57+
char prefix = baseUrl.toString().contains("?") ? '&' : '?';
58+
url = new URL(action.toString() + prefix + this.parameters);
59+
}
60+
61+
if (action.method == RequestMethod.POST) {
62+
url = new URL(action.toString());
4663
}
4764
} catch (MalformedURLException e) {
48-
throw new IllegalArgumentException("URL or action is malformed (" + e.getMessage() + ")");
65+
throw new IllegalArgumentException("URL is malformed (" + e.getMessage() + ")");
4966
}
50-
this.action = action;
51-
this.method = action.method;
52-
this.parameters = String.join("&", parameters);
5367
}
5468

5569
public boolean hasError() {
@@ -83,20 +97,19 @@ public void connect() throws NamelessException {
8397
connection.setRequestMethod(method.toString());
8498
connection.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
8599
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
86-
connection.setDoOutput(true);
87100
connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
88101

89-
if(method == RequestMethod.GET) {
90-
connection.setRequestMethod("GET");
102+
// If request method is POST, send parameters. Otherwise, they're already included in the URL
103+
if (action.method == RequestMethod.POST) {
104+
connection.setDoOutput(true);
105+
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
106+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
107+
writer.write(parameters);
108+
writer.flush();
109+
writer.close();
110+
outputStream.close();
91111
}
92112

93-
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
94-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
95-
writer.write(parameters);
96-
writer.flush();
97-
writer.close();
98-
outputStream.close();
99-
100113
// Initialize input stream
101114
InputStream inputStream = connection.getInputStream();
102115

@@ -122,20 +135,19 @@ public void connect() throws NamelessException {
122135
connection.setRequestMethod(method.toString());
123136
connection.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
124137
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
125-
connection.setDoOutput(true);
126138
connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
127139

128-
if(method == RequestMethod.GET) {
129-
connection.setRequestMethod("GET");
140+
// If request method is POST, send parameters. Otherwise, they're already included in the URL
141+
if (action.method == RequestMethod.POST) {
142+
connection.setDoOutput(true);
143+
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
144+
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
145+
writer.write(parameters);
146+
writer.flush();
147+
writer.close();
148+
outputStream.close();
130149
}
131150

132-
DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream());
133-
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
134-
writer.write(parameters);
135-
writer.flush();
136-
writer.close();
137-
outputStream.close();
138-
139151
// Initialize input stream
140152
InputStream inputStream = connection.getInputStream();
141153

@@ -166,14 +178,6 @@ public void connect() throws NamelessException {
166178
}
167179
}
168180

169-
private static String appendCharacter(String string, char c) {
170-
if (string.endsWith(c + "")) {
171-
return string;
172-
} else {
173-
return string + c;
174-
}
175-
}
176-
177181
public static enum Action {
178182

179183
INFO("info", GET),

0 commit comments

Comments
 (0)