From 69d1880295a185a8f080d990ef868caed0d22595 Mon Sep 17 00:00:00 2001 From: biloute02 Date: Thu, 11 Jan 2024 22:03:30 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20du=20service=20Palto=20pour=20communiqu?= =?UTF-8?q?er=20avec=20l=E2=80=99API.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 2 + .../java/com/example/palto/data/Result.kt | 18 ------ .../palto/data/network/PaltoApiService.kt | 46 ++++++++++++++ .../palto/data/network/ServerDataSource.kt | 60 ------------------- .../data/network/model/UserCredentials.kt | 6 ++ .../palto/data/repository/TokenRepository.kt | 23 +++++++ .../palto/data/repository/TokensRepository.kt | 43 ------------- .../example/palto/{model => domain}/Tokens.kt | 4 +- 8 files changed, 79 insertions(+), 123 deletions(-) delete mode 100644 app/src/main/java/com/example/palto/data/Result.kt create mode 100644 app/src/main/java/com/example/palto/data/network/PaltoApiService.kt delete mode 100644 app/src/main/java/com/example/palto/data/network/ServerDataSource.kt create mode 100644 app/src/main/java/com/example/palto/data/network/model/UserCredentials.kt create mode 100644 app/src/main/java/com/example/palto/data/repository/TokenRepository.kt delete mode 100644 app/src/main/java/com/example/palto/data/repository/TokensRepository.kt rename app/src/main/java/com/example/palto/{model => domain}/Tokens.kt (79%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fc2cb02..cf82b15 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + - */ -sealed class Result { - - data class Success(val data: T) : Result() - data class Error(val exception: Exception) : Result() - - override fun toString(): String { - return when (this) { - is Success<*> -> "Success[data=$data]" - is Error -> "Error[exception=$exception]" - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/palto/data/network/PaltoApiService.kt b/app/src/main/java/com/example/palto/data/network/PaltoApiService.kt new file mode 100644 index 0000000..21cde23 --- /dev/null +++ b/app/src/main/java/com/example/palto/data/network/PaltoApiService.kt @@ -0,0 +1,46 @@ +package com.example.palto.data.network + +import com.example.palto.data.network.model.UserCredentials +import com.example.palto.domain.Tokens +import com.squareup.moshi.Moshi +import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import retrofit2.Retrofit +import retrofit2.converter.moshi.MoshiConverterFactory +import retrofit2.http.Body +import retrofit2.http.POST + +/** + * A public Api object that exposes the lazy-initialized Retrofit service + */ +object PaltoApi { + + // Build the Moshi object that Retrofit will be using, making sure to add the Kotlin adapter for + // full Kotlin compatibility. + private val moshi = Moshi.Builder() + .add(KotlinJsonAdapterFactory()) + .build() + + fun createService(url: String) { + // Use the Retrofit builder to build a retrofit object using a Moshi converter + // with our Moshi object. + val retrofit = Retrofit.Builder() + .addConverterFactory(MoshiConverterFactory.create(moshi)) + .baseUrl(url) + .build() + retrofitService = retrofit.create(PaltoApiService::class.java) + } + + // Retrofit service that Palto will use to do requests. + // Make sure to call createService once before using it. + lateinit var retrofitService: PaltoApiService +} + + +/** + * Functions to query the API. + */ +interface PaltoApiService { + + @POST("api/auth/jwt/token/") + suspend fun getTokens(@Body userCredentials: UserCredentials): Tokens +} diff --git a/app/src/main/java/com/example/palto/data/network/ServerDataSource.kt b/app/src/main/java/com/example/palto/data/network/ServerDataSource.kt deleted file mode 100644 index 8c68892..0000000 --- a/app/src/main/java/com/example/palto/data/network/ServerDataSource.kt +++ /dev/null @@ -1,60 +0,0 @@ -package com.example.palto.data.network - -import com.example.palto.data.Result -import com.example.palto.model.LoggedInUser -import com.example.palto.model.Tokens -import java.io.IOException -import java.util.UUID - -/** - * Class that handles API calls. - */ -class ServerDataSource { - - private var hostname: String? = null - - fun requestToken( - hostname: String, - username: String, - password: String - ): Result { - try { - val tokens = Tokens( - refresh = "aa", - access = "bb" - ) - return Result.Success(tokens) - } catch (e: Throwable) { - return Result.Error(IOException("Error logging in", e)) - } - } - - fun refreshToken(current_tokens: Tokens): Result { - return Result.Success(current_tokens) - } - - fun verifyToken(): Boolean { - return true - } - - fun login( - hostname: String, - username: String, - password: String - ): Result { - try { - val fakeUser = LoggedInUser( - UUID.randomUUID().toString(), - "dede", - "Lucie", - "Doe", - "aa@free.fr", - ) - return Result.Success(fakeUser) - } catch (e: Throwable) { - return Result.Error(IOException("Error logging in", e)) - } - } - - fun logout() { } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/palto/data/network/model/UserCredentials.kt b/app/src/main/java/com/example/palto/data/network/model/UserCredentials.kt new file mode 100644 index 0000000..50b3ffd --- /dev/null +++ b/app/src/main/java/com/example/palto/data/network/model/UserCredentials.kt @@ -0,0 +1,6 @@ +package com.example.palto.data.network.model + +data class UserCredentials( + val username: String, + val password: String +) \ No newline at end of file diff --git a/app/src/main/java/com/example/palto/data/repository/TokenRepository.kt b/app/src/main/java/com/example/palto/data/repository/TokenRepository.kt new file mode 100644 index 0000000..c1bb243 --- /dev/null +++ b/app/src/main/java/com/example/palto/data/repository/TokenRepository.kt @@ -0,0 +1,23 @@ +package com.example.palto.data.repository + +import com.example.palto.data.network.PaltoApi +import com.example.palto.data.network.model.UserCredentials +import com.example.palto.domain.Tokens + +/** + * Class that requests authentication tokens from Palto server. + */ +class TokenRepository() { + + private var tokens: Tokens? = null + + suspend fun authenticate( + hostname: String, + username: String, + password: String + ) { + PaltoApi.createService("http://$hostname:8000/") + val tokens = PaltoApi.retrofitService.getTokens((UserCredentials(username, password))) + this.tokens = tokens + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/palto/data/repository/TokensRepository.kt b/app/src/main/java/com/example/palto/data/repository/TokensRepository.kt deleted file mode 100644 index 184f04c..0000000 --- a/app/src/main/java/com/example/palto/data/repository/TokensRepository.kt +++ /dev/null @@ -1,43 +0,0 @@ -package com.example.palto.data.repository - -import com.example.palto.data.network.ServerDataSource -import com.example.palto.model.Tokens - -/** - * Class that requests authentication and user information from the remote data source and - * maintains an in-memory cache of login status and user credentials information. - */ - -class TokensRepository(val dataSource: ServerDataSource) { - - var tokens: Tokens? = null - private set - - /* - val isLoggedIn: Boolean - get() = user != null - */ - - init { - // If user credentials will be cached in local storage, it is recommended it be encrypted - // @see https://developer.android.com/training/articles/keystore - tokens = null - } - - /* - fun login(username: String, password: String): Result { - // handle login - val result = dataSource.login(username, password) - - if (result is Result.Success) { - setLoggedInUser(result.data) - } - - return result - } - */ - - private fun setTokens(tokens: Tokens) { - this.tokens = tokens - } -} \ No newline at end of file diff --git a/app/src/main/java/com/example/palto/model/Tokens.kt b/app/src/main/java/com/example/palto/domain/Tokens.kt similarity index 79% rename from app/src/main/java/com/example/palto/model/Tokens.kt rename to app/src/main/java/com/example/palto/domain/Tokens.kt index fa90475..4e29846 100644 --- a/app/src/main/java/com/example/palto/model/Tokens.kt +++ b/app/src/main/java/com/example/palto/domain/Tokens.kt @@ -1,4 +1,4 @@ -package com.example.palto.model +package com.example.palto.domain import java.io.Serializable /** @@ -7,4 +7,4 @@ import java.io.Serializable data class Tokens( val refresh: String, val access: String -) : Serializable \ No newline at end of file +) \ No newline at end of file