|
6 | 6 | import org.jetbrains.annotations.NotNull; |
7 | 7 | import xyz.gianlu.librespot.api.server.AbsApiHandler; |
8 | 8 | import xyz.gianlu.librespot.api.server.ApiServer; |
| 9 | +import xyz.gianlu.librespot.api.server.ApiServer.PredefinedJsonRpcException; |
9 | 10 | import xyz.gianlu.librespot.core.Session; |
10 | 11 | import xyz.gianlu.librespot.mercury.MercuryClient; |
11 | 12 | import xyz.gianlu.librespot.mercury.RawMercuryRequest; |
|
17 | 18 | * @author Gianlu |
18 | 19 | */ |
19 | 20 | public class MercuryHandler extends AbsApiHandler { |
20 | | - private final MercuryClient client; |
| 21 | + private final Session session; |
21 | 22 |
|
22 | 23 | public MercuryHandler(@NotNull Session session) { |
23 | 24 | super("mercury"); |
24 | | - this.client = session.mercury(); |
| 25 | + this.session = session; |
25 | 26 | } |
26 | 27 |
|
27 | 28 | @Override |
28 | | - protected @NotNull JsonElement handleRequest(ApiServer.@NotNull Request request) throws HandlingException, ApiServer.PredefinedJsonRpcException { |
29 | | - if (request.getSuffix().equals("request")) { |
30 | | - JsonObject params = request.params.getAsJsonObject(); |
| 29 | + protected @NotNull JsonElement handleRequest(ApiServer.@NotNull Request request) throws HandlingException, PredefinedJsonRpcException { |
| 30 | + if (!request.getSuffix().equals("request")) |
| 31 | + throw PredefinedJsonRpcException.from(request, ApiServer.PredefinedJsonRpcError.METHOD_NOT_FOUND); |
31 | 32 |
|
32 | | - RawMercuryRequest.Builder builder = RawMercuryRequest.newBuilder() |
33 | | - .setMethod(params.get("method").getAsString()) |
34 | | - .setUri(params.get("uri").getAsString()); |
| 33 | + if (request.params == null || !request.params.isJsonArray()) |
| 34 | + throw PredefinedJsonRpcException.from(request, ApiServer.PredefinedJsonRpcError.INVALID_PARAMS); |
35 | 35 |
|
36 | | - if (params.has("headers")) { |
37 | | - JsonArray headers = params.getAsJsonArray("headers"); |
38 | | - for (JsonElement element : headers) { |
39 | | - JsonObject header = element.getAsJsonObject(); |
40 | | - builder.addUserField(header.get("key").getAsString(), header.get("value").getAsString()); |
41 | | - } |
| 36 | + JsonObject params = request.params.getAsJsonObject(); |
| 37 | + |
| 38 | + RawMercuryRequest.Builder builder = RawMercuryRequest.newBuilder() |
| 39 | + .setMethod(params.get("method").getAsString()) |
| 40 | + .setUri(params.get("uri").getAsString()); |
| 41 | + |
| 42 | + if (params.has("headers")) { |
| 43 | + JsonArray headers = params.getAsJsonArray("headers"); |
| 44 | + for (JsonElement element : headers) { |
| 45 | + JsonObject header = element.getAsJsonObject(); |
| 46 | + builder.addUserField(header.get("key").getAsString(), header.get("value").getAsString()); |
42 | 47 | } |
| 48 | + } |
43 | 49 |
|
44 | | - try { |
45 | | - MercuryClient.Response response = client.sendSync(builder.build()); |
| 50 | + try { |
| 51 | + MercuryClient.Response response = session.mercury().sendSync(builder.build()); |
46 | 52 |
|
47 | | - JsonArray payloads = new JsonArray(response.payload.size()); |
48 | | - for (byte[] bytes : response.payload) |
49 | | - payloads.add(Base64.getEncoder().encodeToString(bytes)); |
| 53 | + JsonArray payloads = new JsonArray(response.payload.size()); |
| 54 | + for (byte[] bytes : response.payload) |
| 55 | + payloads.add(Base64.getEncoder().encodeToString(bytes)); |
50 | 56 |
|
51 | | - JsonObject obj = new JsonObject(); |
52 | | - obj.addProperty("code", response.statusCode); |
53 | | - obj.addProperty("uri", response.uri); |
54 | | - obj.add("payloads", payloads); |
55 | | - return obj; |
56 | | - } catch (IOException ex) { |
57 | | - throw new HandlingException(ex, ErrorCode.IO_EXCEPTION); |
58 | | - } |
59 | | - } else { |
60 | | - throw ApiServer.PredefinedJsonRpcException.from(request, ApiServer.PredefinedJsonRpcError.METHOD_NOT_FOUND); |
| 57 | + JsonObject obj = new JsonObject(); |
| 58 | + obj.addProperty("code", response.statusCode); |
| 59 | + obj.addProperty("uri", response.uri); |
| 60 | + obj.add("payloads", payloads); |
| 61 | + return obj; |
| 62 | + } catch (IOException ex) { |
| 63 | + throw new HandlingException(ex, ErrorCode.IO_EXCEPTION); |
61 | 64 | } |
62 | 65 | } |
63 | 66 |
|
|
0 commit comments