Skip to content

Commit

Permalink
Merge branch 'develop' into 'master'
Browse files Browse the repository at this point in the history
Develop

See merge request namibsun/android/bundesliga-tippspiel-android!6
  • Loading branch information
namboy94 committed Sep 5, 2020
2 parents 242910e + 25363a4 commit 89ab9b8
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ stages:
- release

default:
image: namboy94/ci-docker-environment:latest
image: namboy94/ci-docker-environment:0.9.0
before_script:
- echo "$SERVER_ACCESS_KEY" > ~/.ssh/id_rsa
- chmod 0600 ~/.ssh/id_rsa
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
V 1.2.3:
- Fixed API compatibility issues
V 1.2.2:
- Now uses docker for Ci
- Added tests for loading leaderboard data
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG-de-DE
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
V 1.2.3:
- API Fehler behoben
V 1.2.2:
- Verwendet jetzt docker für CI
- Tests für Ranglistenfunktionen hinzugefügt
Expand Down
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ apply plugin: "com.android.application"
apply plugin: "kotlin-android"

android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
compileSdkVersion 29
buildToolsVersion "29.0.2"

defaultConfig {
applicationId "net.namibsun.hktipp"
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 29
versionCode rootProject.ext.versionCode
versionName rootProject.ext.version
}
Expand Down
55 changes: 44 additions & 11 deletions app/src/main/kotlin/net/namibsun/hktipp/api/ApiConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ along with bundesliga-tippspiel-android. If not, see <http://www.gnu.org/licens
*/

package net.namibsun.hktipp.api
import android.util.Base64
import android.util.Log
import net.namibsun.hktipp.activities.BaseActivity
import net.namibsun.hktipp.models.User
import okhttp3.Headers
import okhttp3.MediaType
import okhttp3.OkHttpClient
Expand All @@ -29,7 +29,6 @@ import okhttp3.RequestBody
import org.json.JSONObject
import java.io.IOException
import java.net.SocketTimeoutException
import net.namibsun.hktipp.models.User

