Skip to content
This repository was archived by the owner on Jan 7, 2025. It is now read-only.

Commit 775bb18

Browse files
authored
Implemented RetrofitClient for fictadvisor.com (base URL not added ye… (#1)
* Implemented RetrofitClient for fictadvisor.com (base URL not added yet) and started of implemented API service for auth-processes (added POST-requests only) * Made the funcs suspended
1 parent 886b162 commit 775bb18

16 files changed

+248
-13
lines changed

app/build.gradle

+45-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
plugins {
22
id 'com.android.application'
33
id 'org.jetbrains.kotlin.android'
4+
id 'kotlin-kapt'
5+
id 'kotlin-platform-android'
6+
id 'kotlin-android'
7+
id 'kotlin-parcelize'
48
}
59

610
android {
@@ -9,36 +13,67 @@ android {
913

1014
defaultConfig {
1115
applicationId "com.fictadvisor.android"
12-
minSdk 24
16+
minSdk 26
1317
targetSdk 33
1418
versionCode 1
1519
versionName "1.0"
1620

1721
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1822
}
1923

24+
buildFeatures {
25+
viewBinding = true
26+
}
27+
2028
buildTypes {
29+
debug {
30+
debuggable true
31+
}
2132
release {
33+
debuggable false
2234
minifyEnabled false
23-
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
35+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2436
}
2537
}
38+
39+
buildFeatures {
40+
buildConfig = true
41+
}
42+
2643
compileOptions {
27-
sourceCompatibility JavaVersion.VERSION_1_8
28-
targetCompatibility JavaVersion.VERSION_1_8
44+
sourceCompatibility JavaVersion.VERSION_17
45+
targetCompatibility JavaVersion.VERSION_17
2946
}
3047
kotlinOptions {
31-
jvmTarget = '1.8'
48+
jvmTarget = '17'
3249
}
3350
}
3451

3552
dependencies {
3653

3754
implementation 'androidx.core:core-ktx:1.8.0'
38-
implementation 'androidx.appcompat:appcompat:1.4.1'
39-
implementation 'com.google.android.material:material:1.5.0'
40-
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
55+
implementation 'androidx.appcompat:appcompat:1.6.1'
56+
implementation 'com.google.android.material:material:1.9.0'
57+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
4158
testImplementation 'junit:junit:4.13.2'
42-
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
43-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
59+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
60+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
61+
62+
// - - Coroutines
63+
def coroutines_version = "1.7.3"
64+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
65+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
66+
def lifecycle_version = "2.6.2"
67+
// - - ViewModel
68+
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
69+
// - - LiveData
70+
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
71+
72+
// - - Retrofit2
73+
def retrofit_version = "2.9.0"
74+
def logging_version = "4.9.1"
75+
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
76+
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
77+
implementation "com.squareup.okhttp3:okhttp:$logging_version"
78+
implementation "com.squareup.okhttp3:logging-interceptor:$logging_version"
4479
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.fictadvisor.android.data.dto
2+
3+
data class AuthLoginResponse(
4+
val accessToken: String,
5+
val refreshToken: String,
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.fictadvisor.android.data.dto
2+
3+
data class AuthRefreshResponse(
4+
val accessToken: String,
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.fictadvisor.android.data.dto
2+
3+
data class ForgotPasswordRequest(
4+
val email: String,
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.fictadvisor.android.data.dto
2+
3+
data class RegistrationRequest(
4+
val student: Student,
5+
val user: User,
6+
val telegram: Telegram
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.fictadvisor.android.data.dto
2+
3+
data class RegistrationTelegramRequest(
4+
val token: String,
5+
val telegramId: Long
6+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.fictadvisor.android.data.dto
2+
3+
data class Student(
4+
val groupId: String,
5+
val firstName: String,
6+
val middleName: String,
7+
val lastName: String,
8+
val isCaptain: Boolean,
9+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fictadvisor.android.data.dto
2+
3+
import com.google.gson.annotations.SerializedName
4+
5+
data class Telegram(
6+
@SerializedName("auth_date")
7+
val authDate: Long,
8+
@SerializedName("first_name")
9+
val firstName: String,
10+
@SerializedName("last_name")
11+
val lastName: String,
12+
@SerializedName("photo_url")
13+
val photoUrl: String,
14+
val hash: String,
15+
val id: Long,
16+
val username: String
17+
18+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.fictadvisor.android.data.dto
2+
3+
data class User(
4+
val username: String,
5+
val email: String,
6+
val password: String
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.fictadvisor.android.data.dto
2+
3+
data class VerifyEmailRequest(
4+
val email: String
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fictadvisor.android.data.remote
2+
3+
import com.fictadvisor.android.data.remote.api.AuthApi
4+
import okhttp3.Interceptor
5+
import okhttp3.OkHttpClient
6+
import okhttp3.logging.HttpLoggingInterceptor
7+
import okhttp3.logging.HttpLoggingInterceptor.Level
8+
import retrofit2.Retrofit
9+
import retrofit2.converter.gson.GsonConverterFactory
10+
import java.util.concurrent.TimeUnit
11+
12+
private const val timeoutRead = 30 // In seconds
13+
private const val contentType = "Content-Type"
14+
private const val contentTypeValue = "application/json"
15+
private const val timeoutConnect = 30 // In seconds
16+
17+
object RetrofitClient {
18+
19+
private const val baseUrl = ""
20+
21+
val retrofitClient: Retrofit.Builder by lazy {
22+
23+
val levelType: Level
24+
// if (BuildConfig.DEBUG)
25+
// levelType = Level.BODY else levelType = Level.NONE
26+
27+
levelType = Level.BODY
28+
29+
val headerInterceptor = Interceptor { chain ->
30+
val original = chain.request()
31+
32+
val request = original.newBuilder()
33+
.header(contentType, contentTypeValue)
34+
.method(original.method, original.body)
35+
.build()
36+
37+
chain.proceed(request)
38+
}
39+
40+
val loggingInterceptor = HttpLoggingInterceptor()
41+
loggingInterceptor.setLevel(levelType)
42+
43+
val okhttpClient = OkHttpClient.Builder()
44+
okhttpClient
45+
.addInterceptor(loggingInterceptor)
46+
.addInterceptor(headerInterceptor)
47+
.connectTimeout(timeoutConnect.toLong(), TimeUnit.SECONDS)
48+
.readTimeout(timeoutRead.toLong(), TimeUnit.SECONDS)
49+
50+
Retrofit.Builder()
51+
.baseUrl(baseUrl)
52+
.client(okhttpClient.build())
53+
.addConverterFactory(GsonConverterFactory.create())
54+
}
55+
56+
val authApi: AuthApi by lazy {
57+
retrofitClient
58+
.build()
59+
.create(AuthApi::class.java)
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fictadvisor.android.data.remote.api
2+
3+
import com.fictadvisor.android.data.dto.*
4+
import retrofit2.Response
5+
import retrofit2.http.Body
6+
import retrofit2.http.POST
7+
8+
interface AuthApi {
9+
10+
@POST("/auth/loginTelegram")
11+
suspend fun loginTelegram(@Body telegramDto: Telegram): Response<AuthLoginResponse>
12+
13+
@POST("/auth/login")
14+
suspend fun login(): Response<AuthLoginResponse>
15+
16+
@POST("/auth/registerTelegram")
17+
suspend fun registerTelegram(@Body registrationTelegramRequest: RegistrationTelegramRequest)
18+
19+
@POST("/auth/register")
20+
suspend fun register(@Body registrationRequest: RegistrationRequest)
21+
22+
@POST("/auth/refresh")
23+
suspend fun refresh(): Response<AuthRefreshResponse>
24+
25+
@POST("/auth/forgotPassword")
26+
suspend fun forgotPassword(@Body forgotPasswordRequest: ForgotPasswordRequest)
27+
28+
@POST("/auth/verifyEmail")
29+
suspend fun verifyEmail(@Body verifyEmailRequest: VerifyEmailRequest)
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.fictadvisor.android.repository
2+
3+
import com.fictadvisor.android.data.dto.*
4+
import com.fictadvisor.android.data.remote.RetrofitClient
5+
6+
class AuthRepository() {
7+
private val authService = RetrofitClient.authApi
8+
9+
suspend fun login(username: String, password: String) = authService.login()
10+
suspend fun register(studentInfo: Student, userInfo: User, telegramInfo: Telegram) = authService.register(
11+
RegistrationRequest(
12+
studentInfo,
13+
userInfo,
14+
telegramInfo,
15+
),
16+
)
17+
suspend fun loginWithTelegram(telegramInfo: Telegram) = authService.loginTelegram(telegramInfo)
18+
19+
suspend fun registerWithTelegram(token: String, telegramId: Long) = authService.registerTelegram(
20+
RegistrationTelegramRequest(
21+
token,
22+
telegramId,
23+
),
24+
)
25+
26+
suspend fun refresh() = authService.refresh()
27+
28+
suspend fun forgotPassword(email: String) = authService.forgotPassword(
29+
ForgotPasswordRequest(
30+
email,
31+
),
32+
)
33+
34+
suspend fun verifyEmail(email: String) = authService.verifyEmail(
35+
VerifyEmailRequest(
36+
email,
37+
),
38+
)
39+
}

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22
plugins {
3-
id 'com.android.application' version '8.0.2' apply false
4-
id 'com.android.library' version '8.0.2' apply false
3+
id 'com.android.application' version '8.1.0' apply false
4+
id 'com.android.library' version '8.1.0' apply false
55
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
66
}

gradle.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ kotlin.code.style=official
2020
# Enables namespacing of each library's R class so that its R class includes only the
2121
# resources declared in the library itself and none from the library's dependencies,
2222
# thereby reducing the size of the R class for that library
23-
android.nonTransitiveRClass=true
23+
android.nonTransitiveRClass=true
24+
android.defaults.buildfeatures.buildconfig=true

settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencyResolutionManagement {
1010
repositories {
1111
google()
1212
mavenCentral()
13+
maven { url "https://jitpack.io" }
1314
}
1415
}
1516
rootProject.name = "fictadvisor-android"

0 commit comments

Comments
 (0)