forked from turms-im/turms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(turms-client-kotlin) Wrap the response data as Response<?> on servic…
…e layers for better extendibility + more useful information turms-im#594
- Loading branch information
1 parent
da0e123
commit 3b3eb87
Showing
22 changed files
with
529 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
turms-client-kotlin/src/main/kotlin/im/turms/client/model/Response.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright (C) 2019 The Turms Project | ||
* https://github.com/turms-im/turms | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package im.turms.client.model | ||
|
||
import im.turms.client.exception.ResponseException | ||
import im.turms.client.extension.isError | ||
import im.turms.client.model.proto.notification.TurmsNotification | ||
import java.util.Collections | ||
|
||
data class Response<T>( | ||
val requestId: Long?, | ||
val code: Int, | ||
val data: T | ||
) { | ||
companion object { | ||
|
||
@JvmStatic | ||
fun <T> value(data: T): Response<T> = Response(null, ResponseStatusCode.OK, data) | ||
|
||
@JvmStatic | ||
fun unitValue(): Response<Unit> = Response(null, ResponseStatusCode.OK, Unit) | ||
|
||
@JvmStatic | ||
fun <T> emptyList(): Response<List<T>> = Response(null, ResponseStatusCode.OK, Collections.emptyList()) | ||
|
||
@JvmStatic | ||
fun <T> fromNotification( | ||
notification: TurmsNotification, | ||
dataTransformer: ((TurmsNotification.Data) -> T)? = null | ||
): Response<T> { | ||
if (!notification.hasCode()) { | ||
throw ResponseException.from( | ||
ResponseStatusCode.INVALID_NOTIFICATION, | ||
"Cannot parse a success response from a notification without code" | ||
) | ||
} | ||
if (notification.isError) { | ||
throw ResponseException.from( | ||
ResponseStatusCode.INVALID_NOTIFICATION, | ||
"Cannot parse a success response from non-success notification" | ||
) | ||
} | ||
val data = if (dataTransformer == null) Unit as T else dataTransformer.invoke(notification.data) | ||
return Response( | ||
if (notification.hasRequestId()) notification.requestId else null, | ||
notification.code, | ||
data | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.