Skip to content

Commit 73714aa

Browse files
authored
Merge pull request #15 from MuShare/develop/binding
Support account binding
2 parents 00a2817 + 22fa110 commit 73714aa

File tree

5 files changed

+156
-3
lines changed

5 files changed

+156
-3
lines changed

pluto-kotlin-client-sdk/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ apply plugin: 'kotlin-android-extensions'
44
apply plugin: 'maven-publish'
55

66
buildscript {
7-
ext.versionCode = 11
7+
ext.versionCode = 12
88
ext.versionName = '0.5'
99
}
1010

@@ -28,7 +28,6 @@ android {
2828
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
2929
}
3030
}
31-
3231
}
3332

3433
task sourceJar(type: Jar) {
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.mushare.plutosdk
2+
3+
import retrofit2.Call
4+
import retrofit2.Callback
5+
import retrofit2.Response
6+
7+
val availableLoginTypes: Array<Pluto.LoginType>
8+
get() = arrayOf(Pluto.LoginType.mail, Pluto.LoginType.mail)
9+
10+
val Pluto.availableBindings: Array<PlutoUserBinding>?
11+
get() = data.user?.bindings
12+
13+
fun Pluto.bind(
14+
type: Pluto.LoginType,
15+
authString: String,
16+
success: () -> Unit,
17+
error: ((PlutoError) -> Unit)? = null,
18+
handler: Pluto.PlutoRequestHandler? = null
19+
) {
20+
getAuthorizationHeader(
21+
completion = { header ->
22+
if (header == null) {
23+
handler?.setCall(null)
24+
error?.invoke(PlutoError.notSignIn)
25+
return@getAuthorizationHeader
26+
}
27+
28+
val postData = when (type) {
29+
Pluto.LoginType.mail -> {
30+
BindPostData(
31+
type = type.identifier,
32+
mail = authString
33+
)
34+
}
35+
Pluto.LoginType.google -> {
36+
BindPostData(
37+
type = type.identifier,
38+
idToken = authString
39+
)
40+
}
41+
}
42+
plutoService.bind(postData, header).apply {
43+
enqueue(object : Callback<PlutoResponse> {
44+
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
45+
t.printStackTrace()
46+
error?.invoke(PlutoError.badRequest)
47+
}
48+
49+
override fun onResponse(
50+
call: Call<PlutoResponse>,
51+
response: Response<PlutoResponse>
52+
) {
53+
val plutoResponse = response.body()
54+
if (plutoResponse != null) {
55+
if (plutoResponse.statusOK()) {
56+
success()
57+
} else {
58+
error?.invoke(plutoResponse.errorCode())
59+
}
60+
} else {
61+
error?.invoke(parseErrorCodeFromErrorBody(response.errorBody(), gson))
62+
}
63+
}
64+
})
65+
}
66+
}
67+
)
68+
}
69+
70+
fun Pluto.unbind(
71+
type: Pluto.LoginType,
72+
success: () -> Unit,
73+
error: ((PlutoError) -> Unit)? = null,
74+
handler: Pluto.PlutoRequestHandler? = null
75+
) {
76+
getAuthorizationHeader(
77+
completion = { header ->
78+
if (header == null) {
79+
handler?.setCall(null)
80+
error?.invoke(PlutoError.notSignIn)
81+
return@getAuthorizationHeader
82+
}
83+
84+
val postData = UnbindPostData(type.identifier)
85+
plutoService.unbind(postData, header).apply {
86+
enqueue(object : Callback<PlutoResponse> {
87+
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
88+
t.printStackTrace()
89+
error?.invoke(PlutoError.badRequest)
90+
}
91+
92+
override fun onResponse(
93+
call: Call<PlutoResponse>,
94+
response: Response<PlutoResponse>
95+
) {
96+
val plutoResponse = response.body()
97+
if (plutoResponse != null) {
98+
if (plutoResponse.statusOK()) {
99+
success()
100+
} else {
101+
error?.invoke(plutoResponse.errorCode())
102+
}
103+
} else {
104+
error?.invoke(parseErrorCodeFromErrorBody(response.errorBody(), gson))
105+
}
106+
}
107+
})
108+
}
109+
}
110+
)
111+
}

pluto-kotlin-client-sdk/src/main/java/com/mushare/plutosdk/Pluto.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ class Pluto private constructor() {
3030
}
3131
}
3232

33+
enum class LoginType(val identifier: String) {
34+
mail("mail"),
35+
google("google");
36+
// TODO: wechat
37+
38+
companion object {
39+
private val map = values().associateBy(LoginType::identifier)
40+
41+
fun from(identifier: String): LoginType? {
42+
return map.getValue(identifier)
43+
}
44+
}
45+
}
46+
3347
internal val data by lazy { PlutoModel(context) }
3448

3549
val state: MutableLiveData<State> by lazy {

pluto-kotlin-client-sdk/src/main/java/com/mushare/plutosdk/PlutoModel.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,11 @@ data class PlutoUser(
108108
@field:SerializedName("user_id") var userId: String,
109109
@field:SerializedName("user_id_updated") var userIdUpdated: Boolean,
110110
@field:SerializedName("avatar") var avatar: String,
111-
@field:SerializedName("name") var name: String
111+
@field:SerializedName("name") var name: String,
112+
@field:SerializedName("bindings") var bindings: Array<PlutoUserBinding>
113+
)
114+
115+
data class PlutoUserBinding(
116+
@field:SerializedName("login_type") val loginType: Pluto.LoginType,
117+
@field:SerializedName("mail") var mail: String?
112118
)

pluto-kotlin-client-sdk/src/main/java/com/mushare/plutosdk/PlutoService.kt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ interface PlutoService {
4848
@Body body: UpdateUserInfoPutData,
4949
@HeaderMap authorizationHeader: Map<String, String>
5050
): Call<PlutoResponse>
51+
52+
@POST("v1/user/binding")
53+
fun bind(
54+
@Body body: BindPostData,
55+
@HeaderMap authorizationHeader: Map<String, String>
56+
): Call<PlutoResponse>
57+
58+
@POST("/v1/user/unbinding")
59+
fun unbind(
60+
@Body body: UnbindPostData,
61+
@HeaderMap authorizationHeader: Map<String, String>
62+
): Call<PlutoResponse>
5163
}
5264

5365
class RefreshAuthPostData(
@@ -91,4 +103,15 @@ class UpdateUserInfoPutData(
91103
@field:SerializedName("name") var name: String?,
92104
@field:SerializedName("avatar") var avatar: String?,
93105
@field:SerializedName("user_id") var userId: String?
106+
)
107+
108+
class BindPostData(
109+
@field:SerializedName("type") var type: String,
110+
@field:SerializedName("code") var code: String? = null,
111+
@field:SerializedName("id_token") var idToken: String? = null,
112+
@field:SerializedName("mail") var mail: String? = null
113+
)
114+
115+
class UnbindPostData(
116+
@field:SerializedName("type") var type: String
94117
)

0 commit comments

Comments
 (0)