|
| 1 | +package de.sonallux.spotify.core; |
| 2 | + |
| 3 | +import de.sonallux.spotify.core.model.SpotifyWebApi; |
| 4 | +import de.sonallux.spotify.core.model.SpotifyWebApiEndpoint; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import static de.sonallux.spotify.core.model.SpotifyWebApiEndpoint.ParameterLocation.*; |
| 11 | + |
| 12 | +public class EndpointHelper { |
| 13 | + |
| 14 | + public static void splitEndpoints(SpotifyWebApi spotifyWebApi) throws IllegalArgumentException { |
| 15 | + EndpointSplitter.splitEndpoints(spotifyWebApi); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Fixes duplicated endpoint parameters. |
| 20 | + * Some endpoints allow to pass data either via query argument or via body. As the url has a length limit, |
| 21 | + * passing to much data in the query string might result in an error response. Therefore this method removes |
| 22 | + * the option to pass the data via query argument and makes the body parameter mandatory. |
| 23 | + * @param spotifyWebApi the spotify web api documentation |
| 24 | + */ |
| 25 | + public static void fixDuplicateEndpointParameters(SpotifyWebApi spotifyWebApi) { |
| 26 | + spotifyWebApi.getCategoryList().stream() |
| 27 | + .flatMap(c -> c.getEndpointList().stream()) |
| 28 | + .forEach(EndpointHelper::fixDuplicateEndpointParameters); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Fixes duplicated endpoint parameters. |
| 33 | + * Some endpoints allow to pass data either via query argument or via body. As the url has a length limit, |
| 34 | + * passing to much data in the query string might result in an error response. Therefore this method removes |
| 35 | + * the option to pass the data via query argument and makes the body parameter mandatory. |
| 36 | + * @param endpoint the spotify api endpoint to fix |
| 37 | + */ |
| 38 | + public static void fixDuplicateEndpointParameters(SpotifyWebApiEndpoint endpoint) { |
| 39 | + var duplicates = endpoint.getParameters().stream() |
| 40 | + .collect(Collectors.groupingBy(SpotifyWebApiEndpoint.Parameter::getName)) |
| 41 | + .entrySet().stream() |
| 42 | + .filter(e -> e.getValue().size() > 1) |
| 43 | + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); |
| 44 | + |
| 45 | + duplicates.forEach((paramName, parameters) -> { |
| 46 | + if (!parameters.stream().map(SpotifyWebApiEndpoint.Parameter::getLocation).sorted().collect(Collectors.toList()).equals(List.of(QUERY, BODY))) { |
| 47 | + System.err.println("Endpoint " + endpoint.getName() + " has unfixable duplicate parameters"); |
| 48 | + return; |
| 49 | + } |
| 50 | + endpoint.getParameters().removeIf(p -> p.getLocation() == QUERY && paramName.equals(p.getName())); |
| 51 | + for (var param : endpoint.getParameters()) { |
| 52 | + if (param.getLocation() == BODY && paramName.equals(param.getName())) { |
| 53 | + if (!("endpoint-add-tracks-to-playlist".equals(endpoint.getId()) && "position".equals(param.getName()))) { |
| 54 | + param.setRequired(true); |
| 55 | + } |
| 56 | + } else if (param.getLocation() == HEADER && "Content-Type".equals(param.getName())) { |
| 57 | + param.setRequired(true); |
| 58 | + } |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | +} |
0 commit comments