Skip to content

Expose HTTP lifecycle callbacks on RequestFactory #306

@karolgil

Description

@karolgil

Is your feature request related to a problem? Please describe.

RequestFactory is the only extension point for HTTP customization, and its API (openConnection(url): HttpURLConnection) gives us no way to observe upload lifecycle events without resorting to a full HttpURLConnection wrapper.

HttpURLConnection is abstract, so Kotlin's by delegation does not apply. Intercepting even a single method (e.g. getResponseCode() for upload latency measurement) requires a class that manually forwards every method to a delegate, roughly 25+ overrides for the methods the SDK actually calls. The result is fragile boilerplate that silently breaks whenever the SDK starts calling a method that wasn't forwarded.

Describe the solution you'd like

A callback interface on RequestFactory that the SDK invokes after each upload attempt, for example:

open class RequestFactory {
    // existing API …

    /**
     * Called when an upload receives an HTTP response.
     *
     * @param url the upload endpoint
     * @param statusCode HTTP response code
     * @param elapsedMs wall-clock time from opening the connection to receiving the response
     */
    open fun onUploadCompleted(url: String, statusCode: Int, elapsedMs: Long) {}

    /**
     * Called when an upload fails with a network exception before an HTTP response is received.
     *
     * @param url the upload endpoint
     * @param error the IOException that caused the failure
     * @param elapsedMs wall-clock time from opening the connection to the failure
     */
    open fun onUploadError(url: String, error: IOException, elapsedMs: Long) {}
}

Separating the two callbacks keeps each signature clean:

  • onUploadCompleted always has a valid status code.
  • onUploadError always has a real exception and avoids sentinel values like statusCode = -1 that callers would otherwise need to guard against.

This would let us emit structured metrics and fire alerts, all without touching HttpURLConnection 's internals.

Describe alternatives you've considered

Wrapping HttpURLConnection via a subclass delegating to a real instance (the current workaround). It works, but:

  • Requires overriding ~25 methods manually
  • Silently breaks if the SDK starts calling an un-overridden method after an SDK upgrade (values are written to the wrapper's inherited fields, never forwarded to the underlying connection)

Additional context

We are using RequestFactory to route uploads through a proxy, attach custom request headers, and configure timeouts via a remote config system. The onUploadCompleted hook would let us also emit structured log events for slow or failed uploads without the wrapper approach described above.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions