Skip to content

Commit c7e746b

Browse files
Add Retrofit Client
1 parent 4ce7a3b commit c7e746b

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ android {
1313
targetSdkVersion 29
1414
versionCode 1
1515
versionName "1.0"
16+
buildConfigField "String", "DEFAULT_BASE_URL", '"https://api.github.com/"'
1617
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1718
}
1819
buildTypes {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.codingblocks.mvvm.models
2+
3+
data class User(
4+
val login: String,
5+
val avatarUrl: String,
6+
val organizationsUrl: String,
7+
val name: String,
8+
val followers: Int,
9+
val following: Int
10+
)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.codingblocks.mvvm.restapi
2+
3+
import com.codingblocks.mvvm.BuildConfig
4+
import com.google.gson.FieldNamingPolicy
5+
import com.google.gson.GsonBuilder
6+
import okhttp3.ConnectionPool
7+
import okhttp3.OkHttpClient
8+
import okhttp3.logging.HttpLoggingInterceptor
9+
import retrofit2.Retrofit
10+
import retrofit2.converter.gson.GsonConverterFactory
11+
import java.util.concurrent.TimeUnit
12+
13+
/**
14+
* Singleton Class for the Networking Client
15+
**/
16+
17+
object Clients {
18+
19+
private const val connectTimeout = 15 // 15s
20+
private const val readTimeout = 15 // 15s
21+
22+
private val clientInterceptor = OkHttpClient.Builder()
23+
.connectTimeout(connectTimeout.toLong(), TimeUnit.SECONDS)
24+
.readTimeout(readTimeout.toLong(), TimeUnit.SECONDS)
25+
.connectionPool(ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
26+
/**
27+
* An OkHttp interceptor which logs request and response information.
28+
* Logs are generated into the studio logger
29+
**/
30+
.addInterceptor(HttpLoggingInterceptor().apply {
31+
level = HttpLoggingInterceptor.Level.BODY
32+
}
33+
)
34+
/**
35+
* use this interceptor to authorization token to every request
36+
**/
37+
// .addInterceptor { chain ->
38+
// chain.proceed(
39+
// chain.request().newBuilder().addHeader(
40+
// "Authorization",
41+
// "Token $token"
42+
// ).build()
43+
// )
44+
// }
45+
.build()
46+
47+
/**
48+
* Configures Gson to apply a specific naming policy to an object's field during serialization
49+
* and deserialization.
50+
* Reduces the work to use annotation like @SerializedName
51+
**/
52+
53+
var gson = GsonBuilder()
54+
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
55+
.create()
56+
57+
private val retrofit = Retrofit.Builder()
58+
.client(clientInterceptor)
59+
.baseUrl(BuildConfig.DEFAULT_BASE_URL)
60+
.addConverterFactory(GsonConverterFactory.create(gson))
61+
.build()
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codingblocks.mvvm.restapi
2+
3+
import retrofit2.Response
4+
import retrofit2.http.GET
5+
6+
7+
interface RESTAPI {
8+
9+
@get:GET("users/aggarwalpulkit596")
10+
val getMe: Response<List<String>>
11+
12+
13+
@get:GET("users/aggarwalpulkit596/repos")
14+
val getrepos: Response<List<String>>
15+
16+
17+
}

0 commit comments

Comments
 (0)