Skip to content

Commit

Permalink
Merge pull request #4 from RoavaDev/inventory-api
Browse files Browse the repository at this point in the history
Inventory API implementation
  • Loading branch information
StayBlue authored Jun 7, 2023
2 parents 3ac3263 + 3e77d78 commit 04b4c68
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/kotlin/dev/roava/api/InventoryApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 getUserItems(@Path("userId") userId: Long, @Path("itemType") itemType: String, @Path("itemId") itemId: Long): Call<ItemListData>

@GET("/v1/users/{userId}/items/{itemType}/{itemId}/is-owned")
fun getIsOwned(@Path("userId") userId: Long, @Path("itemType") itemType: String, @Path("itemId") itemId: Long): Call<Boolean>
}
40 changes: 40 additions & 0 deletions src/main/kotlin/dev/roava/json/inventory/ItemData.kt
Original file line number Diff line number Diff line change
@@ -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
)
34 changes: 34 additions & 0 deletions src/main/kotlin/dev/roava/json/inventory/ItemListData.kt
Original file line number Diff line number Diff line change
@@ -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<ItemData>
)
20 changes: 20 additions & 0 deletions src/main/kotlin/dev/roava/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -184,4 +185,23 @@ class User {

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")
.getIsOwned(id, "gamepass", gamepassId)
.execute()
.body()

gamepasses ?: false
}.getOrElse {
throw RuntimeException("Could not fetch the user's items!")
}
}
7 changes: 7 additions & 0 deletions src/test/kotlin/dev/roava/user/UserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,11 @@ internal class UserTest {
fun isInGroup() {
assertTrue(testUser.isInGroup(15771240))
}

@Test
fun hasGamepass() {
val user = User(72242614)

assertTrue(user.hasGamepass(13827472))
}
}

0 comments on commit 04b4c68

Please sign in to comment.