Skip to content

Commit

Permalink
Merge pull request #38 from Ixirsii/feat/logging
Browse files Browse the repository at this point in the history
Add SLF4J logging
  • Loading branch information
Lycoon authored Jan 15, 2024
2 parents 9ebb981 + 802ccfc commit e72216e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.lycoon</groupId>
<artifactId>clash-api</artifactId>
<version>5.1.2</version>
<version>5.1.3</version>

<!-- Project metadata -->
<name>${project.groupId}:${project.artifactId}</name>
Expand Down Expand Up @@ -70,6 +70,13 @@
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.9</version>
</dependency>

</dependencies>

<build>
Expand Down
41 changes: 39 additions & 2 deletions src/main/java/com/lycoon/clashapi/core/ClashAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.lycoon.clashapi.models.war.War
import com.lycoon.clashapi.models.war.WarlogEntry
import com.lycoon.clashapi.models.warleague.WarLeague
import com.lycoon.clashapi.models.warleague.WarLeagueGroup
import org.slf4j.Logger
import org.slf4j.LoggerFactory

/**
* Create an instance of this class to start using the API.<br></br>
Expand All @@ -31,60 +33,70 @@ class ClashAPI : ClashAPIClient, IClashAPI
constructor(tokens: List<String>) : super(tokens)
constructor(email: String, password: String) : super(email, password)

private val log: Logger = LoggerFactory.getLogger(ClashAPI::class.java)

// ##############################################
// || Clans API ||
// ##############################################

override fun getWarLeagueGroup(clanTag: String): WarLeagueGroup
{
log.trace("Getting war league group for clan {}", clanTag)
val tag = formatTag(clanTag)
val res = get("/clans/$tag/currentwar/leaguegroup")
return deserialize(res.body.string())
}

override fun getWarLeagueWar(warTag: String): War
{
log.trace("Getting war league war {}", warTag)
val tag = formatTag(warTag)
val res = get("/clanwarleagues/wars/$tag")
return deserialize(res.body.string())
}

override fun getWarlog(clanTag: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<WarlogEntry>
{
log.trace("Getting war log for clan {} with filter {}", clanTag, queryParamsBuilder)
val tag = formatTag(clanTag)
val res = get("/clans/$tag/warlog", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getClans(queryParamsBuilder: ClanQueryParamsBuilder?): List<Clan>
{
log.trace("Getting clans with filter {}", queryParamsBuilder)
val res = get("/clans", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getCurrentWar(clanTag: String): War
{
log.trace("Getting current war for clan {}", clanTag)
val tag = formatTag(clanTag)
val res = get("/clans/$tag/currentwar")
return deserialize(res.body.string())
}

override fun getClan(clanTag: String): Clan
{
log.trace("Getting clan {}", clanTag)
val tag = formatTag(clanTag)
val res = get("/clans/$tag")
return deserialize(res.body.string())
}

override fun getClanMembers(clanTag: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<ClanMember>
{
log.trace("Getting clan members for clan {} with filter {}", clanTag, queryParamsBuilder)
val tag = formatTag(clanTag)
val res = get("/clans/$tag/members", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getCapitalRaidSeasons(clanTag: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<CapitalRaidSeason>
{
log.trace("Getting capital raid seasons for clan {} with filter {}", clanTag, queryParamsBuilder)
val tag = formatTag(clanTag)
val res = get("/clans/$tag/capitalraidseasons", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
Expand All @@ -96,13 +108,15 @@ class ClashAPI : ClashAPIClient, IClashAPI

override fun getPlayer(playerTag: String): Player
{
log.trace("Getting player {}", playerTag)
val tag = formatTag(playerTag)
val res = get("/players/$tag")
return deserialize(res.body.string())
}

override fun isVerifiedPlayer(playerTag: String, token: String): Boolean
{
log.trace("Verifying player {} with token {}", playerTag, token)
val tag = formatTag(playerTag)
val res = post("/players/$tag/verifytoken", getRequestBody(TokenValidation(token)))
return deserialize<TokenValidationResponse>(res.body.string()).status == "ok"
Expand All @@ -114,62 +128,73 @@ class ClashAPI : ClashAPIClient, IClashAPI

override fun getCapitalLeagues(queryParamsBuilder: SimpleQueryParamsBuilder?): List<CapitalLeague>
{
log.trace("Getting capital leagues with filter {}", queryParamsBuilder)
val res = get("/capitalleagues", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getLeagues(queryParamsBuilder: SimpleQueryParamsBuilder?): List<League>
{
log.trace("Getting leagues with filter {}", queryParamsBuilder)
val res = get("/leagues", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getLeagueSeasonRankings(
leagueId: String, seasonId: String,
leagueId: String,
seasonId: String,
queryParamsBuilder: SimpleQueryParamsBuilder?): List<PlayerRanking>
{
log.trace("Getting league season rankings for league {} and season {} with filter {}", leagueId, seasonId, queryParamsBuilder)
val res = get("/leagues/$leagueId/seasons/$seasonId", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getCapitalLeague(leagueId: String): CapitalLeague
{
log.trace("Getting capital league {}", leagueId)
val res = get("/capitalleagues/$leagueId")
return deserialize(res.body.string())
}

override fun getBuilderBaseLeague(leagueId: String): BuilderBaseLeague
{
log.trace("Getting builder base league {}", leagueId)
val res = get("/builderbaseleagues/$leagueId")
return deserialize(res.body.string())
}

override fun getBuilderBaseLeagues(queryParamsBuilder: SimpleQueryParamsBuilder?): List<BuilderBaseLeague>
{
log.trace("Getting builder base leagues with filter {}", queryParamsBuilder)
val res = get("/builderbaseleagues", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getLeague(leagueId: String): League
{
log.trace("Getting league {}", leagueId)
val res = get("/leagues/$leagueId")
return deserialize(res.body.string())
}

override fun getLeagueSeasons(leagueId: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<LeagueSeason>
{
log.trace("Getting league seasons for league {} with filter {}", leagueId, queryParamsBuilder)
val res = get("/leagues/$leagueId/seasons", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getWarLeague(leagueId: String): WarLeague
{
log.trace("Getting war league {}", leagueId)
val res = get("/warleagues/$leagueId")
return deserialize(res.body.string())
}

override fun getWarLeagues(queryParamsBuilder: SimpleQueryParamsBuilder?): List<WarLeague>
{
log.trace("Getting war leagues with filter {}", queryParamsBuilder)
val res = get("/warleagues", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}
Expand All @@ -180,56 +205,65 @@ class ClashAPI : ClashAPIClient, IClashAPI

override fun getClanRankings(locationId: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<ClanRanking>
{
log.trace("Getting clan rankings for location {} with filter {}", locationId, queryParamsBuilder)
val res = get("/locations/${locationId}/rankings/clans", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getPlayerRankings(locationId: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<PlayerRanking>
{
log.trace("Getting player rankings for location {} with filter {}", locationId, queryParamsBuilder)
val res = get("/locations/${locationId}/rankings/players", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getClanBuilderBaseRankings(locationId: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<ClanBuilderBaseRanking>
{
log.trace("Getting clan builder base rankings for location {} with filter {}", locationId, queryParamsBuilder)
val res = get("/locations/${locationId}/rankings/clans-builder-base", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

@Deprecated("Use getClanBuilderBaseRankings instead")
override fun getClanVersusRankings(locationId: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<ClanBuilderBaseRanking>
{
log.trace("Getting clan versus rankings for location {} with filter {}", locationId, queryParamsBuilder)
val res = get("/locations/${locationId}/rankings/clans-versus", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getPlayerBuilderBaseRankings(locationId: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<PlayerBuilderBaseRanking>
{
log.trace("Getting player builder base rankings for location {} with filter {}", locationId, queryParamsBuilder)
val res = get("/locations/${locationId}/rankings/players-builder-base", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

@Deprecated("Use getPlayerBuilderBaseRankings instead")
override fun getPlayerVersusRankings(locationId: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<PlayerBuilderBaseRanking>
{
log.trace("Getting player versus rankings for location {} with filter {}", locationId, queryParamsBuilder)
val res = get("/locations/${locationId}/rankings/players-versus", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getLocations(queryParamsBuilder: SimpleQueryParamsBuilder?): List<Location>
{
log.trace("Getting locations with filter {}", queryParamsBuilder)
val res = get("/locations", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getCapitalRankings(locationId: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<CapitalRanking>
{
log.trace("Getting capital rankings for location {} with filter {}", locationId, queryParamsBuilder)
val res = get("/locations/$locationId/rankings/capitals", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getLocation(locationId: String): Location
{
log.trace("Getting location {}", locationId)
val res = get("/locations/$locationId")
return deserialize(res.body.string())
}
Expand All @@ -240,6 +274,7 @@ class ClashAPI : ClashAPIClient, IClashAPI

override fun getGoldPass(): GoldPassSeason
{
log.trace("Getting gold pass")
val res = get("/goldpass/seasons/current")
return deserialize(res.body.string())
}
Expand All @@ -250,13 +285,15 @@ class ClashAPI : ClashAPIClient, IClashAPI

override fun getPlayerLabels(queryParamsBuilder: SimpleQueryParamsBuilder?): List<Label>
{
log.trace("Getting player labels with filter {}", queryParamsBuilder)
val res = get("/labels/players", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}

override fun getClanLabels(queryParamsBuilder: SimpleQueryParamsBuilder?): List<Label>
{
log.trace("Getting clan labels with filter {}", queryParamsBuilder)
val res = get("/labels/clans", queryParamsBuilder)
return unwrapList(deserialize(res.body.string()))
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/com/lycoon/clashapi/core/ClashAPIClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.lycoon.clashapi.core.auth.KeyManager
import com.lycoon.clashapi.core.auth.TokenList
import com.lycoon.clashapi.core.exceptions.ClashAPIException
import okhttp3.*
import org.slf4j.LoggerFactory
import java.io.IOException
import java.util.logging.Level
import java.util.logging.Logger
Expand All @@ -29,6 +30,7 @@ abstract class ClashAPIClient
}

private val client: OkHttpClient = OkHttpClient.Builder().cookieJar(APICookieJar()).build()
private val log: org.slf4j.Logger = LoggerFactory.getLogger(ClashAPIClient::class.java)
private val tokens: TokenList

private fun getBaseAPIRequest(suffix: String, queryParamsBuilder: QueryParamsBuilder? = null): Request.Builder
Expand All @@ -46,6 +48,9 @@ abstract class ClashAPIClient
{
val req = getBaseAPIRequest(url, queryParamsBuilder).build()
val res = client.newCall(req).execute()

log.debug("Got response: {}", res)

return CoreUtils.checkResponse(res)
}

Expand All @@ -54,6 +59,9 @@ abstract class ClashAPIClient
{
val req = getBaseAPIRequest(url).post(body).build()
val res = client.newCall(req).execute()

log.debug("Got response: {}", res)

return CoreUtils.checkResponse(res)
}
}
}

0 comments on commit e72216e

Please sign in to comment.