Skip to content

Instantly share code, notes, and snippets.

View Unlimity's full-sized avatar
😴
repo sync -j5

Ilya Lim Unlimity

😴
repo sync -j5
  • Facebook
  • London
View GitHub Profile
val client = NinjatoOkHttpClient(okHttpClient) {
retryPolicy = MyRetryPolicy()
fallbackPolicy = MyFallbackPolicy()
retryPolicy { request, throwable ->
if (request.retries > 3) Retry.DoNotRetry else Retry.WithoutDelay
}
fallbackPolicy { request, throwable ->
request.also { it.baseUrl = "https://anotherServer.com" }
class YourRequestFactory : Request.Factory() {
override fun create() = YourRequest()
}
class YourRequestInterceptor : RequestInterceptor() {
fun intercept(request: Request): Request {
if (request is YourRequest) {
request.headers["request_id"] = request.id
}
}
val client = NinjatoOkHttpClient(okHttpClient) {
interceptors += MyRequestInterceptor()
interceptors += MyResponseInterceptor()
interceptors {
request { r ->
Log.d(r)
r
}
interface YourApi {
val api: Api
fun putListing(listing: Listing) = api.put {
endpointUrl = "/putListing"
body = listing
}
}
class YourApi(client: HttpClient, config: Api.() -> Unit) : Api(client, config) {
override val baseUrl = "https://yourApi.com"
fun search(query: String): SearchResult = get {
endpointUrl = "/search"
parameters { "query" to query }
}
}
class RetryCallEnqueueOnSubscribe<T>(
private val originalCall: Call<T>
) : OnSubscribe<Response<T>> {
private var retries = 0
override fun call(subscriber: Subscriber<in Response<T>>) {
val call = originalCall.clone()
val headers = call.request()
class SplashActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate()
Boots {
boot(all()) {
onBoot = { report -> sendToAnalytics(report) }
onFailure = { report -> logErrors(report) }
}
}
class MainApplication : Application() {
@Inject
lateinit var boots: Set<Bootable>
fun onCreate() {
/* Dagger init */
Boots {
add(listOf(boots))
Boots {
val report = report(all())
}
Boots {
// Adding instance of bootable to the system
add(DeviceIdBootable(applicationContext))
// Requesting to boot all bootables and saving listener instance
listener = boot(all()) {
onBoot = { /* do something */ }
}
}