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
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.2.2' |
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
import okhttp3.Response | |
interface RestRequest { | |
fun response(): Response | |
fun cancel() | |
abstract class Wrap protected constructor( | |
protected val origin: RestRequest | |
) : RestRequest by origin |
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
import okhttp3.Call | |
import okhttp3.HttpUrl.Companion.toHttpUrl | |
import okhttp3.OkHttpClient | |
import okhttp3.Request | |
import okhttp3.Response | |
class GetReposRequest(private val call: Call) : RestRequest { | |
constructor(client: OkHttpClient, baseUrl: String, user: String) : this( | |
client, |
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
interface Repo { | |
fun id(): Long | |
fun name(): String | |
fun description(): String | |
fun url(): String | |
} |
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
import org.json.JSONObject | |
class JsonRepo(private val jsonObject: JSONObject) : Repo { | |
override fun id(): Long = jsonObject.getLong("id") | |
override fun name(): String = jsonObject.getString("name") | |
override fun description(): String = jsonObject.getString("description") |
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
import okhttp3.Response | |
import org.json.JSONArray | |
class JsonRepos(private val repos: List<Repo>) : List<Repo> by repos { | |
constructor(json: String) : this(JSONArray(json)) | |
constructor(jsonArray: JSONArray) : this( | |
(0 until jsonArray.length()) | |
.map(jsonArray::getJSONObject) | |
.map { JsonRepo(it) } |
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
val repos: List<Repo> = JsonRepos( | |
GetReposRequest( | |
client = client, | |
baseUrl = "https://api.github.com/", | |
user = "RoRoche" | |
) | |
) |
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
import android.util.Log | |
import okhttp3.Response | |
class LoggableRequest( | |
origin: RestRequest, | |
private val tag: String = origin.javaClass.simpleName | |
) : RestRequest.Wrap(origin) { | |
override fun response(): Response { | |
Log.d(tag, "response - started") | |
val response = super.response() |
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
val repos: List<Repo> = JsonRepos( | |
WithBodyRestRequest( | |
SuccessfulRestRequest( | |
LoggableRequest( | |
GetReposRequest( | |
client = client, | |
baseUrl = "https://api.github.com/", | |
user = "RoRoche" | |
) | |
) |
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
D/GetReposRequest: response - started | |
D/GetReposRequest: response - done with Response{protocol=http/1.1, code=200, message=OK, url=https://api.github.com/users/RoRoche/repos} |
OlderNewer