Skip to content

Commit 384df58

Browse files
committed
More docs, bugfix, gitignore update
1 parent 14f1bf1 commit 384df58

File tree

9 files changed

+140
-83
lines changed

9 files changed

+140
-83
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
/src/main/java/net/dec4234/javadestinyapi/jdaSrc/
21
/.idea/
32
/out/
3+
/src/main/java/Testing.java

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ If you happen to need a JAR version, check out [releases](https://github.com/dec
3434
You need to get an API key from [bungie.net/developer](https://bungie.net/developer)
3535

3636
```java
37-
DestinyAPI api = new DestinyAPI().setApiKey("YOUR API KEY HERE");
37+
DestinyAPI api = new DestinyAPI("YOUR API KEY HERE");
3838
```
3939
**IMPORTANT:** *DestinyAPI MUST be the first thing initialized before making any interactions with the API! This is because
4040
all interactions with the API rely on the API Key set in DestinyAPI.*

src/main/java/net/dec4234/javadestinyapi/material/DestinyAPI.java

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ public class DestinyAPI {
3838

3939
private static HttpUtils httpUtils;
4040

41+
// Force set API key
42+
private DestinyAPI() {}
43+
44+
public DestinyAPI(String apiKey) {
45+
setApiKey(apiKey);
46+
}
47+
4148
/**
4249
* Set the api key used by the DestinyAPI
4350
*/
@@ -52,66 +59,82 @@ public DestinyAPI setApiKey(String apiKey) {
5259
return this;
5360
}
5461

62+
/**
63+
* Sets the internal client ID, needed for OAuth generation
64+
* @param clientId The client ID
65+
* @return This DestinyAPI instance
66+
*/
5567
public DestinyAPI setClientID(String clientId) {
5668
DestinyAPI.clientId = clientId;
5769
return this;
5870
}
5971

72+
/**
73+
* Sets the internal client secret
74+
* @param clientSecret The client secret
75+
* @return This DestinyAPI instance
76+
*/
6077
public DestinyAPI setClientSecret(String clientSecret) {
6178
DestinyAPI.clientSecret = clientSecret;
6279
return this;
6380
}
6481

82+
/**
83+
* Set the oauth code used to generate access and refresh tokens
84+
* @param oauthCode The oauth code
85+
* @return This DestinyAPI instance
86+
*/
6587
public DestinyAPI setOauthCode(String oauthCode) {
6688
DestinyAPI.oauthCode = oauthCode;
6789
return this;
6890
}
6991

70-
public DestinyAPI setAccessToken(String accessToken) {
92+
/**
93+
* Sets the OAuth Access token. This is typically an internal function only, but is provided for convenience.
94+
* @param accessToken The OAuth access token
95+
* @return This DestinyAPI instance
96+
*/
97+
public static void setAccessToken(String accessToken) {
7198
DestinyAPI.accessToken = accessToken;
7299
if (hasOauthManager()) {
73100
oam.setAccessToken(accessToken);
74101
}
75-
return this;
76102
}
77103

78-
public DestinyAPI setRefreshToken(String refreshToken) {
104+
/**
105+
* Sets the OAuth refresh token. This is typically an internal function only, but is provided for convenience.
106+
* @param refreshToken The OAuth refresh token
107+
* @return This DestinyAPI instance
108+
*/
109+
public static void setRefreshToken(String refreshToken) {
79110
DestinyAPI.refreshToken = refreshToken;
80111
if (hasOauthManager()) {
81112
oam.setRefreshToken(refreshToken);
82113
}
83-
return this;
84114
}
85115

86116
/**
87-
* Debug mode prints all requests and their responses to the console
88-
* This is very useful for feature development
117+
* Debug mode prints all requests and their responses to the console.
118+
* <br>
119+
* WARNING: This could expose sensitive information, this is for development ONLY
89120
*/
90-
public DestinyAPI enableDebugMode() {
121+
public static void enableDebugMode() {
91122
DestinyAPI.debugEnabled = true;
92-
93-
return this;
94123
}
95124

96125
/**
97126
* Disable debug mode
98127
*/
99-
public DestinyAPI disableDebugMode() {
128+
public static void disableDebugMode() {
100129
DestinyAPI.debugEnabled = false;
101-
102-
return this;
103130
}
104131

105132
/**
106133
* Set the OAuth management class
107134
* The class passed in this parameter must "extends OAuthManager"
108135
*/
109-
public DestinyAPI setOauthManager(OAuthManager oam) {
136+
public static void setOauthManager(OAuthManager oam) {
110137
DestinyAPI.oam = oam;
111-
// setApiKey(oam.getAPIToken());
112-
// setAccessToken(oam.getAccessToken());
113-
114-
return this;
115138
}
116139

117140
/**
@@ -225,8 +248,9 @@ public static BungieUser getUserWithName(String nameAndDiscrim) throws APIExcept
225248
* Search users across all platforms for anyone with that name.
226249
* <p>
227250
* Searching "dec4234" will return an array containing a single
228-
* BungieUser. While searching "Gladd" or "Datto" should return
229-
* multiple.
251+
* BungieUser. While searching "Gladd" or "Datto" should return many users.
252+
* <p>
253+
* // TODO: Issue #16
230254
*/
231255
public static List<BungieUser> searchUsers(String name) throws APIException {
232256
List<BungieUser> users = new ArrayList<>();

src/main/java/net/dec4234/javadestinyapi/material/user/BungieUser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public DestinyCharacter getCharacterOfType(DestinyCharacter.DestinyClass destiny
383383
}
384384

385385
/**
386-
* Adds up all of the time played across all characters in minutes
386+
* Adds up all the time played across all characters in minutes
387387
*
388388
* @return The time played of this user, in minutes
389389
*/

src/main/java/net/dec4234/javadestinyapi/utils/HttpUtils.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class HttpUtils {
3636

3737
public static final String URL_BASE = "https://www.bungie.net/Platform";
3838

39-
private String apiKey = DestinyAPI.getApiKey();
39+
private String apiKey;
4040
private static String bearerToken;
4141

4242
public HttpUtils(String apiKey) {
@@ -171,7 +171,8 @@ public String setTokenViaRefresh() throws APIException {
171171
String at = response.get("access_token").getAsString();
172172
String rt = response.get("refresh_token").getAsString();
173173
bearerToken = at;
174-
new DestinyAPI().setAccessToken(at).setRefreshToken(rt);
174+
DestinyAPI.setAccessToken(at);
175+
DestinyAPI.setRefreshToken(rt);
175176

176177
if(DestinyAPI.isDebugEnabled()) {
177178
System.out.println("TOKENS REFRESHED");
@@ -181,7 +182,7 @@ public String setTokenViaRefresh() throws APIException {
181182
}
182183

183184
/**
184-
* Requries an OAuthCode to be manually set inside of the DestinyAPI.setOAuthCode()
185+
* Requries an OAuthCode to be manually set inside the DestinyAPI.setOAuthCode()
185186
*/
186187
public void setTokenViaAuth() throws APIException {
187188
setTokenViaAuth(DestinyAPI.getOauthCode());
@@ -203,11 +204,26 @@ public void setTokenViaAuth(String oAuthCode) throws APIException {
203204
String accessToken = jsonObject.get("access_token").getAsString();
204205
String refreshToken = jsonObject.get("refresh_token").getAsString();
205206

206-
new DestinyAPI().setAccessToken(accessToken).setRefreshToken(refreshToken);
207+
DestinyAPI.setRefreshToken(refreshToken);
208+
DestinyAPI.setAccessToken(accessToken);
207209

208210
HttpUtils.bearerToken = accessToken;
209211
}
210212

213+
public boolean hasValidOAuthTokens() throws APIException {
214+
boolean value = DestinyAPI.hasOauthManager() && DestinyAPI.getAccessToken() != null && DestinyAPI.getHttpUtils().setTokenViaRefresh() != null;
215+
216+
if(value) {
217+
try {
218+
DestinyAPI.getHttpUtils().setTokenViaRefresh();
219+
} catch (AccessTokenExpiredException | RefreshTokenExpiredException | OAuthUnauthorizedException e) {
220+
value = false;
221+
}
222+
}
223+
224+
return value;
225+
}
226+
211227
private JsonObject getJsonObject(HttpRequest httpRequest) throws APIException {
212228
HttpClient httpClient = HttpClient.newHttpClient();
213229
String responseString;

src/main/java/net/dec4234/javadestinyapi/utils/framework/JDAOAuth.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
package net.dec4234.javadestinyapi.utils.framework;
99

1010
/**
11-
* Contains all of the functions needed for the API to adequately store OAuth tokens.
11+
* Contains all the functions needed for the API to adequately store OAuth tokens.
1212
* <br>
13-
* Note: OAuth could allow potenially dangerous actions such as full control over your clan (if you are an admin) as
13+
* Note: OAuth could allow potentially dangerous actions such as full control over your clan (if you are an admin) as
1414
* well as your inventory. Use at your own risk, and use good data management and protection practices.
1515
*/
1616
public interface JDAOAuth {

src/main/java/net/dec4234/javadestinyapi/jdaSrc/JavaDestinyAPIMain.java renamed to src/main/java/net/dec4234/javadestinyapi/utils/framework/JsonOAuthManager.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Github -> https://github.com/dec4234/JavaDestinyAPI
66
*/
77

8-
package net.dec4234.javadestinyapi.jdaSrc;
8+
package net.dec4234.javadestinyapi.utils.framework;
99

1010
import com.google.gson.JsonObject;
1111
import com.google.gson.JsonParser;
@@ -15,12 +15,19 @@
1515
import java.io.*;
1616
import java.nio.file.Paths;
1717

18-
public class JavaDestinyAPIMain extends OAuthManager {
18+
/**
19+
* This class is provided for convenience for local apps using OAuth. It is also provided as a template for what
20+
* you should do to implement your own token manager.
21+
* <br>
22+
* The author assumes no responsibility for the use of this class. It is unsafe to use this in a production environment,
23+
* proceed with your own risk.
24+
*/
25+
public class JsonOAuthManager extends OAuthManager {
1926

2027
private File file = new File(Paths.get("").toAbsolutePath() + "\\oauth.json");
2128
private JsonObject jsonObject;
2229

23-
public JavaDestinyAPIMain() {
30+
public JsonOAuthManager() {
2431
try {
2532
if (!file.exists()) {
2633
file.createNewFile();

0 commit comments

Comments
 (0)