Skip to content

Commit 43f7733

Browse files
committed
Updated to handle response wrapper
1 parent 818c21e commit 43f7733

File tree

6 files changed

+12
-12
lines changed

6 files changed

+12
-12
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.spotify;
22

3-
import com.spotify.json.JSONObject;
43
import com.spotify.requests.IRequest;
4+
import com.spotify.requests.RequestResponse;
55

66
public interface SpotifyClient {
77

88

99
// to be changed
10-
JSONObject executeRequest(IRequest request);
10+
RequestResponse executeRequest(IRequest request);
1111

1212

1313
}

src/main/java/com/spotify/SpotifyClientBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.http.SpotifyHttpServerProvider;
77
import com.spotify.json.JSONObject;
88
import com.spotify.requests.IRequest;
9+
import com.spotify.requests.RequestResponse;
910
import com.spotify.requests.util.Scope;
1011
import com.spotify.util.Util;
1112

@@ -197,7 +198,7 @@ public SpotifyClientImp(String accessToken) {
197198

198199

199200
@Override
200-
public JSONObject executeRequest(IRequest request) {
201+
public RequestResponse executeRequest(IRequest request) {
201202
return request.execute(this.accessToken);
202203
}
203204
}

src/main/java/com/spotify/requests/AbstractRequest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private static <T> String convertToString(Class<T> cls, Object o) {
4848
* @param url The specific API URL to be used, excluding "https://api.spotify.com/v1/"
4949
* @return The raw json response from the request
5050
*/
51-
protected final JSONObject requestGet(String token, String url) {
51+
protected final RequestResponse requestGet(String token, String url) {
5252
// Initialisation of http get request
5353

5454

@@ -63,7 +63,7 @@ protected final JSONObject requestGet(String token, String url) {
6363
String s = response.getMessage();
6464
switch (code) {
6565
case 200:
66-
return new JSONObject(response.getContent());
66+
return new RequestResponse(new JSONObject(response.getContent()), code, s);
6767
case 401:
6868
System.out.println("Bad or expired token. This can happen if the user revoked a " +
6969
"token or the access token has expired. You should re-authenticate the user.");
@@ -79,9 +79,8 @@ protected final JSONObject requestGet(String token, String url) {
7979
default:
8080
System.out.printf("Unknown fail cause, status code: %s.%n", code);
8181
}
82-
System.out.println(s);
8382

84-
return null;
83+
return new RequestResponse(null, code, s);
8584

8685

8786
} catch (NullPointerException e) {
@@ -91,7 +90,7 @@ protected final JSONObject requestGet(String token, String url) {
9190
}
9291

9392
@Override
94-
public JSONObject execute(String token) {
93+
public RequestResponse execute(String token) {
9594

9695
String urlQuery = this.buildRequestUrl();
9796
if (urlQuery == null) return null;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.spotify.requests;
22

3-
import com.spotify.json.JSONObject;
3+
44

55
public interface IRequest {
66

@@ -9,6 +9,6 @@ public interface IRequest {
99
*
1010
* @return {@code JSONObject} the raw json response
1111
*/
12-
JSONObject execute(String token);
12+
RequestResponse execute(String token);
1313

1414
}

src/test/java/com/spotify/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static void main(String[] args) throws IllegalAccessException {
2323
TrackGet al = new TrackGet("05lBXOMA1uHpVPEQZyjoh3");
2424

2525
// AlbumsSavedMeGet al = new AlbumsSavedMeGet();
26-
JSONObject jsonObject = spotifyClient.executeRequest(al);
26+
JSONObject jsonObject = spotifyClient.executeRequest(al).ok();
2727
System.out.println(jsonObject.toString(4));
2828

2929

src/test/java/com/spotify/httpserver/SpotifyRequestTester.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void handle(HttpExchange e) throws IOException {
5959
SpotifyClient spotifyClient = spotifyClientBuilder.build(code);
6060

6161
TrackGet currentUserProfileGet = new TrackGet("2mgkRsjpp6HH1MTyHYpTeF");
62-
JSONObject jsonObject = spotifyClient.executeRequest(currentUserProfileGet);
62+
JSONObject jsonObject = spotifyClient.executeRequest(currentUserProfileGet).ok();
6363

6464

6565
String response = jsonObject.toString(4);

0 commit comments

Comments
 (0)