Skip to content

Commit a77fad7

Browse files
committed
feat: Add GET /instance endpoint, which returns some basic information about the current session
- `device_id`, `device_name`,`device_type`, `country_code`, and `preferred_locale`
1 parent 5b6d5c0 commit a77fad7

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public ApiServer(int port, @NotNull String host, @NotNull SessionWrapper wrapper
4545
.post("/token/{scope}", new TokensHandler(wrapper))
4646
.post("/profile/{user_id}/{action}", new ProfileHandler(wrapper))
4747
.post("/web-api/{endpoint}", new WebApiHandler(wrapper))
48+
.get("/instance", InstanceHandler.forSession(this, wrapper))
4849
.post("/instance/{action}", InstanceHandler.forSession(this, wrapper))
4950
.post("/discovery/{action}", new DiscoveryHandler())
5051
.get("/events", events)

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.invalidParameter(exchange, "action");
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.invalidParameter(exchange, "action");
154+
break;
155+
}
119156
break;
120157
default:
121-
Utils.invalidParameter(exchange, "action");
122158
break;
123159
}
124160
}

0 commit comments

Comments
 (0)