diff --git a/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerAPIV2.kt b/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerAPIV2.kt index 1762a797d9..c8db066692 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerAPIV2.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/file_server/FileServerAPIV2.kt @@ -15,77 +15,66 @@ import org.session.libsignal.utilities.logging.Log object FileServerAPIV2 { - const val DEFAULT_SERVER = "http://88.99.175.227" private const val DEFAULT_SERVER_PUBLIC_KEY = "7cb31905b55cd5580c686911debf672577b3fb0bff81df4ce2d5c4cb3a7aaa69" + const val DEFAULT_SERVER = "http://88.99.175.227" - sealed class Error : Exception() { - object PARSING_FAILED : Error() - object INVALID_URL : Error() - - fun errorDescription() = when (this) { - PARSING_FAILED -> "Invalid response." - INVALID_URL -> "Invalid URL." - } - + sealed class Error(message: String) : Exception(message) { + object ParsingFailed : Error("Invalid response.") + object InvalidURL : Error("Invalid URL.") } data class Request( - val verb: HTTP.Verb, - val endpoint: String, - val queryParameters: Map = mapOf(), - val parameters: Any? = null, - val headers: Map = mapOf(), - // Always `true` under normal circumstances. You might want to disable - // this when running over Lokinet. - val useOnionRouting: Boolean = true + val verb: HTTP.Verb, + val endpoint: String, + val queryParameters: Map = mapOf(), + val parameters: Any? = null, + val headers: Map = mapOf(), + /** + * Always `true` under normal circumstances. You might want to disable + * this when running over Lokinet. + */ + val useOnionRouting: Boolean = true ) private fun createBody(parameters: Any?): RequestBody? { if (parameters == null) return null - val parametersAsJSON = JsonUtil.toJson(parameters) return RequestBody.create(MediaType.get("application/json"), parametersAsJSON) } private fun send(request: Request): Promise, Exception> { - val parsed = HttpUrl.parse(DEFAULT_SERVER) ?: return Promise.ofFail(OpenGroupAPIV2.Error.InvalidURL) + val url = HttpUrl.parse(DEFAULT_SERVER) ?: return Promise.ofFail(OpenGroupAPIV2.Error.InvalidURL) val urlBuilder = HttpUrl.Builder() - .scheme(parsed.scheme()) - .host(parsed.host()) - .port(parsed.port()) - .addPathSegments(request.endpoint) - + .scheme(url.scheme()) + .host(url.host()) + .port(url.port()) + .addPathSegments(request.endpoint) if (request.verb == HTTP.Verb.GET) { for ((key, value) in request.queryParameters) { urlBuilder.addQueryParameter(key, value) } } - val requestBuilder = okhttp3.Request.Builder() - .url(urlBuilder.build()) - .headers(Headers.of(request.headers)) + .url(urlBuilder.build()) + .headers(Headers.of(request.headers)) when (request.verb) { HTTP.Verb.GET -> requestBuilder.get() HTTP.Verb.PUT -> requestBuilder.put(createBody(request.parameters)!!) HTTP.Verb.POST -> requestBuilder.post(createBody(request.parameters)!!) HTTP.Verb.DELETE -> requestBuilder.delete(createBody(request.parameters)) } - if (request.useOnionRouting) { - return OnionRequestAPI.sendOnionRequest(requestBuilder.build(), DEFAULT_SERVER, DEFAULT_SERVER_PUBLIC_KEY) - .fail { e -> - Log.e("Loki", "FileServerV2 failed with error",e) - } + return OnionRequestAPI.sendOnionRequest(requestBuilder.build(), DEFAULT_SERVER, DEFAULT_SERVER_PUBLIC_KEY).fail { e -> + Log.e("Loki", "File server request failed.", e) + } } else { return Promise.ofFail(IllegalStateException("It's currently not allowed to send non onion routed requests.")) } - } - // region Sending fun upload(file: ByteArray): Promise { val base64EncodedFile = Base64.encodeBytes(file) - val parameters = mapOf("file" to base64EncodedFile) + val parameters = mapOf( "file" to base64EncodedFile ) val request = Request(verb = HTTP.Verb.POST, endpoint = "files", parameters = parameters) return send(request).map { json -> json["result"] as? Long ?: throw OpenGroupAPIV2.Error.ParsingFailed @@ -95,9 +84,8 @@ object FileServerAPIV2 { fun download(file: Long): Promise { val request = Request(verb = HTTP.Verb.GET, endpoint = "files/$file") return send(request).map { json -> - val base64EncodedFile = json["result"] as? String ?: throw Error.PARSING_FAILED - Base64.decode(base64EncodedFile) ?: throw Error.PARSING_FAILED + val base64EncodedFile = json["result"] as? String ?: throw Error.ParsingFailed + Base64.decode(base64EncodedFile) ?: throw Error.ParsingFailed } } - } \ No newline at end of file diff --git a/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroupAPIV2.kt b/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroupAPIV2.kt index 7f88cd3bc8..33f9a9613a 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroupAPIV2.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroupAPIV2.kt @@ -34,23 +34,13 @@ object OpenGroupAPIV2 { private const val DEFAULT_SERVER_PUBLIC_KEY = "a03c383cf63c3c4efe67acc52112a6dd734b3a946b9545f488aaa93da7991238" const val DEFAULT_SERVER = "http://116.203.70.33" - sealed class Error : Exception() { - object Generic : Error() - object ParsingFailed : Error() - object DecryptionFailed : Error() - object SigningFailed : Error() - object InvalidURL : Error() - object NoPublicKey : Error() - - fun errorDescription() = when (this) { - Error.Generic -> "An error occurred." - Error.ParsingFailed -> "Invalid response." - Error.DecryptionFailed -> "Couldn't decrypt response." - Error.SigningFailed -> "Couldn't sign message." - Error.InvalidURL -> "Invalid URL." - Error.NoPublicKey -> "Couldn't find server public key." - } - + sealed class Error(message: String) : Exception(message) { + object Generic : Error("An error occurred.") + object ParsingFailed : Error("Invalid response.") + object DecryptionFailed : Error("Couldn't decrypt response.") + object SigningFailed : Error("Couldn't sign message.") + object InvalidURL : Error("Invalid URL.") + object NoPublicKey : Error("Couldn't find server public key.") } data class DefaultGroup(val id: String, val name: String, val image: ByteArray?) {