All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.bselzer.gw2.v2.client.instance.Gw2Client.kt Maven / Gradle / Ivy

The newest version!
package com.bselzer.gw2.v2.client.instance

import com.bselzer.gw2.v2.client.constant.Endpoints
import com.bselzer.gw2.v2.client.extension.bearer
import com.bselzer.gw2.v2.client.extension.language
import com.bselzer.gw2.v2.client.extension.schemaVersion
import com.bselzer.gw2.v2.model.serialization.Modules
import io.ktor.client.*
import io.ktor.client.plugins.*
import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.utils.io.core.*
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonBuilder

/**
 * The GW2 client.
 * @see the wiki
 */
open class Gw2Client(
    private var httpClient: HttpClient = HttpClient(),
    private var json: Json = Modules.JSON,
    private var configuration: Gw2ClientConfiguration = Gw2ClientConfiguration()
) : Closeable {
    /**
     * The account client.
     * @see the wiki
     */
    lateinit var account: AccountClient

    /**
     * The achievement client.
     * @see the wiki
     */
    lateinit var achievement: AchievementClient

    /**
     * The backstory client.
     * @see the wiki
     */
    lateinit var backstory: BackstoryClient

    /**
     * The build client.
     * @see the wiki
     */
    lateinit var build: BuildClient

    /**
     * The character client.
     * @see the wiki
     */
    lateinit var character: CharacterClient

    /**
     * The color client.
     * @see the wiki
     */
    lateinit var color: ColorClient

    /**
     * The commerce client.
     * @see the wiki
     */
    lateinit var commerce: CommerceClient

    /**
     * The continent client.
     * @see the wiki
     */
    lateinit var continent: ContinentClient

    /**
     * The currency client.
     * @see the wiki
     */
    lateinit var currency: CurrencyClient

    /**
     * The daily crafting client.
     * @see the wiki
     */
    lateinit var dailyCrafting: DailyCraftingClient

    /**
     * The dungeon client.
     * @see the wiki
     */
    lateinit var dungeon: DungeonClient

    /**
     * The emblem client.
     * @see the wiki
     */
    lateinit var emblem: EmblemClient

    /**
     * The emote client.
     * @see the wiki
     */
    lateinit var emote: EmoteClient

    /**
     * The file client for commonly requests in-game assets.
     * @see the wiki
     */
    lateinit var file: FileClient

    /**
     * The finisher client.
     * @see the wiki
     */
    lateinit var finisher: FinisherClient

    /**
     * The glider client.
     * @see the wiki
     */
    lateinit var glider: GliderClient

    /**
     * The guild client.
     * @see the wiki
     */
    lateinit var guild: GuildClient

    /**
     * The home instance client.
     */
    lateinit var home: HomeClient

    /**
     * The item client.
     * @see the wiki
     */
    lateinit var item: ItemClient

    /**
     * The legendary armory client.
     * @see the wiki
     */
    lateinit var legendaryArmory: LegendaryArmoryClient

    /**
     * The legend client. For Revenants.
     * @see the wiki
     */
    lateinit var legend: LegendClient

    /**
     * The mail carrier client.
     * @see the wiki
     */
    lateinit var mailCarrier: MailCarrierClient

    /**
     * The map chests client.
     * @see the wiki
     */
    lateinit var mapChest: MapChestClient

    /**
     * The map client.
     * @see the wiki
     */
    lateinit var map: MapClient

    /**
     * The mastery client.
     * @see the wiki
     */
    lateinit var mastery: MasteryClient

    /**
     * The material client.
     * @see the wiki
     */
    lateinit var material: MaterialClient

    /**
     * The mini-pet client.
     * @see the wiki
     */
    lateinit var mini: MiniClient

    /**
     * The mount client.
     * @see the wiki
     */
    lateinit var wvw: WvwClient

    init {
        setupClients(httpClient, json, configuration)
    }

    /**
     * Sets up the underlying clients.
     */
    private fun setupClients(httpClient: HttpClient, json: Json, configuration: Gw2ClientConfiguration) {
        val newClient = httpClient.setup(json, configuration)
        this.httpClient = newClient
        account = AccountClient(newClient, configuration)
        achievement = AchievementClient(newClient, configuration)
        backstory = BackstoryClient(newClient, configuration)
        build = BuildClient(newClient, configuration)
        character = CharacterClient(newClient, configuration)
        color = ColorClient(newClient, configuration)
        commerce = CommerceClient(newClient, configuration)
        continent = ContinentClient(newClient, configuration)
        currency = CurrencyClient(newClient, configuration)
        dailyCrafting = DailyCraftingClient(newClient, configuration)
        dungeon = DungeonClient(newClient, configuration)
        emblem = EmblemClient(newClient, configuration)
        emote = EmoteClient(newClient, configuration)
        file = FileClient(newClient, configuration)
        finisher = FinisherClient(newClient, configuration)
        glider = GliderClient(newClient, configuration)
        guild = GuildClient(newClient, configuration)
        home = HomeClient(newClient, configuration)
        item = ItemClient(newClient, configuration)
        legendaryArmory = LegendaryArmoryClient(newClient, configuration)
        legend = LegendClient(newClient, configuration)
        mailCarrier = MailCarrierClient(newClient, configuration)
        mapChest = MapChestClient(newClient, configuration)
        map = MapClient(newClient, configuration)
        mastery = MasteryClient(newClient, configuration)
        material = MaterialClient(newClient, configuration)
        mini = MiniClient(newClient, configuration)
        mount = MountClient(newClient, configuration)
        novelty = NoveltyClient(newClient, configuration)
        outfit = OutfitClient(newClient, configuration)
        pet = PetClient(newClient, configuration)
        profession = ProfessionClient(newClient, configuration)
        pvp = PvpClient(newClient, configuration)
        quaggan = QuagganClient(newClient, configuration)
        quest = QuestClient(newClient, configuration)
        race = RaceClient(newClient, configuration)
        raid = RaidClient(newClient, configuration)
        recipe = RecipeClient(newClient, configuration)
        skill = SkillClient(newClient, configuration)
        skin = SkinClient(newClient, configuration)
        specialization = SpecializationClient(newClient, configuration)
        story = StoryClient(newClient, configuration)
        token = TokenClient(newClient, configuration)
        title = TitleClient(newClient, configuration)
        trait = TraitClient(newClient, configuration)
        worldBoss = WorldBossClient(newClient, configuration)
        world = WorldClient(newClient, configuration)
        wvw = WvwClient(newClient, configuration)
    }

    /**
     * @return a new [Gw2Client] with an updated [Gw2ClientConfiguration]
     */
    fun config(block: Gw2ClientConfiguration.() -> Gw2ClientConfiguration): Unit = setupClients(httpClient, json, block(configuration))

    /**
     * @return a new [Gw2Client] with an updated [Json]
     */
    fun json(block: JsonBuilder.() -> Unit): Unit = setupClients(httpClient, Json(json, block), configuration)

    /**
     * @return a new [Gw2Client] with an updated [HttpClientConfig]
     */
    fun httpClient(block: HttpClientConfig<*>.() -> Unit): Unit = setupClients(httpClient.config(block), json, configuration)

    /**
     * @return a new http client with the configuration applied
     */
    private fun HttpClient.setup(json: Json, configuration: Gw2ClientConfiguration): HttpClient = config {
        // Enable kotlinx.serialization
        install(ContentNegotiation) {
            json(json)
        }

        defaultRequest {
            url(Endpoints.BASE_URL)

            // Set up the headers.
            configuration.schemaVersion?.let { version ->
                schemaVersion(version)
            }

            configuration.token?.let { token ->
                bearer(token)
            }

            configuration.language?.let { language ->
                language(language)
            }
        }
    }

    /**
     * Close the HTTP client.
     */
    override fun close() {
        httpClient.close()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy