From 607da0e61ec1931d71685a8d8464a117b3448139 Mon Sep 17 00:00:00 2001 From: StayBlue Date: Wed, 31 May 2023 20:43:17 -0700 Subject: [PATCH 1/5] Initial Inventory API implementation --- src/main/kotlin/dev/roava/api/InventoryApi.kt | 35 ++++++++++++++++ .../dev/roava/json/inventory/ItemData.kt | 40 +++++++++++++++++++ .../dev/roava/json/inventory/ItemListData.kt | 34 ++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 src/main/kotlin/dev/roava/api/InventoryApi.kt create mode 100644 src/main/kotlin/dev/roava/json/inventory/ItemData.kt create mode 100644 src/main/kotlin/dev/roava/json/inventory/ItemListData.kt diff --git a/src/main/kotlin/dev/roava/api/InventoryApi.kt b/src/main/kotlin/dev/roava/api/InventoryApi.kt new file mode 100644 index 0000000..07992b3 --- /dev/null +++ b/src/main/kotlin/dev/roava/api/InventoryApi.kt @@ -0,0 +1,35 @@ +/* + * MIT License + * + * Copyright (c) 2023 RoavaDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package dev.roava.api + +import dev.roava.json.inventory.ItemListData +import retrofit2.Call +import retrofit2.http.GET +import retrofit2.http.Path + +interface InventoryApi { + @GET("/v1/users/{userId}/items/{itemType}/{itemId}") + fun exileUser(@Path("userId") userId: Long, @Path("itemType") itemType: String, @Path("itemId") itemId: Long): Call +} \ No newline at end of file diff --git a/src/main/kotlin/dev/roava/json/inventory/ItemData.kt b/src/main/kotlin/dev/roava/json/inventory/ItemData.kt new file mode 100644 index 0000000..dd55c0e --- /dev/null +++ b/src/main/kotlin/dev/roava/json/inventory/ItemData.kt @@ -0,0 +1,40 @@ +/* + * MIT License + * + * Copyright (c) 2023 RoavaDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package dev.roava.json.inventory + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties +import com.fasterxml.jackson.annotation.JsonProperty + +@JsonIgnoreProperties(ignoreUnknown = true) +data class ItemData( + @JsonProperty("type") + val type: String, + @JsonProperty("id") + val id: Long, + @JsonProperty("name") + val name: String, + @JsonProperty("instanceId") + val instanceId: Long +) diff --git a/src/main/kotlin/dev/roava/json/inventory/ItemListData.kt b/src/main/kotlin/dev/roava/json/inventory/ItemListData.kt new file mode 100644 index 0000000..6e2a48f --- /dev/null +++ b/src/main/kotlin/dev/roava/json/inventory/ItemListData.kt @@ -0,0 +1,34 @@ +/* + * MIT License + * + * Copyright (c) 2023 RoavaDev + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package dev.roava.json.inventory + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties +import com.fasterxml.jackson.annotation.JsonProperty + +@JsonIgnoreProperties(ignoreUnknown = true) +data class ItemListData( + @JsonProperty("data") + val items: List +) From f5f4fad1d983b0c8308ce0f33a9f30c9873a1ae7 Mon Sep 17 00:00:00 2001 From: StayBlue Date: Wed, 31 May 2023 22:38:16 -0700 Subject: [PATCH 2/5] Add hasGamepass method to User --- src/main/kotlin/dev/roava/api/InventoryApi.kt | 2 +- src/main/kotlin/dev/roava/user/User.kt | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/dev/roava/api/InventoryApi.kt b/src/main/kotlin/dev/roava/api/InventoryApi.kt index 07992b3..77082e1 100644 --- a/src/main/kotlin/dev/roava/api/InventoryApi.kt +++ b/src/main/kotlin/dev/roava/api/InventoryApi.kt @@ -31,5 +31,5 @@ import retrofit2.http.Path interface InventoryApi { @GET("/v1/users/{userId}/items/{itemType}/{itemId}") - fun exileUser(@Path("userId") userId: Long, @Path("itemType") itemType: String, @Path("itemId") itemId: Long): Call + fun getUserItems(@Path("userId") userId: Long, @Path("itemType") itemType: String, @Path("itemId") itemId: Long): Call } \ No newline at end of file diff --git a/src/main/kotlin/dev/roava/user/User.kt b/src/main/kotlin/dev/roava/user/User.kt index 4ce77a6..3dd7fe8 100644 --- a/src/main/kotlin/dev/roava/user/User.kt +++ b/src/main/kotlin/dev/roava/user/User.kt @@ -26,6 +26,7 @@ package dev.roava.user import dev.roava.api.FriendApi import dev.roava.api.GroupApi +import dev.roava.api.InventoryApi import dev.roava.api.UserApi import dev.roava.client.RoavaRequest import dev.roava.group.Group @@ -184,4 +185,46 @@ class User { return false } + + /** + * Method to get if a User has a gamepass + */ + + /* + fun hasGamepass(gamepassId: Long): Boolean { + runCatching { + val gamepasses = request.createRequest(InventoryApi::class.java, "inventory") + .getUserItems(id, "gamepass", gamepassId) + .execute() + .body()?.items + + gamepasses?.let { + return it.isNotEmpty() + } + }.onFailure { + throw RuntimeException("Could not fetch the user's items!") + } + + return false + } + */ + + /** + * Method to get if a User has a gamepass + * + * @param[gamepassId] The gamepass's ID + * @throws[RuntimeException] + * @return[Boolean] + */ + @Throws(RuntimeException::class) + fun hasGamepass(gamepassId: Long) = runCatching { + val gamepasses = request.createRequest(InventoryApi::class.java, "inventory") + .getUserItems(id, "gamepass", gamepassId) + .execute() + .body()?.items + + gamepasses?.isNotEmpty() ?: false + }.getOrElse { + throw RuntimeException("Could not fetch the user's items!") + } } \ No newline at end of file From ca10b15e278f72106a03ae5fce1e6e74fffc6524 Mon Sep 17 00:00:00 2001 From: StayBlue Date: Tue, 6 Jun 2023 18:07:59 -0700 Subject: [PATCH 3/5] Add is-owned endpoint --- src/main/kotlin/dev/roava/api/InventoryApi.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/kotlin/dev/roava/api/InventoryApi.kt b/src/main/kotlin/dev/roava/api/InventoryApi.kt index 77082e1..70b2d4a 100644 --- a/src/main/kotlin/dev/roava/api/InventoryApi.kt +++ b/src/main/kotlin/dev/roava/api/InventoryApi.kt @@ -32,4 +32,7 @@ import retrofit2.http.Path interface InventoryApi { @GET("/v1/users/{userId}/items/{itemType}/{itemId}") fun getUserItems(@Path("userId") userId: Long, @Path("itemType") itemType: String, @Path("itemId") itemId: Long): Call + + @GET("/v1/users/{userId}/items/{itemType}/{itemId}/is-owned") + fun getIsOwned(@Path("userId") userId: Long, @Path("itemType") itemType: String, @Path("itemId") itemId: Long): Call } \ No newline at end of file From c3fae8ebb114ca819d2e1f41ba8a5060cb8d9002 Mon Sep 17 00:00:00 2001 From: StayBlue Date: Tue, 6 Jun 2023 18:08:23 -0700 Subject: [PATCH 4/5] Update hasGamepass to use is-owned endpoint --- src/main/kotlin/dev/roava/user/User.kt | 29 +++----------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/src/main/kotlin/dev/roava/user/User.kt b/src/main/kotlin/dev/roava/user/User.kt index 3dd7fe8..0c971f8 100644 --- a/src/main/kotlin/dev/roava/user/User.kt +++ b/src/main/kotlin/dev/roava/user/User.kt @@ -186,29 +186,6 @@ class User { return false } - /** - * Method to get if a User has a gamepass - */ - - /* - fun hasGamepass(gamepassId: Long): Boolean { - runCatching { - val gamepasses = request.createRequest(InventoryApi::class.java, "inventory") - .getUserItems(id, "gamepass", gamepassId) - .execute() - .body()?.items - - gamepasses?.let { - return it.isNotEmpty() - } - }.onFailure { - throw RuntimeException("Could not fetch the user's items!") - } - - return false - } - */ - /** * Method to get if a User has a gamepass * @@ -219,11 +196,11 @@ class User { @Throws(RuntimeException::class) fun hasGamepass(gamepassId: Long) = runCatching { val gamepasses = request.createRequest(InventoryApi::class.java, "inventory") - .getUserItems(id, "gamepass", gamepassId) + .getIsOwned(id, "gamepass", gamepassId) .execute() - .body()?.items + .body() - gamepasses?.isNotEmpty() ?: false + gamepasses ?: false }.getOrElse { throw RuntimeException("Could not fetch the user's items!") } From 3e77d78fac1d6ddaa1f272696e8ae6cfed43fc23 Mon Sep 17 00:00:00 2001 From: StayBlue Date: Tue, 6 Jun 2023 18:08:31 -0700 Subject: [PATCH 5/5] Add hasGamepass test --- src/test/kotlin/dev/roava/user/UserTest.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/kotlin/dev/roava/user/UserTest.kt b/src/test/kotlin/dev/roava/user/UserTest.kt index 78d07fd..1a33560 100644 --- a/src/test/kotlin/dev/roava/user/UserTest.kt +++ b/src/test/kotlin/dev/roava/user/UserTest.kt @@ -79,4 +79,11 @@ internal class UserTest { fun isInGroup() { assertTrue(testUser.isInGroup(15771240)) } + + @Test + fun hasGamepass() { + val user = User(72242614) + + assertTrue(user.hasGamepass(13827472)) + } } \ No newline at end of file