Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pluto-kotlin-client-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'maven-publish'

buildscript {
ext.versionCode = 11
ext.versionCode = 12
ext.versionName = '0.5'
}

Expand All @@ -28,7 +28,6 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

}

task sourceJar(type: Jar) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.mushare.plutosdk

import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

val availableLoginTypes: Array<Pluto.LoginType>
get() = arrayOf(Pluto.LoginType.mail, Pluto.LoginType.mail)

val Pluto.availableBindings: Array<PlutoUserBinding>?
get() = data.user?.bindings

fun Pluto.bind(
type: Pluto.LoginType,
authString: String,
success: () -> Unit,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
getAuthorizationHeader(
completion = { header ->
if (header == null) {
handler?.setCall(null)
error?.invoke(PlutoError.notSignIn)
return@getAuthorizationHeader
}

val postData = when (type) {
Pluto.LoginType.mail -> {
BindPostData(
type = type.identifier,
mail = authString
)
}
Pluto.LoginType.google -> {
BindPostData(
type = type.identifier,
idToken = authString
)
}
}
plutoService.bind(postData, header).apply {
enqueue(object : Callback<PlutoResponse> {
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
t.printStackTrace()
error?.invoke(PlutoError.badRequest)
}

override fun onResponse(
call: Call<PlutoResponse>,
response: Response<PlutoResponse>
) {
val plutoResponse = response.body()
if (plutoResponse != null) {
if (plutoResponse.statusOK()) {
success()
} else {
error?.invoke(plutoResponse.errorCode())
}
} else {
error?.invoke(parseErrorCodeFromErrorBody(response.errorBody(), gson))
}
}
})
}
}
)
}

fun Pluto.unbind(
type: Pluto.LoginType,
success: () -> Unit,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
getAuthorizationHeader(
completion = { header ->
if (header == null) {
handler?.setCall(null)
error?.invoke(PlutoError.notSignIn)
return@getAuthorizationHeader
}

val postData = UnbindPostData(type.identifier)
plutoService.unbind(postData, header).apply {
enqueue(object : Callback<PlutoResponse> {
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
t.printStackTrace()
error?.invoke(PlutoError.badRequest)
}

override fun onResponse(
call: Call<PlutoResponse>,
response: Response<PlutoResponse>
) {
val plutoResponse = response.body()
if (plutoResponse != null) {
if (plutoResponse.statusOK()) {
success()
} else {
error?.invoke(plutoResponse.errorCode())
}
} else {
error?.invoke(parseErrorCodeFromErrorBody(response.errorBody(), gson))
}
}
})
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class Pluto private constructor() {
}
}

enum class LoginType(val identifier: String) {
mail("mail"),
google("google");
// TODO: wechat

companion object {
private val map = values().associateBy(LoginType::identifier)

fun from(identifier: String): LoginType? {
return map.getValue(identifier)
}
}
}

internal val data by lazy { PlutoModel(context) }

val state: MutableLiveData<State> by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,11 @@ data class PlutoUser(
@field:SerializedName("user_id") var userId: String,
@field:SerializedName("user_id_updated") var userIdUpdated: Boolean,
@field:SerializedName("avatar") var avatar: String,
@field:SerializedName("name") var name: String
@field:SerializedName("name") var name: String,
@field:SerializedName("bindings") var bindings: Array<PlutoUserBinding>
)

data class PlutoUserBinding(
@field:SerializedName("login_type") val loginType: Pluto.LoginType,
@field:SerializedName("mail") var mail: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ interface PlutoService {
@Body body: UpdateUserInfoPutData,
@HeaderMap authorizationHeader: Map<String, String>
): Call<PlutoResponse>

@POST("v1/user/binding")
fun bind(
@Body body: BindPostData,
@HeaderMap authorizationHeader: Map<String, String>
): Call<PlutoResponse>

@POST("/v1/user/unbinding")
fun unbind(
@Body body: UnbindPostData,
@HeaderMap authorizationHeader: Map<String, String>
): Call<PlutoResponse>
}

class RefreshAuthPostData(
Expand Down Expand Up @@ -91,4 +103,15 @@ class UpdateUserInfoPutData(
@field:SerializedName("name") var name: String?,
@field:SerializedName("avatar") var avatar: String?,
@field:SerializedName("user_id") var userId: String?
)

class BindPostData(
@field:SerializedName("type") var type: String,
@field:SerializedName("code") var code: String? = null,
@field:SerializedName("id_token") var idToken: String? = null,
@field:SerializedName("mail") var mail: String? = null
)

class UnbindPostData(
@field:SerializedName("type") var type: String
)