Add self governments impl

This commit is contained in:
Mikołaj Pich 2020-04-02 23:40:52 +02:00
parent bff34b1990
commit 58cf03a711
10 changed files with 123 additions and 7 deletions

View file

@ -0,0 +1,18 @@
package io.github.wulkanowy.sdk.scrapper.home
import com.google.gson.annotations.SerializedName
data class GovernmentMember(
@SerializedName("Name")
val name: String,
@SerializedName("Position")
val position: String,
@SerializedName("Division")
val division: String,
@SerializedName("Id")
val id: Int
)

View file

@ -0,0 +1,12 @@
package io.github.wulkanowy.sdk.scrapper.home
import com.google.gson.annotations.SerializedName
data class GovernmentUnit(
@SerializedName("UnitName")
val unitName: String,
@SerializedName("People")
val people: List<GovernmentMember>
)

View file

@ -1,6 +1,7 @@
package io.github.wulkanowy.sdk.scrapper.repository
import io.github.wulkanowy.sdk.scrapper.getScriptParam
import io.github.wulkanowy.sdk.scrapper.home.GovernmentUnit
import io.github.wulkanowy.sdk.scrapper.home.LuckyNumber
import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorHandlerTransformer
import io.github.wulkanowy.sdk.scrapper.service.HomepageService
@ -18,11 +19,9 @@ class HomepageRepository(private val api: HomepageService) {
}.map { it.apply { token = this } }
}
fun getSelfGovernments(): Single<List<String>> {
fun getSelfGovernments(): Single<List<GovernmentUnit>> {
return getToken().flatMap { api.getSelfGovernments(it) }
.compose(ErrorHandlerTransformer()).map { it.data.orEmpty() }.map { it[0].content }.map { res ->
res.map { it.name }
}
.compose(ErrorHandlerTransformer()).map { requireNotNull(it.data) }
}
fun getStudentsTrips(): Single<List<String>> {

View file

@ -1,6 +1,7 @@
package io.github.wulkanowy.sdk.scrapper.service
import io.github.wulkanowy.sdk.scrapper.ApiResponse
import io.github.wulkanowy.sdk.scrapper.home.GovernmentUnit
import io.github.wulkanowy.sdk.scrapper.home.HomepageTileResponse
import io.reactivex.Single
import retrofit2.http.Field
@ -15,7 +16,7 @@ interface HomepageService {
@FormUrlEncoded
@POST("Start.mvc/GetSelfGovernments")
fun getSelfGovernments(@Field("permissions") token: String): Single<ApiResponse<List<HomepageTileResponse>>>
fun getSelfGovernments(@Field("permissions") token: String): Single<ApiResponse<List<GovernmentUnit>>>
@FormUrlEncoded
@POST("Start.mvc/GetStudentTrips")

View file

@ -17,6 +17,22 @@ class HomepageTest : BaseLocalTest() {
@Test
fun getSelfGovernments() {
server.enqueue(MockResponse().setBody(HomepageTest::class.java.getResource("Index.html").readText()))
server.enqueue("GetSelfGovernments.json")
server.start(3000)
val units = repo.getSelfGovernments().blockingGet()
assertEquals(1, units.size)
assertEquals("ZST-I", units[0].unitName)
val members = units[0].people
assertEquals(3, members.size)
with(members[0]) {
assertEquals("Jan Michał Kowalski", name)
assertEquals("Przewodniczący", position)
assertEquals("3tm (T 17)", division)
assertEquals(0, id)
}
}
@Test

View file

@ -0,0 +1,30 @@
{
"data": [
{
"UnitName": "ZST-I",
"People": [
{
"Name": "Jan Michał Kowalski",
"Position": "Przewodniczący",
"Division": "3tm (T 17)",
"Id": 0
},
{
"Name": "Paweł Jan Czerwiński",
"Position": "Zastępca",
"Division": "3tmm (T 17)",
"Id": 0
},
{
"Name": "Magdalena Trzecia Wielka",
"Position": "Skarbnik",
"Division": "3tmm (T 17)",
"Id": 0
}
]
}
],
"success": true,
"errorMessage": null,
"feedback": null
}

View file

@ -491,9 +491,9 @@ class Sdk {
}.map { it.number }
}
fun getSelfGovernments(): Single<List<String>> {
fun getSelfGovernments(): Single<List<GovernmentUnit>> {
return when (mode) {
Mode.HYBRID, Mode.SCRAPPER -> scrapper.getSelfGovernments().compose(ScrapperExceptionTransformer())
Mode.HYBRID, Mode.SCRAPPER -> scrapper.getSelfGovernments().compose(ScrapperExceptionTransformer()).map { it.mapToUnits() }
Mode.API -> throw FeatureNotAvailableException("Self governments is not available in API mode")
}
}

View file

@ -0,0 +1,26 @@
package io.github.wulkanowy.sdk.mapper
import io.github.wulkanowy.sdk.pojo.GovernmentMember
import io.github.wulkanowy.sdk.pojo.GovernmentUnit
import io.github.wulkanowy.sdk.scrapper.home.GovernmentMember as ScrapperGovernmentMember
import io.github.wulkanowy.sdk.scrapper.home.GovernmentUnit as ScrapperGovernmentUnit
fun List<ScrapperGovernmentUnit>.mapToUnits(): List<GovernmentUnit> {
return map {
GovernmentUnit(
unitName = it.unitName,
people = it.people.mapToMembers()
)
}
}
fun List<ScrapperGovernmentMember>.mapToMembers(): List<GovernmentMember> {
return map {
GovernmentMember(
name = it.name,
division = it.division,
position = it.position,
id = it.id
)
}
}

View file

@ -0,0 +1,8 @@
package io.github.wulkanowy.sdk.pojo
data class GovernmentMember(
val name: String,
val position: String,
val division: String,
val id: Int
)

View file

@ -0,0 +1,6 @@
package io.github.wulkanowy.sdk.pojo
data class GovernmentUnit(
val unitName: String,
val people: List<GovernmentMember>
)