-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Added support to change the playback volume (#67)
Added HTTP endpoint to change the playback volume. Rules applied: - if single device is connected, then change the device volume - If multiple devices connected, then change only device that active - If no device is connected to player, then error is returned Written tests. Added docs. Small refactoring of classes. Implementation of #66
- Loading branch information
1 parent
38907f8
commit 5a2458d
Showing
30 changed files
with
735 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
### Change volume of the player | ||
|
||
Set the volume for the user’s current playback device. | ||
|
||
Note: the order of the commands are not guaranteed while using Player API. | ||
|
||
#### Request | ||
|
||
```http request | ||
PUT /player/volume | ||
Authorization: Bearer user_access_token | ||
``` | ||
|
||
Query parameters: | ||
- volume_percent - an integer from 0 to 100, where 0 means that device is muted and 100 max volume | ||
|
||
#### Response | ||
|
||
Status: | ||
- 204 No Content - command has been received | ||
- 400 Bad Request - invalid body, request or there is no active device. All responses with this status code have 'reason_code' in body, that can be used to determine type of the error | ||
- 500 Server Error - should never happen, but if so - please, create a new GitHub issue that can be used to reproduce the issue. | ||
|
||
See the [tests](../src/test/java/com/odeyalo/sonata/connect/controller/ChangePlayerVolumeEndpointTest.java) for further info | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/odeyalo/sonata/connect/exception/InvalidVolumeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.odeyalo.sonata.connect.exception; | ||
|
||
public final class InvalidVolumeException extends RuntimeException { | ||
|
||
public static InvalidVolumeException withCustomMessage(final String message) { | ||
return new InvalidVolumeException(message); | ||
} | ||
|
||
public static InvalidVolumeException withMessageAndCause(final String message, final Throwable cause) { | ||
return new InvalidVolumeException(message, cause); | ||
} | ||
|
||
public InvalidVolumeException() { | ||
super(); | ||
} | ||
|
||
public InvalidVolumeException(final String message) { | ||
super(message); | ||
} | ||
|
||
public InvalidVolumeException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public InvalidVolumeException(final Throwable cause) { | ||
super(cause); | ||
} | ||
} |
37 changes: 33 additions & 4 deletions
37
src/main/java/com/odeyalo/sonata/connect/exception/NoActiveDeviceException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,42 @@ | ||
package com.odeyalo.sonata.connect.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.experimental.StandardException; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Exception that should be thrown when there is no active device found for the given user | ||
*/ | ||
@StandardException | ||
@Getter | ||
public class NoActiveDeviceException extends RuntimeException implements ReasonCodeAware { | ||
final String reasonCode = "no_active_device"; | ||
public class NoActiveDeviceException extends PlayerCommandException { | ||
static final String REASON_CODE = "no_active_device"; | ||
static final String DEFAULT_MESSAGE = "At least one connected device is required to execute this command"; | ||
|
||
@NotNull | ||
public static NoActiveDeviceException defaultException() { | ||
return withCustomMessage(DEFAULT_MESSAGE); | ||
} | ||
|
||
@NotNull | ||
public static NoActiveDeviceException withCustomMessage(@NotNull final String message) { | ||
return new NoActiveDeviceException(message); | ||
} | ||
|
||
@NotNull | ||
public static NoActiveDeviceException withMessageAndCause(@NotNull final String message, | ||
@NotNull final Throwable cause) { | ||
return new NoActiveDeviceException(message, cause); | ||
} | ||
|
||
public NoActiveDeviceException() { | ||
super(DEFAULT_MESSAGE, REASON_CODE); | ||
} | ||
|
||
public NoActiveDeviceException(@NotNull final String message) { | ||
super(message, REASON_CODE); | ||
} | ||
|
||
private NoActiveDeviceException(@NotNull final String message, | ||
@NotNull final Throwable cause) { | ||
super(message, REASON_CODE, cause); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/odeyalo/sonata/connect/exception/PlayerCommandException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.odeyalo.sonata.connect.exception; | ||
|
||
public class PlayerCommandException extends RuntimeException implements ReasonCodeAware { | ||
private final String reasonCode; | ||
|
||
public PlayerCommandException(final String message, final String reasonCode) { | ||
super(message); | ||
this.reasonCode = reasonCode; | ||
} | ||
|
||
public PlayerCommandException(final String message, final String reasonCode, final Throwable cause) { | ||
super(message, cause); | ||
this.reasonCode = reasonCode; | ||
} | ||
|
||
@Override | ||
public String getReasonCode() { | ||
return reasonCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/odeyalo/sonata/connect/model/Volume.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.odeyalo.sonata.connect.model; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Represent a volume for the device | ||
* Volume MUST BE in range from 0 to 100 | ||
* | ||
* @param value - an integer that represent a volume | ||
*/ | ||
public record Volume(int value) { | ||
/** | ||
* @throws IllegalStateException if a volume is in invalid range | ||
*/ | ||
public Volume { | ||
Assert.state(value >= 0, "Volume cannot be negative!"); | ||
Assert.state(value <= 100, "Volume must be in range 0 - 100!"); | ||
} | ||
|
||
@NotNull | ||
public static Volume from(final int value) { | ||
return new Volume(value); | ||
} | ||
|
||
@NotNull | ||
public static Volume fromInt(final int value) { | ||
return from(value); | ||
} | ||
|
||
@NotNull | ||
public static Volume muted() { | ||
return new Volume(0); | ||
} | ||
|
||
public int asInt() { | ||
return value; | ||
} | ||
} |
Oops, something went wrong.