如何在RxJava 2自定义运算符的onSubscribe上获取RetroFit请求的URL和方法?

时间:2018-01-18 23:42:58

标签: firebase kotlin retrofit2 rx-java2 firebase-performance

因此,我正在尝试为Http请求集成Firebase性能,并在显示here时手动添加它们(步骤9)。

我正在使用Retrofit 2和RxJava 2,所以我想做一个自定义运算符,检查下面的代码:

改造2个客户

@GET("branch-{environment}/v2/branches")
    fun getBranch(@Path("environment") environment: String, @Query("location") location: String, @Query("fulfilment_type") fulfilmentType: String): Single<Response<GetBranchResponse>>

RxJava调用Retrofit客户端

private val client: BranchClient = clientFactory.create(urlProvider.apiUrl)

override fun getBranch(postCode: String, fulfilmentType: FulfilmentType): Single<GetBranchResponse> {
        return client
                .getBranch(environment, postCode.toUpperCase(), fulfilmentType.toString())
                .lift(RxHttpPerformanceSingleOperator(URL?, METHOD?))
                .map { it.body() }
                .subscribeIO() //custom Kotlin extension 
                .observeMain() //custom Kotlin extension 
                ...
    } 

RxJava 2定制操作员通过电梯:

class RxHttpPerformanceSingleOperator<T>(private val url: String, private val method: String) : SingleOperator<Response<T>, Response<T>> {


    private lateinit var metric: HttpMetric

    @Throws(Exception::class)
    override fun apply(observer: SingleObserver<in Response<T>>): SingleObserver<in Response<T>> {
        return object : SingleObserver<Response<T>> {

            override fun onSubscribe(d: Disposable) {

                    metric = FirebasePerformance.getInstance().newHttpMetric(url,
                            method.toUpperCase())
                    metric.start()


                observer.onSubscribe(d)
            }

            override fun onSuccess(t: Response<T>) {
                observer.onSuccess(t)

                //More info: https://firebase.google.com/docs/perf-mon/get-started-android
                metric.setRequestPayloadSize(t.raw().body().contentLength())
                metric.setHttpResponseCode(t.code())
                metric.stop()
            }

            override fun onError(e: Throwable) {
                observer.onError(e)
                metric.stop()
            }


        }
    }

所以目前我不确定如何以正确的方式获取请求的URL和方法(标记为URL?和METHOD?)以发送给运营商

我在onSubscribe上需要它们来启动指标 ..在那里我没有响应...

目前UGLYYYYYYY我的方式是:

添加到Retrofit客户端:

@GET("branch-{environment}/v2/branches")
    fun getBranchURL(@Path("environment") environment: String, @Query("location") location: String, @Query("fulfilment_type") fulfilmentType: String): Call<JsonObject>

添加参数:

val request = client.getBranchURL(environment, postCode.toUpperCase(), fulfilmentType.toString()).request()

url = request.url().toString()
method = request.method()

这使我在每个请求的客户端上都有2个条目......这没有任何意义。

一路上有一些有用的线索: - How to get the request url in retrofit 2.0 with rxjava?

2 个答案:

答案 0 :(得分:3)

使用Interceptor向您的HttpClient.Builder添加改造FirebaseInstance并在那里生成HttpMetrics

class FirebasePerformanceInterceptor(val performanceInstance: FirebasePerformance) : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        //Get request values
        val url = request.url().url()
        val requestPayloadSize = request.body()?.contentLength() ?: 0L
        val httpMethod = request.method()

        //Initialize http trace
        val trace = performanceInstance.newHttpMetric(url, httpMethod)
        trace.setRequestPayloadSize(requestPayloadSize)
        trace.start()

        //Proceed
        val response = chain.proceed(chain.request())

        //Get response values
        val responseCode = response.code()
        val responsePayloadSize = response.body()?.contentLength() ?: 0L

        //Add response values to trace and close it
        trace.setHttpResponseCode(responseCode)
        trace.setResponsePayloadSize(responsePayloadSize)
        trace.stop()

        return response
    }
}

您可以直接复制并粘贴此代码,它将起作用。 享受!

答案 1 :(得分:1)

我要建议的不一定是你的方法,它只是一种不同的方式来思考你想要完​​成的事情。

我建议采用两种不同的方法:

  • 创建自己的观察者(创建一个扩展Observer的类),接收一个Retrofit Call对象并在subscribeActual方法中执行firebase逻辑。
  • 使用Aspectj定义将在要执行Retrofit调用时处理的注释,并且您可以在Aspect中执行firebase逻辑。 (我不确定Aspectj和kotlin是如何工作的)