| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
A Kotlin library that processes server-sent events or streaming HTTP responses in a line-by-line JSON format using OkHttp.
Add the library to your project's dependencies:
dependencies {
implementation("app.pago:okhttp-applicationstream:1.1.0")
}// Create an instance with your base URL
val service = ApplicationStreamService("https://api.example.com")
// GET request: Get a flow of typed objects from a streaming endpoint
val responseType = object : TypeToken<MyDataType>() {}
val dataFlow: Flow<MyDataType> = service.get("stream/endpoint", responseType)
// POST request: Send data and get a flow of typed objects from a streaming endpoint
val requestBody = MyRequestType(param1 = "value", param2 = 123)
val postFlow: Flow<MyResponseType> = service.post(
"stream/endpoint",
requestBody,
object : TypeToken<MyResponseType>() {}
)
// Process the flow in a coroutine scope
lifecycleScope.launch {
dataFlow.collect { data ->
// Process each data object as it arrives
}
}You can customize the service with additional configuration options:
// Create a custom OkHttpClient
val okHttpClient = OkHttpClient.Builder()
.readTimeout(30, TimeUnit.MINUTES)
.writeTimeout(30, TimeUnit.MINUTES)
.callTimeout(30, TimeUnit.MINUTES)
.connectTimeout(30, TimeUnit.MINUTES)
.addInterceptor(yourCustomInterceptor)
.build()
// Configure the service with custom options
val config = ApplicationStreamServiceConfig(
baseUrl = "https://api.example.com",
client = okHttpClient,
readBufferSize = 2048 // Default is 1024
)
val service = ApplicationStreamService(config)| Back | FazBrowse Home | New Git URL |