Skip to content

Commit cee578d

Browse files
committed
Http Encoding for ^
1 parent b837660 commit cee578d

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

src/main/java/material/DestinyAPI.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import material.user.UserCredential;
1818
import material.user.UserCredentialType;
1919
import utils.HttpUtils;
20+
import utils.StringUtils;
2021
import utils.framework.OAuthManager;
2122

2223
import java.util.ArrayList;
@@ -157,7 +158,6 @@ public static UserCredential[] getUserCredentials(BungieUser bungieUser) {
157158

158159
/**
159160
* Get a "UserCredential" from a BungieUser
160-
*
161161
*/
162162
public static UserCredential getUserCredential(UserCredentialType type, BungieUser bungieUser) {
163163
for(UserCredential userCredential : getUserCredentials(bungieUser)) {
@@ -182,15 +182,15 @@ public static List<BungieUser> getUsersWithName(String name) {
182182
List<BungieUser> temp = new ArrayList<>();
183183
List<String> ids = new ArrayList<>();
184184

185-
// encode characters space hashtag
186-
name = name.replace(" ", "%20").replace("#", "%23");
185+
// encode characters
186+
name = StringUtils.httpEncode(name);
187187

188188
try {
189189
JsonObject obj = hu.urlRequestGET("https://www.bungie.net/platform/Destiny2/SearchDestinyPlayer/-1/" + name + "/?components=204");
190-
JsonArray ja = obj.getAsJsonArray("Response");
190+
JsonArray jsonArray = obj.getAsJsonArray("Response");
191191

192-
for (JsonElement je : ja) {
193-
JsonObject us = je.getAsJsonObject();
192+
for (JsonElement jsonElement : jsonArray) {
193+
JsonObject us = jsonElement.getAsJsonObject();
194194
BungieUser bu = new BungieUser(us.get("membershipId").getAsString(), DestinyPlatform.fromMembershipType(us.get("membershipType").getAsInt()));
195195
if (!ids.contains(bu.getBungieMembershipID())) {
196196
temp.add(bu);
@@ -220,6 +220,8 @@ public static List<BungieUser> getValidUsers(String name) {
220220
* while for this you can search with "dec4234"
221221
*/
222222
public static List<BungieUser> searchGlobalDisplayNames(String prefix) {
223+
prefix = StringUtils.httpEncode(prefix);
224+
223225
List<BungieUser> bungieUsers = new ArrayList<>();
224226

225227
JsonArray jsonArray = new HttpUtils().urlRequestGET("https://www.bungie.net/Platform/User/Search/Prefix/" + prefix + "/0/").getAsJsonObject("Response").getAsJsonArray("searchResults");

src/main/java/material/user/UserCredential.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
package material.user;
1010

11+
/**
12+
* A "UserCredential" is a linked Platform to a Bungie Account
13+
*
14+
* Such as a twitch account
15+
*/
1116
public class UserCredential {
1217

1318
private UserCredentialType userCredentialType;

src/main/java/utils/HttpUtils.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@
88

99
package utils;
1010

11-
import com.google.gson.*;
11+
import com.google.gson.JsonObject;
12+
import com.google.gson.JsonParser;
1213
import exceptions.APIOfflineException;
1314
import exceptions.AccessTokenInvalidException;
1415
import material.DestinyAPI;
1516
import material.manifest.ManifestEntityTypes;
1617

17-
import java.net.ConnectException;
1818
import java.net.URI;
1919
import java.net.http.HttpClient;
2020
import java.net.http.HttpRequest;
2121
import java.net.http.HttpResponse;
2222
import java.time.Duration;
2323
import java.util.Base64;
24-
import java.util.concurrent.CompletableFuture;
2524
import java.util.concurrent.ExecutionException;
2625

2726
/**
@@ -30,7 +29,7 @@
3029
*/
3130
public class HttpUtils {
3231

33-
String apiKey = DestinyAPI.getApiKey();
32+
private String apiKey = DestinyAPI.getApiKey();
3433
private static String bearerToken;
3534

3635
/**

src/main/java/utils/StringUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class StringUtils {
2424
*/
2525
public static Date valueOfZTime(String zuluTimeInString) {
2626
String temp = zuluTimeInString;
27-
if(temp.length() == 24) {
27+
if(temp.length() == 24) { // Sometimes the date will be in an extra precise format, which is truncated here
2828
temp = temp.substring(0, temp.length() - 5);
2929
temp = temp + "Z";
3030
}
@@ -38,8 +38,20 @@ public static Date valueOfZTime(String zuluTimeInString) {
3838
return null;
3939
}
4040

41+
/**
42+
* Get the number of days since the time date provided
43+
*/
4144
public static double getDaysSinceTime(Date date) {
4245
DecimalFormat df = new DecimalFormat("0.##");
4346
return Double.parseDouble(df.format((new Date().getTime() - date.getTime()) / 1000.0 / 60.0 / 60.0 / 24.0));
4447
}
48+
49+
/**
50+
* Encode a string to be suitable for use in a url
51+
*
52+
* Specific characters need to be encoded in order to have a successful request
53+
*/
54+
public static String httpEncode(String input) {
55+
return input.replace(" ", "%20").replace("#", "%23").replace("^", "%5E");
56+
}
4557
}

0 commit comments

Comments
 (0)