Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 59f0d42
Author: StayBlue <[email protected]>
Date:   Sun Aug 11 01:25:36 2024 -0700

    Next release

commit a2404a7
Merge: 6ddb488 c40a109
Author: capyrightdev <[email protected]>
Date:   Thu Aug 8 17:44:32 2024 -0500

    Merge pull request #6 from desolationdev/master

    Added Thumbnails!

commit c40a109
Author: desolationdev <[email protected]>
Date:   Tue Aug 6 00:27:32 2024 -0400

    Cleaning up imports

commit 17b547a
Author: desolationdev <[email protected]>
Date:   Tue Aug 6 00:23:07 2024 -0400

    Added Thumbnails!

commit 6ddb488
Author: StayBlue <[email protected]>
Date:   Sat Jun 1 23:11:31 2024 -0700

    Added response codes to exceptions to certain endpoints

    This will likely be expanded in the future to more than just these methods.

commit 480144d
Author: StayBlue <[email protected]>
Date:   Fri May 31 18:58:10 2024 -0700

    Next release

commit 5f8cd4d
Author: StayBlue <[email protected]>
Date:   Thu May 30 22:40:40 2024 -0700

    Fixed an issue where requests would repeat if they required authentication

    This should fix an issue where the tokens wouldn't be stored, making it so that if the requests being made required authentication, it would always demand an additional request just to get the token.

commit 10edc5d
Author: StayBlue <[email protected]>
Date:   Thu May 30 22:19:09 2024 -0700

    Added a more descriptive error message when ranking user fails
  • Loading branch information
capyrightdev committed Aug 15, 2024
1 parent b43e612 commit b37060e
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
kotlin.code.style=official

org.gradle.caching=true
version = 0.0.2-SNAPSHOT
version = 0.0.4-SNAPSHOT
54 changes: 54 additions & 0 deletions src/main/kotlin/dev/roava/api/ThumbnailApi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* MIT License
*
* Copyright (c) 2024 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.user.ThumbnailListData
import retrofit2.Call
import retrofit2.http.GET
import retrofit2.http.Query

interface ThumbnailApi {
@GET("/v1/users/avatar")
fun getAvatar(
@Query("userIds") userId: Long,
@Query("size") size: String,
@Query("format") format: String,
@Query("isCircular") circular: Boolean
): Call<ThumbnailListData>
@GET("/v1/users/avatar-headshot")
fun getHeadShot(
@Query("userIds") userId: Long,
@Query("size") size: String,
@Query("format") format: String,
@Query("isCircular") circular: Boolean
): Call<ThumbnailListData>
@GET("/v1/users/avatar-bust")
fun getBust(
@Query("userIds") userId: Long,
@Query("size") size: String,
@Query("format") format: String,
@Query("isCircular") circular: Boolean
): Call<ThumbnailListData>
}
2 changes: 1 addition & 1 deletion src/main/kotlin/dev/roava/client/RoavaClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RoavaClient {
throw RuntimeException("Your cookie is not set properly! Please make sure that you include the entirety of the string, including the _|WARNING:-")
}

request = dev.roava.client.RoavaRequest(cookie)
request = RoavaRequest(cookie)
this.cookie = cookie

