Skip to content

Commit 38fda60

Browse files
committed
refact with retrofit2
1 parent a2e6b0b commit 38fda60

File tree

13 files changed

+326
-257
lines changed

13 files changed

+326
-257
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.3.70'
4+
ext.kotlin_version = '1.3.72'
55
repositories {
66
google()
77
jcenter()

demoapp/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ android {
66
compileSdkVersion 29
77
buildToolsVersion "29.0.3"
88

9+
compileOptions {
10+
sourceCompatibility JavaVersion.VERSION_1_8
11+
targetCompatibility JavaVersion.VERSION_1_8
12+
}
13+
914
defaultConfig {
1015
applicationId "com.mushare.demoapp"
1116
minSdkVersion 16
@@ -34,7 +39,7 @@ dependencies {
3439
implementation 'androidx.annotation:annotation:1.1.0'
3540
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
3641
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
37-
testImplementation 'junit:junit:4.12'
42+
testImplementation 'junit:junit:4.13'
3843
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3944
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
4045
implementation project(path: ':pluto-kotlin-client-sdk')

demoapp/src/main/java/com/mushare/demoapp/data/LoginDataSource.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ class LoginDataSource {
1717
//pluto?.registerByEmail(username, password, "Test", {
1818
//pluto?.resendValidationEmail(username, {
1919
pluto?.loginWithEmail(username, password, {
20-
pluto?.myInfo({
20+
pluto.myInfo({
2121
val fakeUser = LoggedInUser(java.util.UUID.randomUUID().toString(), it.name)
2222
onComplete(Result.Success(fakeUser))
2323
}, {
24-
onComplete(Result.Error(IOException("Error logging in $it")))
25-
Log.e("login", "failed $it")
24+
onComplete(Result.Error(IOException("Error getting account info $it")))
25+
Log.e("getInfo", "failed $it")
2626
})
2727
}, {
2828
onComplete(Result.Error(IOException("Error logging in $it")))

demoapp/src/main/java/com/mushare/demoapp/ui/login/LoginActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class LoginActivity : AppCompatActivity() {
2727

2828
setContentView(R.layout.activity_login)
2929

30-
Pluto.initialize(this,"https://staging.easyjapanese-api-gateway.mushare.cn/pluto","org.mushare.easyjapanese")
30+
Pluto.initialize(this,"https://staging.easyjapanese-api-gateway.mushare.cn/pluto/","org.mushare.easyjapanese")
3131

3232
val username = findViewById<EditText>(R.id.username)
3333
val password = findViewById<EditText>(R.id.password)

pluto-kotlin-client-sdk/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ android {
1010
defaultConfig {
1111
minSdkVersion 16
1212
targetSdkVersion 29
13-
versionCode 6
14-
versionName "0.1.5"
13+
versionCode 7
14+
versionName "0.1.6"
1515

1616
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1717
consumerProguardFiles 'consumer-rules.pro'
@@ -36,7 +36,7 @@ publishing {
3636
gpr(MavenPublication) {
3737
groupId "org.mushare"
3838
artifactId "pluto-kotlin-client-sdk"
39-
version "0.1.5"
39+
version "0.1.6"
4040
artifact sourceJar
4141
artifact "$buildDir/outputs/aar/pluto-kotlin-client-sdk-release.aar"
4242

@@ -74,6 +74,8 @@ dependencies {
7474
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
7575
implementation 'androidx.appcompat:appcompat:1.1.0'
7676
implementation 'androidx.core:core-ktx:1.2.0'
77+
implementation 'com.squareup.retrofit2:retrofit:2.7.2'
78+
implementation 'com.squareup.retrofit2:converter-gson:2.7.2'
7779
implementation 'com.squareup.okhttp3:okhttp:4.4.0'
7880
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
7981
testImplementation 'junit:junit:4.13'
Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
package com.mushare.plutosdk
22

33
import com.mushare.plutosdk.Pluto.Companion.appId
4-
import okhttp3.Call
5-
import okhttp3.Callback
6-
import okhttp3.Headers
7-
import okhttp3.Response
8-
import org.json.JSONObject
9-
import java.io.IOException
4+
import retrofit2.Callback
105

116
fun Pluto.getToken(completion: (String?) -> Unit) {
127
val jwt = data.jwt
@@ -26,33 +21,32 @@ fun Pluto.getToken(completion: (String?) -> Unit) {
2621
private fun Pluto.refreshToken(completion: (String?) -> Unit) {
2722
val userId = data.userId
2823
val refreshToken = data.refreshToken
29-
if (userId == null || refreshToken == null) {
24+
val deviceId = data.deviceID
25+
if (userId == null || refreshToken == null || deviceId == null) {
3026
completion(null)
3127
return
3228
}
33-
val bodyJson = JSONObject()
34-
bodyJson.put("refresh_token", refreshToken)
35-
bodyJson.put("user_id", userId)
36-
bodyJson.put("device_id", data.deviceID)
37-
bodyJson.put("app_id", appId)
38-
requestPost("api/auth/refresh", bodyJson, commonHeaders, object : Callback {
39-
override fun onFailure(call: Call, e: IOException) {
40-
e.printStackTrace()
29+
plutoService.refreshAuth(
30+
RefreshAuthPostData(refreshToken, userId, deviceId, appId)
31+
).enqueue(object : Callback<PlutoResponseWithBody<RefreshAuthResponse>> {
32+
override fun onFailure(
33+
call: retrofit2.Call<PlutoResponseWithBody<RefreshAuthResponse>>,
34+
t: Throwable
35+
) {
36+
t.printStackTrace()
4137
completion(null)
4238
}
4339

44-
override fun onResponse(call: Call, response: Response) {
45-
val plutoResponse = PlutoResponse(response)
46-
if (plutoResponse.statusOK()) {
47-
try {
48-
val jwt = plutoResponse.getBody().getString("jwt")
49-
if (data.updateJwt(jwt)) {
50-
completion(jwt)
51-
} else {
52-
completion(null)
53-
}
54-
} catch (e: Exception) {
55-
e.printStackTrace()
40+
override fun onResponse(
41+
call: retrofit2.Call<PlutoResponseWithBody<RefreshAuthResponse>>,
42+
response: retrofit2.Response<PlutoResponseWithBody<RefreshAuthResponse>>
43+
) {
44+
val plutoResponse = response.body()
45+
if (plutoResponse != null && plutoResponse.statusOK()) {
46+
val jwt = plutoResponse.getBody().jwt
47+
if (data.updateJwt(jwt)) {
48+
completion(jwt)
49+
} else {
5650
completion(null)
5751
}
5852
} else {
@@ -62,12 +56,12 @@ private fun Pluto.refreshToken(completion: (String?) -> Unit) {
6256
})
6357
}
6458

65-
fun Pluto.getHeaders(completion: (Headers) -> Unit) {
59+
fun Pluto.getAuthorizationHeader(completion: (Map<String, String>?) -> Unit) {
6660
getToken {
6761
if (it == null) {
68-
completion(commonHeaders)
62+
completion(null)
6963
} else {
70-
completion(commonHeaders.newBuilder().add("Authorization", "jwt $it").build())
64+
completion(hashMapOf("Authorization" to "jwt $it"))
7165
}
7266
}
7367
}

0 commit comments

Comments
 (0)