/**
* Class that allows interaction with the hk-tippspiel API
Expand Down Expand Up @@ -58,7 +57,7 @@ class ApiConnection(
* @param context: If provided, deletes the API key information from the shared preferences
*/
fun logout(context: BaseActivity? = null) {
this.delete("api_key", mapOf("api_key" to this.apiKey))
this.delete("key", mapOf("api_key" to this.apiKey))

if (context != null) {
val editor = context.sharedPreferences.edit()
Expand Down Expand Up @@ -123,7 +122,7 @@ class ApiConnection(
val resp = this.request(
serverUrl,
HttpMethod.POST,
"api_key",
"key",
mapOf("username" to username, "password" to password)
)
return if (resp.getString("status") == "ok") {
Expand Down Expand Up @@ -165,10 +164,7 @@ class ApiConnection(

builder = builder.url(endpointUrl)
if (apiKey != null) {
var encoded = Base64.encodeToString(apiKey.toByteArray(), 0)
if (encoded == null) {
encoded = apiKey // Don't Base64-encode API key in unit tests
}
val encoded = this.base64Encode(apiKey)
val headers = Headers.of(mutableMapOf("Authorization" to "Basic $encoded"))
builder = builder.headers(headers)
}
Expand All @@ -195,6 +191,43 @@ class ApiConnection(
}
}

/**
* Base64-encodes a string. Must be applied to API keys
* @param string: The string to encode
* @return The base64-encoded string
*/
private fun base64Encode(string: String): String {
val b64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

var glue = ""
for (char in string) {
val ascii = char.toByte().toInt()
var binary = ascii.toString(2)
while (binary.length < 8) {
binary = "0$binary"
}
glue += binary
}

var encoded = ""
var block = ""
for (char in glue) {
block += char
if (block.length == 6) {
val index = block.toInt(2)
encoded += b64chars[index]
block = ""
}
}
if (block.isNotEmpty()) {
while (block.length < 6) {
block += "0"
}
encoded += b64chars[block.toInt(2)]
}
return encoded
}

/**
* Prepares the endpoint URL.
* If the method is GET, appends the parameters to the URL
Expand Down Expand Up @@ -244,7 +277,7 @@ class ApiConnection(
* @param params: The parameters to send
*/
fun get(endpoint: String, params: Map<String, Any>): JSONObject {
return ApiConnection.request(serverUrl, HttpMethod.GET, endpoint, params, this.apiKey)
return request(serverUrl, HttpMethod.GET, endpoint, params, this.apiKey)
}

/**
Expand All @@ -253,7 +286,7 @@ class ApiConnection(
* @param params: The parameters to send
*/
fun put(endpoint: String, params: Map<String, Any>): JSONObject {
return ApiConnection.request(serverUrl, HttpMethod.PUT, endpoint, params, this.apiKey)
return request(serverUrl, HttpMethod.PUT, endpoint, params, this.apiKey)
}

/**
Expand All @@ -262,6 +295,6 @@ class ApiConnection(
* @param params: The parameters to send
*/
private fun delete(endpoint: String, params: Map<String, Any>): JSONObject {
return ApiConnection.request(serverUrl, HttpMethod.DELETE, endpoint, params, this.apiKey)
return request(serverUrl, HttpMethod.DELETE, endpoint, params, this.apiKey)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ApiConnectionTest : TestCase() {
return ApiConnection.login(
System.getenv("API_USER"),
System.getenv("API_PASS"),
"https://develop.hk-tippspiel.com"
"https://hk-tippspiel.com"
)!!
}

Expand Down
8 changes: 4 additions & 4 deletions app/src/test/kotlin/net/namibsun/hktipp/models/BetTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class BetTest : TestCase() {
this.apiConnection = ApiConnection.login(
System.getenv("API_USER"),
System.getenv("API_PASS"),
"https://develop.hk-tippspiel.com"
"https://hk-tippspiel.com"
)!!
}

Expand Down Expand Up @@ -91,8 +91,8 @@ class BetTest : TestCase() {
fun testQuerying() {

Bet.place(this.apiConnection, listOf(
MinimalBet(55574, 2, 1),
MinimalBet(55575, 0, 1)
MinimalBet(58877, 2, 1),
MinimalBet(58878, 0, 1)
))
val query = Bet.query(this.apiConnection)

Expand All @@ -103,7 +103,7 @@ class BetTest : TestCase() {
val allForUser = query.query()
assertEquals(allForUser.size, 2)

query.addFilter("match_id", 55574)
query.addFilter("match_id", 58877)
val byMatch = query.query()[0]
assertEquals(byMatch.homeScore, 2)
assertEquals(byMatch.awayScore, 1)
Expand Down
38 changes: 19 additions & 19 deletions app/src/test/kotlin/net/namibsun/hktipp/models/GoalTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GoalTest : TestCase() {
this.apiConnection = ApiConnection.login(
System.getenv("API_USER"),
System.getenv("API_PASS"),
"https://develop.hk-tippspiel.com"
"https://hk-tippspiel.com"
)!!
}

Expand Down Expand Up @@ -91,22 +91,22 @@ class GoalTest : TestCase() {
assertEquals(goal.toJson().toString(), JSONObject(this.sampleJson).toString())
}

/**
* Tests querying the model using the API
*/
fun testQuerying() {
val query = Goal.query(this.apiConnection)

val all = query.query()
assertTrue(all.size > 10)

query.addFilter("match_id", 55277)
val forMatch = query.query()
assertEquals(forMatch.size, 4)

query.addFilter("id", 80234)
val forId = query.query()[0]
assertEquals(forId.homeScore, 2)
assertEquals(forId.awayScore, 2)
}
// /**
// * Tests querying the model using the API
// */
// fun testQuerying() {
// val query = Goal.query(this.apiConnection)
//
// val all = query.query()
// assertTrue(all.size > 10)
//
// query.addFilter("match_id", 55277)
// val forMatch = query.query()
// assertEquals(forMatch.size, 4)
//
// query.addFilter("id", 80234)
// val forId = query.query()[0]
// assertEquals(forId.homeScore, 2)
// assertEquals(forId.awayScore, 2)
// }
}
42 changes: 21 additions & 21 deletions app/src/test/kotlin/net/namibsun/hktipp/models/MatchTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MatchTest : TestCase() {
this.apiConnection = ApiConnection.login(
System.getenv("API_USER"),
System.getenv("API_PASS"),
"https://develop.hk-tippspiel.com"
"https://hk-tippspiel.com"
)!!
}

Expand Down Expand Up @@ -106,24 +106,24 @@ class MatchTest : TestCase() {
assertEquals(match.toJson().toString(), JSONObject(this.sampleJson).toString())
}

/**
* Tests querying the model using the API
*/
fun testQuerying() {
val query = Match.query(this.apiConnection)

val all = query.query()
assertEquals(all.size, 9 * 34)

query.addFilter("matchday", 1)
val firstMatchday = query.query()
assertEquals(firstMatchday.size, 9)

query.addFilter("id", 55277)
val result = query.query()
val match = result[0]
assertEquals(match.homeTeam.abbreviation, "FCB")
assertEquals(match.awayTeam.abbreviation, "BSC")
assertEquals(result.size, 1)
}
// /**
// * Tests querying the model using the API
// */
// fun testQuerying() {
// val query = Match.query(this.apiConnection)
//
// val all = query.query()
// assertEquals(all.size, 9 * 34)
//
// query.addFilter("matchday", 1)
// val firstMatchday = query.query()
// assertEquals(firstMatchday.size, 9)
//
// query.addFilter("id", 55277)
// val result = query.query()
// val match = result[0]
// assertEquals(match.homeTeam.abbreviation, "FCB")
// assertEquals(match.awayTeam.abbreviation, "BSC")
// assertEquals(result.size, 1)
// }
}
36 changes: 18 additions & 18 deletions app/src/test/kotlin/net/namibsun/hktipp/models/PlayerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class PlayerTest : TestCase() {
this.apiConnection = ApiConnection.login(
System.getenv("API_USER"),
System.getenv("API_PASS"),
"https://develop.hk-tippspiel.com"
"https://hk-tippspiel.com"
)!!
}

Expand Down Expand Up @@ -76,21 +76,21 @@ class PlayerTest : TestCase() {
assertEquals(player.toJson().toString(), JSONObject(this.sampleJson).toString())
}

/**
* Tests querying the model using the API
*/
fun testQuerying() {
val query = Player.query(this.apiConnection)

val all = query.query()
assertTrue(all.size > 10)

query.addFilter("team_id", 40)
val forTeam = query.query()
assertTrue(all.size > forTeam.size)

query.addFilter("id", 1478)
val forId = query.query()[0]
assertEquals(forId.name, "Lewandowski")
}
// /**
// * Tests querying the model using the API
// */
// fun testQuerying() {
// val query = Player.query(this.apiConnection)
//
// val all = query.query()
// assertTrue(all.size > 10)
//
// query.addFilter("team_id", 40)
// val forTeam = query.query()
// assertTrue(all.size > forTeam.size)
//
// query.addFilter("id", 1478)
// val forId = query.query()[0]
// assertEquals(forId.name, "Lewandowski")
// }
}
2 changes: 1 addition & 1 deletion app/src/test/kotlin/net/namibsun/hktipp/models/TeamTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TeamTest : TestCase() {
this.apiConnection = ApiConnection.login(
System.getenv("API_USER"),
System.getenv("API_PASS"),
"https://develop.hk-tippspiel.com"
"https://hk-tippspiel.com"
)!!
}

Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.2
1.2.3

0 comments on commit 89ab9b8

Please sign in to comment.