try {
Expand Down
17 changes: 13 additions & 4 deletions src/main/kotlin/dev/roava/client/RoavaInterceptor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,20 @@ import okhttp3.Response
* For Intercepting calls and adding the X-CSRF-TOKEN to the header if the original request failed (for internal use only).
*/
internal class RoavaInterceptor: Interceptor {
private val header = "X-CSRF-TOKEN"

private var token: String? = null

override fun intercept(chain: Interceptor.Chain): Response {
var response = chain.proceed(chain.request())
val orig = chain.request()
val request = orig.newBuilder()
.header(header, token ?: "")
.build()

var response = chain.proceed(request)

if (response.code() == 403 && !hasToken(response)) {
val token: String? = response.header("X-CSRF-TOKEN")
token = response.header(header)

token?.let {
response.close()
Expand All @@ -50,15 +59,15 @@ internal class RoavaInterceptor: Interceptor {

private fun hasToken(response: Response?): Boolean {
response?.let {
return !it.request().header("X-CSRF-TOKEN").isNullOrEmpty()
return !it.request().header(header).isNullOrEmpty()
}

return false
}

private fun retry(request: Request?, token: String): Request? {
return request?.newBuilder()
?.header("X-CSRF-TOKEN", token)
?.header(header, token)
?.build()
}
}
22 changes: 14 additions & 8 deletions src/main/kotlin/dev/roava/group/Group.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import dev.roava.client.RoavaRequest
import dev.roava.json.group.GroupData
import dev.roava.json.group.RoleRequest
import dev.roava.user.User
import retrofit2.HttpException

/**
* A class which represents a Group.
Expand Down Expand Up @@ -234,16 +235,21 @@ class Group {
roleNumber = getRole(roleNumber).id
}

runCatching {
val result = runCatching {
client.request.createRequest(GroupApi::class.java, "groups")
.rankUser(id, userId, RoleRequest(roleNumber))
.execute().isSuccessful.also {
if (!it) {
throw RuntimeException("Could not rank the provided user!")
}
}
}.onFailure {
throw RuntimeException("Could not rank the provided user!")
.execute()
}

result.onFailure { exception ->
if (exception is HttpException) {
val errorCode = exception.code()
val message = exception.message()

throw RuntimeException("Ranking user with id $userId failed with message \"$message\" and response code $errorCode")
} else {
throw RuntimeException("An unknown error has occurred while ranking the user!")
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions src/main/kotlin/dev/roava/json/user/ThumbnailData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* MIT License
*
* Copyright (c) 2024 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.user

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
@JsonIgnoreProperties(ignoreUnknown = true)
data class ThumbnailData(
@JsonProperty("imageUrl")
val thumbnail: String?
)
33 changes: 33 additions & 0 deletions src/main/kotlin/dev/roava/json/user/ThumbnailListData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* MIT License
*
* Copyright (c) 2024 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.user

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
@JsonIgnoreProperties(ignoreUnknown = true)
data class ThumbnailListData (
@JsonProperty("data")
val data: List<ThumbnailData>?
)
117 changes: 108 additions & 9 deletions src/main/kotlin/dev/roava/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@

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.api.*
import dev.roava.client.RoavaRequest
import dev.roava.group.Group
import dev.roava.json.user.UserData
import dev.roava.json.user.UserNameRequest
import retrofit2.HttpException

/**
* A class which represents a User which is not authenticated by the [dev.roava.client.RoavaClient].
Expand Down Expand Up @@ -154,22 +152,123 @@ class User {
fun getGroups(): List<Group> {
val groups = mutableListOf<Group>()

try {
val groupData = request.createRequest(GroupApi::class.java, "groups")
val result = runCatching {
request.createRequest(GroupApi::class.java, "groups")
.getUserRoleInfo(id)
.execute()
.body()
}

result.onFailure { exception ->
if (exception is HttpException) {
val errorCode = exception.code()
val message = exception.message()

throw RuntimeException("Ranking user with id ${this.id} failed with message \"$message\" and response code $errorCode")
} else {
throw RuntimeException("An unknown error has occurred while fetching the user's groups!")
}
}.onSuccess {
val groupData = it.body()

for (group in groupData?.data!!) {
groups.add(Group(group.groupData!!))
}
} catch(exception: Exception) {
throw RuntimeException("Could not fetch the user's groups!")
}

return groups.toList()
}

/**
* Method to get a User's Avatar
*
* Available Values:
* 30x30, 48x48, 60x60, 75x75, 100x100, 110x110, 140x140, 150x150, 150x200, 180x180, 250x250, 352x352, 420x420, 720x720
* @throws[RuntimeException]
* @return[String]
*/
@Throws(RuntimeException::class)
fun getAvatar(size: String,isCircular: Boolean): String {
var thumbnail = ""
val result = runCatching {
request.createRequest(ThumbnailApi::class.java, "thumbnails")
.getAvatar(id,size,"Png",isCircular)
.execute()
}
result.onFailure { exception ->
if (exception is HttpException) {
val errorCode = exception.code()
val message = exception.message()

throw RuntimeException("Grabbing thumbnail of user with id ${this.id} failed with message \"$message\" and response code $errorCode")
} else {
throw RuntimeException("an unknown error has occurred while fetching the user's thumbnail!\n${exception.message}")
}
}.onSuccess {
thumbnail = it.body()?.data?.get(0)?.thumbnail?: ""
}
return thumbnail
}

/**
* Method to get a User's Headshot
*
* Available Values:
* 30x30, 48x48, 60x60, 75x75, 100x100, 110x110, 140x140, 150x150, 150x200, 180x180, 250x250, 352x352, 420x420, 720x720
* @throws[RuntimeException]
* @return[String]
*/
@Throws(RuntimeException::class)
fun getHeadShot(size: String,isCircular: Boolean): String {
var thumbnail = ""
val result = runCatching {
request.createRequest(ThumbnailApi::class.java, "thumbnails")
.getHeadShot(id, size, "Png", isCircular)
.execute()
}
result.onFailure { exception ->
if (exception is HttpException) {
val errorCode = exception.code()
val message = exception.message()

throw RuntimeException("Grabbing headshot of user with id ${this.id} failed with message \"$message\" and response code $errorCode")
} else {
throw RuntimeException("an unknown error has occurred while fetching the user's headshot!\n${exception.message}")
}
}.onSuccess {
thumbnail = it.body()?.data?.get(0)?.thumbnail ?: ""
}
return thumbnail
}
/**
* Method to get a User's Bust
*
* Available Values:
* 48x48, 50x50, 60x60, 75x75, 100x100, 150x150, 180x180, 352x352, 420x420
* @throws[RuntimeException]
* @return[String]
*/
@Throws(RuntimeException::class)
fun getBust(size: String,isCircular: Boolean): String {
var thumbnail = ""
val result = runCatching {
request.createRequest(ThumbnailApi::class.java, "thumbnails")
.getBust(id, size, "Png", isCircular)
.execute()
}
result.onFailure { exception ->
if (exception is HttpException) {
val errorCode = exception.code()
val message = exception.message()

throw RuntimeException("Grabbing bust of user with id ${this.id} failed with message \"$message\" and response code $errorCode")
} else {
throw RuntimeException("an unknown error has occurred while fetching the user's bust!\n${exception.message}")
}
}.onSuccess {
thumbnail = it.body()?.data?.get(0)?.thumbnail ?: ""
}
return thumbnail
}
/**
* Method to get if a User is in a group
*
Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/dev/roava/user/UserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package dev.roava.user
import org.junit.jupiter.api.Test

import org.junit.jupiter.api.Assertions.*
import kotlin.test.assertContains

internal class UserTest {
private val testUser = User(3838771115)
Expand Down Expand Up @@ -91,4 +92,16 @@ internal class UserTest {
fun testDescription() {
assertEquals(testUser.description, "This is a test description")
}
@Test
fun testAvatar() {
assertContains(testUser.getAvatar("30x30",true), "https://tr.rbxcdn.com/")
}
@Test
fun testHeadShot(){
assertContains(testUser.getHeadShot("48x48", true), "https://tr.rbxcdn.com/")
}
@Test
fun testBust(){
assertContains(testUser.getBust("48x48",true), "https://tr.rbxcdn.com/")
}
}

0 comments on commit b37060e

Please sign in to comment.