-
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
3c1bdcf
commit c96b445
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
android/app/src/main/java/com/epfl/dedis/hbt/di/HttpModule.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,19 @@ | ||
package com.epfl.dedis.hbt.di | ||
|
||
import android.content.Context | ||
import dagger.Module | ||
import dagger.Provides | ||
import dagger.hilt.InstallIn | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dagger.hilt.components.SingletonComponent | ||
import org.chromium.net.CronetEngine | ||
import javax.inject.Singleton | ||
|
||
@Module | ||
@InstallIn(SingletonComponent::class) | ||
class HttpModule { | ||
@Provides | ||
@Singleton | ||
fun provideCronet(@ApplicationContext context: Context): CronetEngine = | ||
CronetEngine.Builder(context).build() | ||
} |
68 changes: 68 additions & 0 deletions
68
android/app/src/main/java/com/epfl/dedis/hbt/service/http/HttpService.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,68 @@ | ||
package com.epfl.dedis.hbt.service.http | ||
|
||
import io.reactivex.rxjava3.core.Single | ||
import io.reactivex.rxjava3.subjects.SingleSubject | ||
import org.chromium.net.CronetEngine | ||
import org.chromium.net.CronetException | ||
import org.chromium.net.UrlRequest | ||
import org.chromium.net.UrlResponseInfo | ||
import java.nio.ByteBuffer | ||
import java.util.concurrent.Executors | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class HttpService @Inject constructor(private val engine: CronetEngine) { | ||
|
||
private val executor = Executors.newSingleThreadExecutor() | ||
|
||
fun get(url: String): Single<String> { | ||
val single = SingleSubject.create<String>() | ||
|
||
engine.newUrlRequestBuilder(url, Callback(single), executor) | ||
.setHttpMethod("GET") | ||
.build() | ||
.start() | ||
|
||
return single | ||
} | ||
|
||
private class Callback(private val single: SingleSubject<String>) : UrlRequest.Callback() { | ||
override fun onRedirectReceived( | ||
request: UrlRequest?, | ||
info: UrlResponseInfo?, | ||
newLocationUrl: String? | ||
) { | ||
request?.followRedirect() | ||
} | ||
|
||
override fun onResponseStarted(request: UrlRequest?, info: UrlResponseInfo?) { | ||
request?.read(ByteBuffer.allocateDirect(BYTE_BUFFER_CAPACITY_BYTES)) | ||
} | ||
|
||
override fun onReadCompleted( | ||
request: UrlRequest?, | ||
info: UrlResponseInfo?, | ||
byteBuffer: ByteBuffer? | ||
) { | ||
byteBuffer?.clear() | ||
request?.read(byteBuffer) | ||
} | ||
|
||
override fun onSucceeded(request: UrlRequest?, info: UrlResponseInfo?) { | ||
single.onSuccess(info?.httpStatusCode.toString()) | ||
} | ||
|
||
override fun onFailed( | ||
request: UrlRequest?, | ||
info: UrlResponseInfo?, | ||
error: CronetException? | ||
) { | ||
single.onError(error ?: Exception("Unknown error")) | ||
} | ||
|
||
companion object { | ||
private const val BYTE_BUFFER_CAPACITY_BYTES = 100 * 1024 | ||
} | ||
} | ||
} |