Ajout du service Palto pour communiquer avec l’API.

This commit is contained in:
biloute02 2024-01-11 22:03:30 +01:00
parent a79b591e90
commit 69d1880295
8 changed files with 79 additions and 123 deletions

View file

@ -5,6 +5,7 @@
<uses-permission android:name="android.permission.NFC" /> <uses-permission android:name="android.permission.NFC" />
<uses-feature android:name="android.hardware.nfc" android:required="true" /> <uses-feature android:name="android.hardware.nfc" android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
@ -15,6 +16,7 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/Theme.Palto" android:theme="@style/Theme.Palto"
android:usesCleartextTraffic="true"
tools:targetApi="31"> tools:targetApi="31">
<activity <activity
android:name=".PaltoActivity" android:name=".PaltoActivity"

View file

@ -1,18 +0,0 @@
package com.example.palto.data
/**
* A generic class that holds a value with its loading status.
* @param <T>
*/
sealed class Result<out T : Any> {
data class Success<out T : Any>(val data: T) : Result<T>()
data class Error(val exception: Exception) : Result<Nothing>()
override fun toString(): String {
return when (this) {
is Success<*> -> "Success[data=$data]"
is Error -> "Error[exception=$exception]"
}
}
}

View file

@ -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
}

View file

@ -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<Tokens> {
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<Tokens> {
return Result.Success(current_tokens)
}
fun verifyToken(): Boolean {
return true
}
fun login(
hostname: String,
username: String,
password: String
): Result<LoggedInUser> {
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() { }
}

View file

@ -0,0 +1,6 @@
package com.example.palto.data.network.model
data class UserCredentials(
val username: String,
val password: String
)

View file

@ -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
}
}

View file

@ -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<LoggedInUser> {
// 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
}
}

View file

@ -1,4 +1,4 @@
package com.example.palto.model package com.example.palto.domain
import java.io.Serializable import java.io.Serializable
/** /**
@ -7,4 +7,4 @@ import java.io.Serializable
data class Tokens( data class Tokens(
val refresh: String, val refresh: String,
val access: String val access: String
) : Serializable )