Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting custom headers to HTTP requests made to Measure servers #1689

Open
abhaysood opened this issue Jan 3, 2025 · 3 comments
Open
Assignees
Labels
android android related feature new features

Comments

@abhaysood
Copy link
Contributor

Description

Currently, the SDK does not provide a mechanism to add custom HTTP headers to PUT API requests. Users require this to do custom authentication or rate limiting.

Implement a way to configure both the SDK and the gradle plugin to accept custom headers.

@abhaysood abhaysood added feature new features android android related labels Jan 3, 2025
@abhaysood abhaysood self-assigned this Jan 3, 2025
@abhaysood abhaysood moved this to Todo in Measure Roadmap Jan 3, 2025
@abhaysood
Copy link
Contributor Author

abhaysood commented Jan 3, 2025

There are two APIs we plan to expose.

  1. The Measure gradle plugin makes requests at build time for uploading mapping files. To configure headers for the gradle plugin, use the measure extension in your build.gradle file.
measure {
  httpHeaders = mapOf(
    "key" to "value"
  )
}

There will be an option to set different headers for a particular variant using the variant filter API.

measure {
    variantFilter {
        httpHeaders = when {
            name.contains("debug") -> mapOf("key" to "debug-value")
            name.contains("alpha") -> mapOf("key" to "alpha-value")
            else -> mapOf("key" to "prod-value")
        }
    }
}
  1. For HTTP requests made by the SDK at runtime, headers can be configured during SDK initialization.
Measure.init(
  context, 
  MeasureConfig(
    requestHttpHeaders = mapOf("key", "value")
  )
)

@adnan-rapido
Copy link

adnan-rapido commented Jan 3, 2025

Measure.init(
  context, 
  MeasureConfig(
    requestHttpHeaders = mapOf("key", "value")
  )
)

This works for majority of the use cases, though is there a way to update the MeasureConfig post init? We might not have the auth token on the App Start for a non logged in user. So ideally, would like to have an update config method which accepts an updated config object?

Or would a reinit work?

@abhaysood
Copy link
Contributor Author

We can expose an API for taking in a provider instead of a static map during initialization. This provider will get called just before every request. Any valid headers returned will be added to the request. Here's what it might look like:

Config

Measure.init(
  context, 
  MeasureConfig(requestHeadersProvider = CustomHeadersProvider())
)

Provider

interface MsrRequestHeadersProvider {
    fun getRequestHeaders(): Map<String, String>
}

Sample usage

class CustomHeadersProvider: MsrRequestHeadersProvider {
    private val headers = ConcurrentHashMap<String, String>()

    override fun getRequestHeaders(): Map<String, String> {
        return headers.toMap()
    }

    fun setGatewayAuthHeader(value: String) {
        headers["X-Gateway-Token"] = value
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android android related feature new features
Projects
Status: Todo
Development

No branches or pull requests

2 participants