mirror of
https://github.com/Matte23/circolapp.git
synced 2025-12-06 07:29:10 +00:00
Change ServerApi constructor
This commit is contained in:
@@ -11,7 +11,7 @@ object AndroidServerApi {
|
||||
|
||||
fun getInstance(server: ServerAPI.Companion.Servers): ServerAPI {
|
||||
return instance ?: synchronized(this) {
|
||||
instance ?: ServerAPI(ServerAPI.createServer(server)).also { instance = it }
|
||||
instance ?: ServerAPI(server).also { instance = it }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ object AndroidServerApi {
|
||||
val server = ServerAPI.getServer(serverID)
|
||||
|
||||
return instance ?: synchronized(this) {
|
||||
instance ?: ServerAPI(ServerAPI.createServer(server)).also { instance = it }
|
||||
instance ?: ServerAPI(server).also { instance = it }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,6 +36,6 @@ object AndroidServerApi {
|
||||
if (notifyNewCirculars && !enablePolling)
|
||||
FirebaseTopicUtils.selectTopic(newServer.toString(), context)
|
||||
|
||||
instance?.changeServer(ServerAPI.createServer(newServer))
|
||||
instance?.changeServer(newServer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,12 @@
|
||||
|
||||
package net.underdesk.circolapp.shared.server
|
||||
|
||||
import io.ktor.client.*
|
||||
import net.underdesk.circolapp.shared.data.Circular
|
||||
|
||||
abstract class Server {
|
||||
abstract class Server(
|
||||
val ktorClient: HttpClient
|
||||
) {
|
||||
abstract val serverID: Int
|
||||
abstract suspend fun getCircularsFromServer(): Pair<List<Circular>, ServerAPI.Companion.Result>
|
||||
abstract suspend fun newCircularsAvailable(): Pair<Boolean, ServerAPI.Companion.Result>
|
||||
|
||||
@@ -18,17 +18,23 @@
|
||||
|
||||
package net.underdesk.circolapp.shared.server
|
||||
|
||||
import io.ktor.client.*
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.underdesk.circolapp.shared.PlatformDispatcher
|
||||
import net.underdesk.circolapp.shared.data.Circular
|
||||
import net.underdesk.circolapp.shared.server.curie.CurieServer
|
||||
import net.underdesk.circolapp.shared.server.porporato.PorporatoServer
|
||||
|
||||
class ServerAPI(
|
||||
class ServerAPI(serverName: Servers) {
|
||||
private val ktorClient = KtorFactory().createClient()
|
||||
private var server: Server
|
||||
) {
|
||||
|
||||
fun serverID(): Int = server.serverID
|
||||
|
||||
init {
|
||||
server = createServer(serverName, ktorClient)
|
||||
}
|
||||
|
||||
suspend fun getCircularsFromServer(): Pair<List<Circular>, Result> = withContext(PlatformDispatcher.IO) {
|
||||
val newCircularsAvailable = server.newCircularsAvailable()
|
||||
|
||||
@@ -41,8 +47,8 @@ class ServerAPI(
|
||||
server.getCircularsFromServer()
|
||||
}
|
||||
|
||||
fun changeServer(server: Server) {
|
||||
this.server = server
|
||||
fun changeServer(serverName: Servers) {
|
||||
server = createServer(serverName, ktorClient)
|
||||
}
|
||||
|
||||
companion object {
|
||||
@@ -67,9 +73,9 @@ class ServerAPI(
|
||||
Servers.PORPORATO -> "Liceo G.F. Porporato"
|
||||
}
|
||||
|
||||
fun createServer(server: Servers) = when (server) {
|
||||
Servers.CURIE -> CurieServer()
|
||||
Servers.PORPORATO -> PorporatoServer()
|
||||
fun createServer(server: Servers, ktorClient: HttpClient) = when (server) {
|
||||
Servers.CURIE -> CurieServer(ktorClient)
|
||||
Servers.PORPORATO -> PorporatoServer(ktorClient)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
package net.underdesk.circolapp.shared.server.curie
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.utils.io.errors.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.underdesk.circolapp.shared.data.Circular
|
||||
import net.underdesk.circolapp.shared.server.KtorFactory
|
||||
import net.underdesk.circolapp.shared.server.Server
|
||||
import net.underdesk.circolapp.shared.server.ServerAPI
|
||||
import net.underdesk.circolapp.shared.server.pojo.Response
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
|
||||
class CurieServer : Server() {
|
||||
private val client = KtorFactory().createClient()
|
||||
|
||||
class CurieServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
override val serverID = ServerAPI.getServerId(ServerAPI.Companion.Servers.CURIE)
|
||||
|
||||
override suspend fun getCircularsFromServer(): Pair<List<Circular>, ServerAPI.Companion.Result> {
|
||||
@@ -35,7 +33,7 @@ class CurieServer : Server() {
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
@Throws(IOException::class, CancellationException::class)
|
||||
private suspend fun retrieveDataFromServer(): Response {
|
||||
return client.get(ENDPOINT_URL)
|
||||
return ktorClient.get(ENDPOINT_URL)
|
||||
}
|
||||
|
||||
fun generateFromString(string: String, url: String): Circular {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package net.underdesk.circolapp.shared.server.porporato
|
||||
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.*
|
||||
import io.ktor.utils.io.charsets.*
|
||||
@@ -7,14 +8,11 @@ import io.ktor.utils.io.errors.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import net.underdesk.circolapp.shared.data.Circular
|
||||
import net.underdesk.circolapp.shared.server.KtorFactory
|
||||
import net.underdesk.circolapp.shared.server.Server
|
||||
import net.underdesk.circolapp.shared.server.ServerAPI
|
||||
import kotlin.coroutines.cancellation.CancellationException
|
||||
|
||||
class PorporatoServer : Server() {
|
||||
private val client = KtorFactory().createClient()
|
||||
|
||||
class PorporatoServer(ktorClient: HttpClient) : Server(ktorClient) {
|
||||
private val baseUrl = "https://www.liceoporporato.edu.it/ARCHIVIO/PR/VP/"
|
||||
private val endpointUrls = listOf(
|
||||
"https://www.liceoporporato.edu.it/ARCHIVIO/PR/VP/circolari.php?dirname=CIRCOLARIP/- CIRCOLARI 2020-21/-01-Settembre/",
|
||||
@@ -99,7 +97,7 @@ class PorporatoServer : Server() {
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
@Throws(IOException::class, CancellationException::class)
|
||||
private suspend fun retrieveDataFromServer(url: String): String {
|
||||
return client.request<HttpResponse>(url).readText(Charsets.ISO_8859_1)
|
||||
return ktorClient.request<HttpResponse>(url).readText(Charsets.ISO_8859_1)
|
||||
}
|
||||
|
||||
fun generateFromString(string: String, path: String, index: Long): Circular {
|
||||
|
||||
Reference in New Issue
Block a user