Skip to content

Commit

Permalink
feat: working auth with credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Lycoon committed Nov 24, 2023
1 parent f1d0233 commit e5d79eb
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 70 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
<version>5.0.0-alpha.11</version>
</dependency>

<dependency>
Expand Down
64 changes: 32 additions & 32 deletions src/main/java/com/lycoon/clashapi/core/ClashAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,55 +39,55 @@ class ClashAPI : ClashAPIClient, IClashAPI
{
val tag = formatTag(clanTag)
val res = get("/clans/$tag/currentwar/leaguegroup")
return deserialize(res)
return deserialize(res.body.string())
}

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

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

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

override fun getCurrentWar(clanTag: String): War
{
val tag = formatTag(clanTag)
val res = get("/clans/$tag/currentwar")
return deserialize(res)
return deserialize(res.body.string())
}

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

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

override fun getCapitalRaidSeasons(clanTag: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<CapitalRaidSeason>
{
val tag = formatTag(clanTag)
val res = get("/clans/$tag/capitalraidseasons", queryParamsBuilder)
return unwrapList(deserialize(res))
return unwrapList(deserialize(res.body.string()))
}

// ##############################################
Expand All @@ -98,14 +98,14 @@ class ClashAPI : ClashAPIClient, IClashAPI
{
val tag = formatTag(playerTag)
val res = get("/players/$tag")
return deserialize(res)
return deserialize(res.body.string())
}

override fun isVerifiedPlayer(playerTag: String, token: String): Boolean
{
val tag = formatTag(playerTag)
val res = post("/players/$tag/verifytoken", getRequestBody(TokenValidation(token)))
return deserialize<TokenValidationResponse>(res).status == "ok"
return deserialize<TokenValidationResponse>(res.body.string()).status == "ok"
}

// ##############################################
Expand All @@ -115,63 +115,63 @@ class ClashAPI : ClashAPIClient, IClashAPI
override fun getCapitalLeagues(queryParamsBuilder: SimpleQueryParamsBuilder?): List<CapitalLeague>
{
val res = get("/capitalleagues", queryParamsBuilder)
return unwrapList(deserialize(res))
return unwrapList(deserialize(res.body.string()))
}

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

override fun getLeagueSeasonRankings(
leagueId: String, seasonId: String,
queryParamsBuilder: SimpleQueryParamsBuilder?): List<PlayerRanking>
{
val res = get("/leagues/$leagueId/seasons/$seasonId", queryParamsBuilder)
return unwrapList(deserialize(res))
return unwrapList(deserialize(res.body.string()))
}

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

override fun getBuilderBaseLeague(leagueId: String): BuilderBaseLeague
{
val res = get("/builderbaseleagues/$leagueId")
return deserialize(res)
return deserialize(res.body.string())
}

override fun getBuilderBaseLeagues(queryParamsBuilder: SimpleQueryParamsBuilder?): List<BuilderBaseLeague>
{
val res = get("/builderbaseleagues", queryParamsBuilder)
return unwrapList(deserialize(res))
return unwrapList(deserialize(res.body.string()))
}

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

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

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

override fun getWarLeagues(queryParamsBuilder: SimpleQueryParamsBuilder?): List<WarLeague>
{
val res = get("/warleagues", queryParamsBuilder)
return unwrapList(deserialize(res))
return unwrapList(deserialize(res.body.string()))
}

// ##############################################
Expand All @@ -181,57 +181,57 @@ class ClashAPI : ClashAPIClient, IClashAPI
override fun getClanRankings(locationId: String, queryParamsBuilder: SimpleQueryParamsBuilder?): List<ClanRanking>
{
val res = get("/locations/${locationId}/rankings/clans", queryParamsBuilder)
return unwrapList(deserialize(res))
return unwrapList(deserialize(res.body.string()))
}

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

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

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

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

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

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

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

override fun getLocation(locationId: String): Location
{
val res = get("/locations/$locationId")
return deserialize(res)
return deserialize(res.body.string())
}

// ##############################################
Expand All @@ -241,7 +241,7 @@ class ClashAPI : ClashAPIClient, IClashAPI
override fun getGoldPass(): GoldPassSeason
{
val res = get("/goldpass/seasons/current")
return deserialize(res)
return deserialize(res.body.string())
}

// ##############################################
Expand All @@ -251,12 +251,12 @@ class ClashAPI : ClashAPIClient, IClashAPI
override fun getPlayerLabels(queryParamsBuilder: SimpleQueryParamsBuilder?): List<Label>
{
val res = get("/labels/players", queryParamsBuilder)
return unwrapList(deserialize(res))
return unwrapList(deserialize(res.body.string()))
}

override fun getClanLabels(queryParamsBuilder: SimpleQueryParamsBuilder?): List<Label>
{
val res = get("/labels/clans", queryParamsBuilder)
return unwrapList(deserialize(res))
return unwrapList(deserialize(res.body.string()))
}
}
9 changes: 7 additions & 2 deletions src/main/java/com/lycoon/clashapi/core/ClashAPIClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import com.lycoon.clashapi.core.auth.TokenList
import com.lycoon.clashapi.core.exceptions.ClashAPIException
import okhttp3.*
import java.io.IOException
import java.util.logging.Level
import java.util.logging.Logger

abstract class ClashAPIClient
{
Expand All @@ -19,8 +21,11 @@ abstract class ClashAPIClient
val authManager = AuthManager(client, email, password)
val keyManager = KeyManager()

val token = keyManager.createKey(client);
this.tokens = TokenList(token)
val validTokens = keyManager.getValidTokens(client)
if (validTokens.isEmpty())
this.tokens = TokenList(keyManager.createKey(client).token)
else
this.tokens = TokenList(validTokens)
}

private val client: OkHttpClient = OkHttpClient.Builder().cookieJar(APICookieJar()).build()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/lycoon/clashapi/core/CoreUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ object CoreUtils {
}

@Throws(IOException::class)
inline fun <reified T> deserialize(res: Response): T {
return json.decodeFromString(res.body?.string() ?: "")
inline fun <reified T> deserialize(obj: String): T {
return json.decodeFromString(obj)
}

@Throws(IOException::class)
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/lycoon/clashapi/core/auth/AuthManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ class AuthManager(client: OkHttpClient, email: String, password: String) {
val req = Request.Builder().url("$DEVELOPER_API_URL/login").post(body).build()

val res = client.newCall(req).execute()
if (res.code == 403) throw AuthException()
if (res.code == 403)
{
res.body.close()
throw AuthException()
}

res.body.close()
}

init {
Expand Down
Loading

0 comments on commit e5d79eb

Please sign in to comment.