This repository has been archived by the owner on Jan 4, 2022. It is now read-only.
-
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.
Support guilds, provide more data to users & bug fixes
- Loading branch information
1 parent
6749011
commit 9bc006e
Showing
8 changed files
with
197 additions
and
27 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
src/commonMain/kotlin/com/gabriel/lunala/project/entity/Guild.kt
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,23 @@ | ||
package com.gabriel.lunala.project.entity | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class Guild( | ||
val id: Long, | ||
val locale: String, | ||
val partner: Boolean | ||
) | ||
|
||
@Serializable | ||
data class GuildCreateDTO( | ||
val id: Long, | ||
val locale: String, | ||
val partner: Boolean | ||
) | ||
|
||
@Serializable | ||
data class GuildUpdateDTO( | ||
val locale: String? = null, | ||
val partner: Boolean? = null | ||
) |
33 changes: 30 additions & 3 deletions
33
src/commonMain/kotlin/com/gabriel/lunala/project/entity/User.kt
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,12 +1,39 @@ | ||
package com.gabriel.lunala.project.entity | ||
|
||
import com.gabriel.lunala.project.util.PremiumType | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class User(val id: Long, val coins: Long, val planet: String) | ||
data class User( | ||
val id: Long, | ||
val ship: Int, | ||
val coins: Long, | ||
val equipment: Int, | ||
val crew: Int, | ||
val planet: String, | ||
val galaxy: String, | ||
val premium: PremiumType | ||
) | ||
|
||
@Serializable | ||
data class UserCreateDTO(val id: Long, val coins: Long, val planet: String) | ||
data class UserCreateDTO( | ||
val id: Long, | ||
val ship: Int, | ||
val coins: Long, | ||
val equipment: Int, | ||
val crew: Int, | ||
val planet: String, | ||
val galaxy: String, | ||
val premium: PremiumType | ||
) | ||
|
||
@Serializable | ||
data class UserUpdateDTO(val coins: Long?, val planet: String?) | ||
data class UserUpdateDTO( | ||
val ship: Int? = null, | ||
val coins: Long? = null, | ||
val equipment: Int? = null, | ||
val crew: Int? = null, | ||
val planet: String? = null, | ||
val galaxy: String? = null, | ||
val premium: PremiumType? = null | ||
) |
35 changes: 35 additions & 0 deletions
35
src/commonMain/kotlin/com/gabriel/lunala/project/service/GuildService.kt
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,35 @@ | ||
package com.gabriel.lunala.project.service | ||
|
||
import com.gabriel.lunala.project.LunalaWrapper | ||
import com.gabriel.lunala.project.entity.Guild | ||
import com.gabriel.lunala.project.entity.GuildCreateDTO | ||
import com.gabriel.lunala.project.entity.GuildUpdateDTO | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
|
||
class GuildService(private val wrapper: LunalaWrapper, private val route: String = "${wrapper.url}/guilds") { | ||
|
||
suspend fun create(create: GuildCreateDTO) = wrapper.client.post<Guild>(route) { | ||
header(HttpHeaders.Authorization, wrapper.key) | ||
header("Content-Type", "application/json; charset=utf-8") | ||
|
||
body = create | ||
} | ||
|
||
suspend fun retrieve(id: Long) = wrapper.client.get<Guild>("$route/$id") { | ||
header(HttpHeaders.Authorization, wrapper.key) | ||
header("Content-Type", "application/json; charset=utf-8") | ||
} | ||
|
||
suspend fun update(id: Long, update: GuildUpdateDTO): Guild = wrapper.client.put("$route/$id") { | ||
header(HttpHeaders.Authorization, wrapper.key) | ||
header("Content-Type", "application/json; charset=utf-8") | ||
|
||
body = update | ||
} | ||
|
||
suspend fun delete(id: Long) = wrapper.client.delete<HttpStatusCode>("$route/$id") { | ||
header(HttpHeaders.Authorization, wrapper.key) | ||
header("Content-Type", "application/json; charset=utf-8") | ||
} | ||
} |
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
15 changes: 4 additions & 11 deletions
15
src/commonMain/kotlin/com/gabriel/lunala/project/util/Client.kt
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,25 +1,18 @@ | ||
package com.gabriel.lunala.project.util | ||
|
||
import com.gabriel.lunala.project.LunalaWrapper | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.* | ||
import io.ktor.client.features.json.* | ||
import io.ktor.client.features.json.serializer.* | ||
import io.ktor.client.request.* | ||
import io.ktor.client.utils.* | ||
import io.ktor.http.* | ||
import io.ktor.util.* | ||
|
||
val ContentType.Application.Default | ||
get() = "application/json; charset=utf-8" | ||
|
||
@KtorExperimentalAPI | ||
fun LunalaWrapper.prepareClient() = client.config { | ||
fun HttpClientConfig<*>.prepareClient() = apply { | ||
install(JsonFeature) { | ||
serializer = KotlinxSerializer() | ||
acceptContentTypes = acceptContentTypes + ContentType("application", "json") | ||
} | ||
}.let { this } | ||
|
||
// TODO: workaround | ||
fun HttpRequestBuilder.applyHeaders(wrapper: LunalaWrapper) { | ||
header(HttpHeaders.Authorization, wrapper.key) | ||
header(HttpHeaders.ContentType, ContentType.Application.Json) | ||
} |
36 changes: 36 additions & 0 deletions
36
src/commonMain/kotlin/com/gabriel/lunala/project/util/Premium.kt
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,36 @@ | ||
package com.gabriel.lunala.project.util | ||
|
||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.descriptors.PrimitiveKind | ||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
|
||
@Serializable(with = PremiumSerializer::class) | ||
enum class PremiumType(val id: Int) { | ||
|
||
NONE(0), | ||
STAR(1), | ||
SUPERNOVA(2), | ||
BLACK_HOLE(3); | ||
|
||
companion object { | ||
fun findById(id: Int): PremiumType? = values().firstOrNull { | ||
it.id == id | ||
} | ||
} | ||
|
||
} | ||
|
||
class PremiumSerializer: KSerializer<PremiumType> { | ||
|
||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("premium_type", PrimitiveKind.INT) | ||
|
||
override fun deserialize(decoder: Decoder): PremiumType = | ||
PremiumType.findById(decoder.decodeInt())!! | ||
|
||
override fun serialize(encoder: Encoder, value: PremiumType) = | ||
encoder.encodeInt(value.ordinal) | ||
} |
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,49 @@ | ||
import com.gabriel.lunala.project.LunalaWrapper | ||
import com.gabriel.lunala.project.entity.Guild | ||
import com.gabriel.lunala.project.entity.GuildCreateDTO | ||
import com.gabriel.lunala.project.entity.GuildUpdateDTO | ||
import com.gabriel.lunala.project.service.GuildService | ||
import com.gabriel.lunala.project.util.prepareClient | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.cio.* | ||
import io.ktor.util.* | ||
import org.junit.jupiter.api.Test | ||
|
||
class GuildOperationsTest { | ||
|
||
@Test | ||
@KtorExperimentalAPI | ||
suspend fun `test operations`() { | ||
operate(GuildService(LunalaWrapper("http://localhost:8080/api", "key", client = HttpClient(CIO.create()) { | ||
prepareClient() | ||
}))) | ||
} | ||
|
||
} | ||
|
||
private suspend fun operate(service: GuildService) { | ||
create(service) { | ||
println("Created $this") | ||
} | ||
update(service) { | ||
println("Updated $this") | ||
} | ||
get(service) { | ||
println("Got $this") | ||
} | ||
delete(service) { | ||
println("Deleted $this") | ||
} | ||
} | ||
|
||
suspend fun create(service: GuildService, block: Guild.() -> Unit) = | ||
service.create(GuildCreateDTO(1, "en-us", false)).also(block) | ||
|
||
suspend fun update(service: GuildService, block: Guild.() -> Unit) = | ||
service.update(1, GuildUpdateDTO(locale = "pt-br")).also(block) | ||
|
||
suspend fun get(service: GuildService, block: Guild.() -> Unit) = | ||
service.retrieve(1).also(block) | ||
|
||
suspend fun delete(service: GuildService, id: Long = 1, block: Long.() -> Unit) = | ||
service.delete(id).also { block(id) } |
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