Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ All the endpoints will respond with `200` if successful or:
- `GET /profile/{user_id}/following` Retrieve a list of profiles that the specified user is following

### Instance
- `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`
- `POST /instance/terminate` Terminates the API server.
- `POST /instance/close` Closes the current session (and player).

Expand Down
5 changes: 4 additions & 1 deletion api/src/main/java/xyz/gianlu/librespot/api/ApiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class ApiServer {
private Undertow undertow = null;

public ApiServer(int port, @NotNull String host, @NotNull SessionWrapper wrapper) {
AbsSessionHandler instanceHandler = InstanceHandler.forSession(this, wrapper);

this.port = port;
this.host = host;
this.wrapper = wrapper;
Expand All @@ -45,7 +47,8 @@ public ApiServer(int port, @NotNull String host, @NotNull SessionWrapper wrapper
.post("/token/{scope}", new TokensHandler(wrapper))
.post("/profile/{user_id}/{action}", new ProfileHandler(wrapper))
.post("/web-api/{endpoint}", new WebApiHandler(wrapper))
.post("/instance/{action}", InstanceHandler.forSession(this, wrapper))
.get("/instance", instanceHandler)
.post("/instance/{action}", instanceHandler)
.post("/discovery/{action}", new DiscoveryHandler())
.get("/events", events)
.setFallbackHandler(new PathHandler(ResponseCodeHandler.HANDLE_404)
Expand Down
5 changes: 5 additions & 0 deletions api/src/main/java/xyz/gianlu/librespot/api/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ public static void internalError(@NotNull HttpServerExchange exchange, @NotNull
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
exchange.getResponseSender().send(String.format(INTERNAL_ERROR_BODY, reason));
}

public static void methodNotAllowed(@NotNull HttpServerExchange exchange) {
exchange.setStatusCode(StatusCodes.METHOD_NOT_ALLOWED);
exchange.getResponseSender().send(StatusCodes.METHOD_NOT_ALLOWED_STRING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package xyz.gianlu.librespot.api.handlers;

import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.gianlu.librespot.api.ApiServer;
Expand Down Expand Up @@ -55,6 +58,16 @@ private static String getAction(@NotNull HttpServerExchange exchange) throws IOE
return action;
}

private static String getInstanceInfo(@NotNull Session session) throws JsonSyntaxException {
JsonObject infoObj = new JsonObject();
infoObj.addProperty("device_id", session.deviceId());
infoObj.addProperty("device_name", session.deviceName());
infoObj.addProperty("device_type", session.deviceType().toString());
infoObj.addProperty("country_code", session.countryCode());
infoObj.addProperty("preferred_locale", session.preferredLocale());
return infoObj.toString();
}

private static class SessionHandler extends AbsSessionHandler {
private final ApiServer server;

Expand All @@ -71,19 +84,31 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess
return;
}

String action = getAction(exchange);
if (action == null) return;

switch (action) {
case "terminate":
exchange.endExchange();
new Thread(server::stop).start();
break;
case "close":
session.close();
String requestMethod = exchange.getRequestMethod().toString();
switch(requestMethod) {
case "GET":
String info = getInstanceInfo(session);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
exchange.getResponseSender().send(info);
return;
case "POST":
String action = getAction(exchange);
if (action == null) return;

switch (action) {
case "terminate":
exchange.endExchange();
new Thread(server::stop).start();
break;
case "close":
session.close();
break;
default:
Utils.methodNotAllowed(exchange);
break;
}
break;
default:
Utils.invalidParameter(exchange, "action");
break;
}
}
Expand All @@ -105,20 +130,31 @@ protected void handleRequest(@NotNull HttpServerExchange exchange, @NotNull Sess
return;
}

String action = getAction(exchange);
if (action == null) return;

switch (action) {
case "terminate":
exchange.endExchange();
new Thread(server::stop).start();
break;
case "close":
player.close();
session.close();
String requestMethod = exchange.getRequestMethod().toString();
switch(requestMethod) {
case "GET":
String info = getInstanceInfo(session);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
exchange.getResponseSender().send(info);
return;
case "POST":
String action = getAction(exchange);
if (action == null) return;

switch (action) {
case "terminate":
exchange.endExchange();
new Thread(server::stop).start();
break;
case "close":
session.close();
break;
default:
Utils.methodNotAllowed(exchange);
break;
}
break;
default:
Utils.invalidParameter(exchange, "action");
break;
}
}
Expand Down