-
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.
- Loading branch information
1 parent
0b07330
commit 1707ccb
Showing
123 changed files
with
454 additions
and
338 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
2 changes: 0 additions & 2 deletions
2
onebusaway-sdk-kotlin-core/src/main/kotlin/org/onebusaway/client/OnebusawaySdkClient.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
2 changes: 0 additions & 2 deletions
2
onebusaway-sdk-kotlin-core/src/main/kotlin/org/onebusaway/client/OnebusawaySdkClientAsync.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
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
109 changes: 109 additions & 0 deletions
109
onebusaway-sdk-kotlin-core/src/main/kotlin/org/onebusaway/core/HttpRequestBodies.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,109 @@ | ||
@file:JvmName("HttpRequestBodies") | ||
|
||
package org.onebusaway.core | ||
|
||
import com.fasterxml.jackson.databind.json.JsonMapper | ||
import java.io.ByteArrayOutputStream | ||
import java.io.OutputStream | ||
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder | ||
import org.onebusaway.core.http.HttpRequestBody | ||
import org.onebusaway.errors.OnebusawaySdkException | ||
|
||
internal inline fun <reified T> json( | ||
jsonMapper: JsonMapper, | ||
value: T, | ||
): HttpRequestBody { | ||
return object : HttpRequestBody { | ||
private var cachedBytes: ByteArray? = null | ||
|
||
private fun serialize(): ByteArray { | ||
if (cachedBytes != null) return cachedBytes!! | ||
|
||
val buffer = ByteArrayOutputStream() | ||
try { | ||
jsonMapper.writeValue(buffer, value) | ||
cachedBytes = buffer.toByteArray() | ||
return cachedBytes!! | ||
} catch (e: Exception) { | ||
throw OnebusawaySdkException("Error writing request", e) | ||
} | ||
} | ||
|
||
override fun writeTo(outputStream: OutputStream) { | ||
outputStream.write(serialize()) | ||
} | ||
|
||
override fun contentType(): String = "application/json" | ||
|
||
override fun contentLength(): Long { | ||
return serialize().size.toLong() | ||
} | ||
|
||
override fun repeatable(): Boolean = true | ||
|
||
override fun close() {} | ||
} | ||
} | ||
|
||
internal fun multipartFormData( | ||
jsonMapper: JsonMapper, | ||
parts: Array<MultipartFormValue<*>?> | ||
): HttpRequestBody { | ||
val builder = MultipartEntityBuilder.create() | ||
parts.forEach { part -> | ||
if (part?.value != null) { | ||
when (part.value) { | ||
is JsonValue -> { | ||
val buffer = ByteArrayOutputStream() | ||
try { | ||
jsonMapper.writeValue(buffer, part.value) | ||
} catch (e: Exception) { | ||
throw OnebusawaySdkException("Error serializing value to json", e) | ||
} | ||
builder.addBinaryBody( | ||
part.name, | ||
buffer.toByteArray(), | ||
part.contentType, | ||
part.filename | ||
) | ||
} | ||
is Boolean -> | ||
builder.addTextBody( | ||
part.name, | ||
if (part.value) "true" else "false", | ||
part.contentType | ||
) | ||
is Int -> builder.addTextBody(part.name, part.value.toString(), part.contentType) | ||
is Long -> builder.addTextBody(part.name, part.value.toString(), part.contentType) | ||
is Double -> builder.addTextBody(part.name, part.value.toString(), part.contentType) | ||
is ByteArray -> | ||
builder.addBinaryBody(part.name, part.value, part.contentType, part.filename) | ||
is String -> builder.addTextBody(part.name, part.value, part.contentType) | ||
is Enum -> builder.addTextBody(part.name, part.value.toString(), part.contentType) | ||
else -> | ||
throw IllegalArgumentException( | ||
"Unsupported content type: ${part.value::class.java.simpleName}" | ||
) | ||
} | ||
} | ||
} | ||
val entity = builder.build() | ||
|
||
return object : HttpRequestBody { | ||
override fun writeTo(outputStream: OutputStream) { | ||
try { | ||
return entity.writeTo(outputStream) | ||
} catch (e: Exception) { | ||
throw OnebusawaySdkException("Error writing request", e) | ||
} | ||
} | ||
|
||
override fun contentType(): String = entity.contentType | ||
|
||
override fun contentLength(): Long = -1 | ||
|
||
override fun repeatable(): Boolean = entity.isRepeatable | ||
|
||
override fun close() = entity.close() | ||
} | ||
} |
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.