From 64357ca1d3fcf16791af401a8f15a8ba01a8a007 Mon Sep 17 00:00:00 2001 From: Henrik Lepson Date: Sun, 7 Nov 2021 14:14:22 +0200 Subject: [PATCH] MOB-23 token emit related code --- MobileAuthApp/app/build.gradle | 3 ++ .../mobileauthapp/HomeFragment.kt | 2 +- .../mobileauthapp/network/Response.kt | 9 +++++ .../mobileauthapp/network/TokenApiService.kt | 37 +++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/Response.kt create mode 100644 MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt diff --git a/MobileAuthApp/app/build.gradle b/MobileAuthApp/app/build.gradle index 5cd7f6a..a65bc6e 100644 --- a/MobileAuthApp/app/build.gradle +++ b/MobileAuthApp/app/build.gradle @@ -62,4 +62,7 @@ dependencies { //SecureDataStoring implementation("androidx.security:security-crypto:1.0.0") + // Retrofit + Moshi Converter + implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' + implementation 'com.squareup.moshi:moshi-kotlin:1.9.3' } \ No newline at end of file diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt index 3466620..e53cd54 100644 --- a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/HomeFragment.kt @@ -67,7 +67,7 @@ class HomeFragment : Fragment() { (activity as MainActivity).menuAvailable = false // Currently saving is true because the application is not yet integrated with // other applications or websites. - // TODO: Check the navigation action default values. Not everything has to be declared implicitly. + // TODO: Check the navigation action default values. Not everything has to be declared explicitly. if (auth) { val action = HomeFragmentDirections.actionHomeFragmentToCanFragment(reading = false, auth = true, mobile = mobile) findNavController().navigate(action) diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/Response.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/Response.kt new file mode 100644 index 0000000..199b913 --- /dev/null +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/Response.kt @@ -0,0 +1,9 @@ +package com.tarkvaraprojekt.mobileauthapp.network + +/** + * Placeholder ResponseItem. + */ +data class ResponseItem ( + val data1: Int, + val data2: Int, +) \ No newline at end of file diff --git a/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt new file mode 100644 index 0000000..aa08490 --- /dev/null +++ b/MobileAuthApp/app/src/main/java/com/tarkvaraprojekt/mobileauthapp/network/TokenApiService.kt @@ -0,0 +1,37 @@ +package com.tarkvaraprojekt.mobileauthapp.network + +import com.squareup.moshi.Moshi +import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory +import retrofit2.Response +import retrofit2.Retrofit +import retrofit2.converter.moshi.MoshiConverterFactory +import retrofit2.http.Body +import retrofit2.http.GET +import retrofit2.http.Headers +import retrofit2.http.POST + +/** + * Class for making HTTP requests + * Based on https://developer.android.com/courses/pathways/android-basics-kotlin-unit-4-pathway-2 + */ +private const val BASE_URL = + "add-endpoint-url-here" + +private val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build() +private val retrofit = Retrofit.Builder().addConverterFactory(MoshiConverterFactory.create(moshi)) + .baseUrl(BASE_URL).build() + +interface TokenApiService { + @GET("something") + suspend fun getData(): ResponseItem + + @Headers("Content-Type: application/json") + @POST("posts") + suspend fun addData(@Body data: ResponseItem): Response +} + +object TokenApi { + val retrofitService : TokenApiService by lazy { + retrofit.create(TokenApiService::class.java) + } +} \ No newline at end of file