Skip to content
This repository has been archived by the owner on Jun 16, 2019. It is now read-only.

Commit

Permalink
Fix retrofit interface
Browse files Browse the repository at this point in the history
  • Loading branch information
afollestad committed Jun 1, 2018
1 parent ec69a99 commit 77c45ab
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
7 changes: 4 additions & 3 deletions dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ext.versions = [
minSdk : 14,
compileSdk : 27,
publishVersion: '4.0.2',
versionCode : 6,
publishVersion: '4.0.3',
versionCode : 7,
kotlin : '1.2.41',

versionsPlugin: '0.17.0',
Expand All @@ -16,5 +16,6 @@ ext.versions = [
rxJava : '2.1.13',
rxAndroid : '2.0.2',
glide : '4.7.1',
retrofit : '2.4.0'
retrofit : '2.4.0',
gson : '2.8.4'
]
2 changes: 2 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ dependencies {
implementation 'com.android.support:support-annotations:' + versions.supportLib
implementation 'com.squareup.retrofit2:retrofit:' + versions.retrofit
implementation 'com.squareup.retrofit2:adapter-rxjava2:' + versions.retrofit
implementation 'com.squareup.retrofit2:converter-gson:' + versions.retrofit
implementation 'com.google.code.gson:gson:' + versions.gson

api 'io.reactivex.rxjava2:rxjava:' + versions.rxJava
api 'io.reactivex.rxjava2:rxandroid:' + versions.rxAndroid
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.afollestad.iconrequest.remote

data class ApiResponse(
var status: String = "success",
var error: String? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.afollestad.iconrequest.remote

import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import java.io.IOException

class ApiTypeAdapter : TypeAdapter<ApiResponse>() {
companion object {
private const val KEY_STATUS = "status"
private const val KEY_ERROR = "error"
}

@Throws(IOException::class)
override fun write(
out: JsonWriter,
value: ApiResponse
) {
out.beginObject()
out.name(KEY_STATUS)
.value(value.status)
out.name(KEY_ERROR)
.value(value.error)
out.endObject()
}

@Throws(IOException::class)
override fun read(input: JsonReader): ApiResponse? {
input.beginObject()
var status = "success"
var error: String? = null
while (input.hasNext()) {
val next = input.nextString()
when (next) {
"status" -> status = next
"error" -> error = next
}
}
input.endObject()
return ApiResponse(status = status, error = error)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ import com.afollestad.iconrequest.extensions.toUri
import com.afollestad.iconrequest.extensions.writeAll
import com.afollestad.iconrequest.extensions.writeIconTo
import com.afollestad.iconrequest.extensions.zipInto
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import io.reactivex.Observable
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.RequestBody
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.Date

Expand Down Expand Up @@ -201,10 +204,16 @@ internal class RealSendInteractor(private val context: Context) : SendInteractor
val archiveFile = MultipartBody.Part.createFormData("archive", "icons.zip", archiveFileBody)
val appsJson = MultipartBody.Part.createFormData("apps", jsonSb.toString())
api.performRequest(archiveFile, appsJson)
.flatMap {
if (it.status == "error") {
Observable.error(Exception(it.error))
} else {
Observable.just(true)
}
}
.doOnNext {
"Request uploaded to the server!".log(TAG)
}
.map { true }
} else {
launchIntent(zipFile, request.uriTransformer, config, selectedApps)
}
Expand Down Expand Up @@ -259,10 +268,15 @@ internal class RealSendInteractor(private val context: Context) : SendInteractor
.build()
chain.proceed(request)
}
val gson = GsonBuilder()
.serializeNulls()
.registerTypeAdapter(ApiResponse::class.java, ApiTypeAdapter())
.create()
val retrofit = Retrofit.Builder()
.baseUrl(host)
.client(httpClient.build())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
return retrofit.create(RequestManagerApi::class.java)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package com.afollestad.iconrequest.remote

import io.reactivex.Observable
import okhttp3.MultipartBody
import retrofit2.http.Multipart
import retrofit2.http.POST
import retrofit2.http.Part

interface RequestManagerApi {
@Multipart
@POST("/v1/request")
fun performRequest(
@Part archive: MultipartBody.Part,
@Part appsJson: MultipartBody.Part
): Observable<Void>
): Observable<ApiResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class MainActivity : AppCompatActivity(), Toolbar.OnMenuItemClickListener {
list.adapter = adapter

val config =
ArcticConfig(emailRecipient = "fake-email@helloworld.com", errorOnInvalidDrawables = false)
ArcticConfig(emailRecipient = "fake-email@helloworld.com", errorOnInvalidDrawables = false,
apiHost = "https://arcticmanager.com",
apiKey = "helloworld")
val uriTransformer: UriTransformer = {
FileProvider.getUriForFile(
this@MainActivity,
Expand Down

0 comments on commit 77c45ab

Please sign in to comment.