Skip to content

Commit 17b547a

Browse files
committed
Added Thumbnails!
1 parent 6ddb488 commit 17b547a

File tree

5 files changed

+228
-4
lines changed

5 files changed

+228
-4
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 RoavaDev
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package dev.roava.api
26+
27+
import dev.roava.json.user.ThumbnailListData
28+
import retrofit2.Call
29+
import retrofit2.http.GET
30+
import retrofit2.http.Query
31+
32+
interface ThumbnailApi {
33+
@GET("/v1/users/avatar")
34+
fun getAvatar(
35+
@Query("userIds") userId: Long,
36+
@Query("size") size: String,
37+
@Query("format") format: String,
38+
@Query("isCircular") circular: Boolean
39+
): Call<ThumbnailListData>
40+
@GET("/v1/users/avatar-headshot")
41+
fun getHeadShot(
42+
@Query("userIds") userId: Long,
43+
@Query("size") size: String,
44+
@Query("format") format: String,
45+
@Query("isCircular") circular: Boolean
46+
): Call<ThumbnailListData>
47+
@GET("/v1/users/avatar-bust")
48+
fun getBust(
49+
@Query("userIds") userId: Long,
50+
@Query("size") size: String,
51+
@Query("format") format: String,
52+
@Query("isCircular") circular: Boolean
53+
): Call<ThumbnailListData>
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 RoavaDev
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package dev.roava.json.user
26+
27+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
28+
import com.fasterxml.jackson.annotation.JsonProperty
29+
@JsonIgnoreProperties(ignoreUnknown = true)
30+
data class ThumbnailData(
31+
@JsonProperty("imageUrl")
32+
val thumbnail: String?
33+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2024 RoavaDev
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package dev.roava.json.user
26+
27+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
28+
import com.fasterxml.jackson.annotation.JsonProperty
29+
@JsonIgnoreProperties(ignoreUnknown = true)
30+
data class ThumbnailListData (
31+
@JsonProperty("data")
32+
val data: List<ThumbnailData>?
33+
)

src/main/kotlin/dev/roava/user/User.kt

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@
2424

2525
package dev.roava.user
2626

27-
import dev.roava.api.FriendApi
28-
import dev.roava.api.GroupApi
29-
import dev.roava.api.InventoryApi
30-
import dev.roava.api.UserApi
27+
import com.fasterxml.jackson.databind.ObjectMapper
28+
import dev.roava.api.*
3129
import dev.roava.client.RoavaRequest
3230
import dev.roava.group.Group
31+
import dev.roava.json.user.ThumbnailListData
3332
import dev.roava.json.user.UserData
3433
import dev.roava.json.user.UserNameRequest
3534
import retrofit2.HttpException
35+
import java.net.URI
3636

3737
/**
3838
* A class which represents a User which is not authenticated by the [dev.roava.client.RoavaClient].
@@ -181,6 +181,97 @@ class User {
181181
return groups.toList()
182182
}
183183

184+
/**
185+
* Method to get a User's Avatar
186+
*
187+
* Available Values:
188+
* 30x30, 48x48, 60x60, 75x75, 100x100, 110x110, 140x140, 150x150, 150x200, 180x180, 250x250, 352x352, 420x420, 720x720
189+
* @throws[RuntimeException]
190+
* @return[String]
191+
*/
192+
@Throws(RuntimeException::class)
193+
fun getAvatar(size: String,isCircular: Boolean): String {
194+
var thumbnail = ""
195+
val result = runCatching {
196+
request.createRequest(ThumbnailApi::class.java, "thumbnails")
197+
.getAvatar(id,size,"Png",isCircular)
198+
.execute()
199+
}
200+
result.onFailure { exception ->
201+
if (exception is HttpException) {
202+
val errorCode = exception.code()
203+
val message = exception.message()
204+
205+
throw RuntimeException("Grabbing thumbnail of user with id ${this.id} failed with message \"$message\" and response code $errorCode")
206+
} else {
207+
throw RuntimeException("an unknown error has occurred while fetching the user's thumbnail!\n${exception.message}")
208+
}
209+
}.onSuccess {
210+
thumbnail = it.body()?.data?.get(0)?.thumbnail?: ""
211+
}
212+
return thumbnail
213+
}
214+
215+
/**
216+
* Method to get a User's Headshot
217+
*
218+
* Available Values:
219+
* 30x30, 48x48, 60x60, 75x75, 100x100, 110x110, 140x140, 150x150, 150x200, 180x180, 250x250, 352x352, 420x420, 720x720
220+
* @throws[RuntimeException]
221+
* @return[String]
222+
*/
223+
@Throws(RuntimeException::class)
224+
fun getHeadShot(size: String,isCircular: Boolean): String {
225+
var thumbnail = ""
226+
val result = runCatching {
227+
request.createRequest(ThumbnailApi::class.java, "thumbnails")
228+
.getHeadShot(id, size, "Png", isCircular)
229+
.execute()
230+
}
231+
result.onFailure { exception ->
232+
if (exception is HttpException) {
233+
val errorCode = exception.code()
234+
val message = exception.message()
235+
236+
throw RuntimeException("Grabbing headshot of user with id ${this.id} failed with message \"$message\" and response code $errorCode")
237+
} else {
238+
throw RuntimeException("an unknown error has occurred while fetching the user's headshot!\n${exception.message}")
239+
}
240+
}.onSuccess {
241+
thumbnail = it.body()?.data?.get(0)?.thumbnail ?: ""
242+
}
243+
return thumbnail
244+
}
245+
/**
246+
* Method to get a User's Bust
247+
*
248+
* Available Values:
249+
* 48x48, 50x50, 60x60, 75x75, 100x100, 150x150, 180x180, 352x352, 420x420
250+
* @throws[RuntimeException]
251+
* @return[String]
252+
*/
253+
@Throws(RuntimeException::class)
254+
fun getBust(size: String,isCircular: Boolean): String {
255+
var thumbnail = ""
256+
val result = runCatching {
257+
request.createRequest(ThumbnailApi::class.java, "thumbnails")
258+
.getBust(id, size, "Png", isCircular)
259+
.execute()
260+
}
261+
result.onFailure { exception ->
262+
if (exception is HttpException) {
263+
val errorCode = exception.code()
264+
val message = exception.message()
265+
266+
throw RuntimeException("Grabbing bust of user with id ${this.id} failed with message \"$message\" and response code $errorCode")
267+
} else {
268+
throw RuntimeException("an unknown error has occurred while fetching the user's bust!\n${exception.message}")
269+
}
270+
}.onSuccess {
271+
thumbnail = it.body()?.data?.get(0)?.thumbnail ?: ""
272+
}
273+
return thumbnail
274+
}
184275
/**
185276
* Method to get if a User is in a group
186277
*

src/test/kotlin/dev/roava/user/UserTest.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ package dev.roava.user
2727
import org.junit.jupiter.api.Test
2828

2929
import org.junit.jupiter.api.Assertions.*
30+
import kotlin.test.assertContains
3031

3132
internal class UserTest {
3233
private val testUser = User(3838771115)
@@ -91,4 +92,16 @@ internal class UserTest {
9192
fun testDescription() {
9293
assertEquals(testUser.description, "This is a test description")
9394
}
95+
@Test
96+
fun testAvatar() {
97+
assertContains(testUser.getAvatar("30x30",true), "https://tr.rbxcdn.com/")
98+
}
99+
@Test
100+
fun testHeadShot(){
101+
assertContains(testUser.getHeadShot("48x48", true), "https://tr.rbxcdn.com/")
102+
}
103+
@Test
104+
fun testBust(){
105+
assertContains(testUser.getBust("48x48",true), "https://tr.rbxcdn.com/")
106+
}
94107
}

0 commit comments

Comments
 (0)