This file contains hidden or 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
POST /test/api2 | |
Authorization Bearer JWT | |
User-Agent: Make sure to include platform | |
Content-Type: image/jpeg | |
Content-Length: 7351375 | |
Body: file | |
--- | |
202 Accepted |
This file contains hidden or 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
fun chainApi1AndApi1(): Observable<okhttp3.Response> { | |
return Api.api1().flatMap { response1 -> | |
if (response1.isSuccessful) { | |
var uploadUrl = response.headers().get(ApiUtils.Header.LOCATION) | |
uploadUrl?.let { url -> | |
Api.api2(url) | |
} ?: kotlin.run { | |
throw Exceptions.propagate(NullPointerException("Media upload URL is empty")) | |
} | |
} else |
This file contains hidden or 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
fun zipApi1AndApi2(): Observable<Pair<Media, okhttp3.Response>> { | |
return Api.api1().flatMap { response -> | |
if (response.isSuccessful) { | |
var uploadUrl = response.headers().get(ApiUtils.Header.LOCATION) | |
uploadUrl?.let {url -> | |
Observable.zip( | |
Observable.just(response.body()), | |
Api.api2(url) | |
) { t1, t2 -> Pair(t1, t2) } | |
} ?: run { |
This file contains hidden or 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
fun cacheApi1AndZipApi2(): Observable<Pair<Media, okhttp3.Response>> { | |
val api1: Observable<Response<Media>> = Api.api1().cache() | |
return api1.flatMap { response -> | |
if (response.isSuccessful) { | |
var uploadUrl = response.headers().get(ApiUtils.Header.LOCATION) | |
uploadUrl?.let { url -> | |
Api.api2(url).zipWith(api1) { r2, r1 -> Pair(r1.body(), r2) } | |
} ?: run { | |
throw Exceptions.propagate(NullPointerException("Media upload URL is empty")) | |
} |
This file contains hidden or 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
chainApi1AndApi1() | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(object : Subscriber<okhttp3.Response>() { | |
override fun onCompleted() { | |
} | |
override fun onError(e: Throwable) { | |
e.printStackTrace() |
This file contains hidden or 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
zipApi1AndApi2() | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe { pair -> | |
if (pair.second.isSuccessful) | |
Logger.d(pair) | |
else | |
throw ApiRequestException(pair.second) | |
} |
This file contains hidden or 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
package com.machipopo.swag.data | |
import com.machipopo.swag.data.api.ApiHelper | |
import com.machipopo.swag.data.api.model.Media | |
import com.machipopo.swag.utils.ApiRequestException | |
import com.machipopo.swag.utils.ApiUtils | |
import com.orhanobut.logger.Logger | |
import io.reactivex.exceptions.Exceptions | |
import okhttp3.Request | |
import retrofit2.Response |
This file contains hidden or 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
const val KEY_JOB_ID = "jobId" | |
const val KEY_FILE_PATH = "filePath" | |
const val KEY_UPLOAD_URL = "uploadUrl" | |
@Entity | |
data class NewMessage( | |
@PrimaryKey val jobId: String = UUID.randomUUID().toString(), | |
@ColumnInfo val caption: String, | |
@ColumnInfo(name = "media_file") val mediaFile: File, | |
) |
This file contains hidden or 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
class NewMessageCreationWorker(var context: Context, params: WorkerParameters) : Worker(context, params) { | |
private newMessageDb: newMessageDb = ... // from dependency injection | |
override fun doWork(): Result { | |
var jobId = inputData.getString(NewMessage.KEY_JOB_ID)?.run { this } ?: "" | |
var newMessage = newMessageDb.find(jobId) | |
return newMessage?.run { | |
val postMesageResponse = postMessage(this) | |
if (response.isSuccessful) { |
This file contains hidden or 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
object NewMessageManager { | |
fun send(newMessage: NewMessage) { | |
val data = Data.Builder() | |
.putString(NewMessage.KEY_JOB_ID, newMessage.jobId) | |
.build() | |
val woker = OneTimeWorkRequest.Builder(NewMessageCreationWorker::class.java) | |
.setInputData(data) | |
.setConstraints(Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()) | |
.build() | |
WorkManager.getInstance().enqueue(worker) |