Skip to content

Commit 60ebdc6

Browse files
authored
Merge pull request #403 from Twinki14/dev-feat-instance-get
feat: Add GET /instance endpoint, which returns some basic information about the current session
2 parents 5b6d5c0 + ef4e95d commit 60ebdc6

4 files changed

Lines changed: 69 additions & 24 deletions

File tree

api/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ All the endpoints will respond with `200` if successful or:
4242
- `GET /profile/{user_id}/following` Retrieve a list of profiles that the specified user is following
4343

4444
### Instance
45+
- `GET /instance` Returns a json model that contains basic information about the current session; `device_id`, `device_name`,`device_type`, `country_code`, and `preferred_locale`
4546
- `POST /instance/terminate` Terminates the API server.
4647
- `POST /instance/close` Closes the current session (and player).
4748

api/src/main/java/xyz/gianlu/librespot/api/ApiServer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class ApiServer {
3535
private Undertow undertow = null;
3636

3737
public ApiServer(int port, @NotNull String host, @NotNull SessionWrapper wrapper) {
38+
AbsSessionHandler instanceHandler = InstanceHandler.forSession(this, wrapper);
39+
3840
this.port = port;
3941
this.host = host;
4042
this.wrapper = wrapper;
@@ -45,7 +47,8 @@ public ApiServer(int port, @NotNull String host, @NotNull SessionWrapper wrapper
4547
.post("/token/{scope}", new TokensHandler(wrapper))
4648
.post("/profile/{user_id}/{action}", new ProfileHandler(wrapper))
4749
.post("/web-api/{endpoint}", new WebApiHandler(wrapper))
48-
.post("/instance/{action}", InstanceHandler.forSession(this, wrapper))
50+
.get("/instance", instanceHandler)
51+
.post("/instance/{action}", instanceHandler)
4952
.post("/discovery/{action}", new DiscoveryHandler())
5053
.get("/events", events)
5154
.setFallbackHandler(new PathHandler(ResponseCodeHandler.HANDLE_404)

api/src/main/java/xyz/gianlu/librespot/api/Utils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,9 @@ public static void internalError(@NotNull HttpServerExchange exchange, @NotNull
8383
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
8484
exchange.getResponseSender().send(String.format(INTERNAL_ERROR_BODY, reason));
8585
}
86+
87+
public static void methodNotAllowed(@NotNull HttpServerExchange exchange) {
88+
exchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
89+
exchange.getResponseSender().send(StatusCodes.METHOD_NOT_ALLOWED_STRING);
90+
}
8691
}

api/src/main/java/xyz/gianlu/librespot/api/handlers/InstanceHandler.java

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package xyz.gianlu.librespot.api.handlers;
1818

19+
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonSyntaxException;
1921
import io.undertow.server.HttpServerExchange;
22+
import io.undertow.util.Headers;
2023
import org.jetbrains.annotations.NotNull;
2124
import org.jetbrains.annotations.Nullable;
2225
import xyz.gianlu.librespot.api.ApiServer;
@@ -55,6 +58,16 @@ private static String getAction(@NotNull HttpServerExchange exchange) throws IOE
5558
return action;
5659
}
5760

61+
private static String getInstanceInfo(@NotNull Session session) throws JsonSyntaxException {
62+
JsonObject infoObj = new JsonObject();
63+
infoObj.addProperty("device_id", session.deviceId());
64+
infoObj.addProperty("device_name", session.deviceName());
65+
infoObj.addProperty("device_type", session.deviceType().toString());
66+
infoObj.addProperty("country_code", session.countryCode());
67+
infoObj.addProperty("preferred_locale", session.preferredLocale());
68+
return infoObj.toString();
69+
}
70+
5871
private static class SessionHandler extends AbsSessionHandler {
5972
private final ApiServer server;
6073

@@ -71,19 +84,31 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess
7184
return;
7285
}
7386

74-
String action = getAction(exchange);
75-
if (action == null) return;
76-
77-
switch (action) {
78-
case "terminate":
79-
exchange.endExchange();
80-
new Thread(server::stop).start();
81-
break;
82-
case "close":
83-
session.close();
87+
String requestMethod = exchange.getRequestMethod().toString();
88+
switch(requestMethod) {
89+
case "GET":
90+
String info = getInstanceInfo(session);
91+
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
92+
exchange.getResponseSender().send(info);
93+
return;
94+
case "POST":
95+
String action = getAction(exchange);
96+
if (action == null) return;
97+
98+
switch (action) {
99+
case "terminate":
100+
exchange.endExchange();
101+
new Thread(server::stop).start();
102+
break;
103+
case "close":
104+
session.close();
105+
break;
106+
default:
107+
Utils.methodNotAllowed(exchange);
108+
break;
109+
}
84110
break;
85111
default:
86-
Utils.invalidParameter(exchange, "action");
87112
break;
88113
}
89114
}
@@ -105,20 +130,31 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess
105130
return;
106131
}
107132

108-
String action = getAction(exchange);
109-
if (action == null) return;
110-
111-
switch (action) {
112-
case "terminate":
113-
exchange.endExchange();
114-
new Thread(server::stop).start();
115-
break;
116-
case "close":
117-
player.close();
118-
session.close();
133+
String requestMethod = exchange.getRequestMethod().toString();
134+
switch(requestMethod) {
135+
case "GET":
136+
String info = getInstanceInfo(session);
137+
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
138+
exchange.getResponseSender().send(info);
139+
return;
140+
case "POST":
141+
String action = getAction(exchange);
142+
if (action == null) return;
143+
144+
switch (action) {
145+
case "terminate":
146+
exchange.endExchange();
147+
new Thread(server::stop).start();
148+
break;
149+
case "close":
150+
session.close();
151+
break;
152+
default:
153+
Utils.methodNotAllowed(exchange);
154+
break;
155+
}
119156
break;
120157
default:
121-
Utils.invalidParameter(exchange, "action");
122158
break;
123159
}
124160
}

0 commit comments

Comments
 (0)