-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ECO-4943] feat: basic sending and receiving messages in the Chat
In this commit, we introduce the basic implementation for sending and receiving messages. Corner cases and potential race conditions are not the focus at this stage and will be addressed later, once Room lifecycle management is implemented and a unified test suite is set up.
- Loading branch information
Showing
13 changed files
with
666 additions
and
207 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.ably.chat | ||
|
||
import com.google.gson.JsonElement | ||
import com.google.gson.JsonObject | ||
import com.google.gson.JsonPrimitive | ||
import io.ably.lib.http.HttpCore | ||
import io.ably.lib.http.HttpUtils | ||
import io.ably.lib.types.AblyException | ||
import io.ably.lib.types.ErrorInfo | ||
|
||
internal fun JsonElement?.toRequestBody(useBinaryProtocol: Boolean = false): HttpCore.RequestBody = | ||
HttpUtils.requestBodyFromGson(this, useBinaryProtocol) | ||
|
||
internal fun Map<String, String>.toJson() = JsonObject().apply { | ||
forEach { (key, value) -> addProperty(key, value) } | ||
} | ||
|
||
internal fun JsonElement.toMap() = buildMap<String, String> { | ||
requireJsonObject().entrySet().filter { (_, value) -> value.isJsonPrimitive }.forEach { (key, value) -> put(key, value.asString) } | ||
} | ||
|
||
internal fun JsonElement.requireJsonObject(): JsonObject { | ||
if (!isJsonObject) { | ||
throw AblyException.fromErrorInfo( | ||
ErrorInfo("Response value expected to be JsonObject, got primitive instead", HttpStatusCodes.InternalServerError), | ||
) | ||
} | ||
return asJsonObject | ||
} | ||
|
||
internal fun JsonElement.requireString(memberName: String): String { | ||
val memberElement = requireField(memberName) | ||
if (!memberElement.isJsonPrimitive) { | ||
throw AblyException.fromErrorInfo( | ||
ErrorInfo( | ||
"Value for \"$memberName\" field expected to be JsonPrimitive, got object instead", | ||
HttpStatusCodes.InternalServerError, | ||
), | ||
) | ||
} | ||
return memberElement.asString | ||
} | ||
|
||
internal fun JsonElement.requireLong(memberName: String): Long { | ||
val memberElement = requireJsonPrimitive(memberName) | ||
try { | ||
return memberElement.asLong | ||
} catch (formatException: NumberFormatException) { | ||
throw AblyException.fromErrorInfo( | ||
formatException, | ||
ErrorInfo("Required numeric field \"$memberName\" is not a valid long", HttpStatusCodes.InternalServerError), | ||
) | ||
} | ||
} | ||
|
||
internal fun JsonElement.requireInt(memberName: String): Int { | ||
val memberElement = requireJsonPrimitive(memberName) | ||
try { | ||
return memberElement.asInt | ||
} catch (formatException: NumberFormatException) { | ||
throw AblyException.fromErrorInfo( | ||
formatException, | ||
ErrorInfo("Required numeric field \"$memberName\" is not a valid int", HttpStatusCodes.InternalServerError), | ||
) | ||
} | ||
} | ||
|
||
internal fun JsonElement.requireJsonPrimitive(memberName: String): JsonPrimitive { | ||
val memberElement = requireField(memberName) | ||
if (!memberElement.isJsonPrimitive) { | ||
throw AblyException.fromErrorInfo( | ||
ErrorInfo( | ||
"Value for \"$memberName\" field expected to be JsonPrimitive, got object instead", | ||
HttpStatusCodes.InternalServerError, | ||
), | ||
) | ||
} | ||
return memberElement.asJsonPrimitive | ||
} | ||
|
||
internal fun JsonElement.requireField(memberName: String): JsonElement = requireJsonObject().get(memberName) | ||
?: throw AblyException.fromErrorInfo( | ||
ErrorInfo("Required field \"$memberName\" is missing", HttpStatusCodes.InternalServerError), | ||
) |
Oops, something went wrong.