This project provides a utility for encoding and decoding Sec-Browsing-Topics HTTP header values. It is based on the Topics API draft specification, which can be found here.
The SecBrowsingTopicsHeader object offers two primary functions:
- decode: Converts a
Sec-Browsing-Topicsheader string into a list ofBrowsingTopicobjects. - encode: Converts a list of
Topicobjects into aSec-Browsing-Topicsheader string.
A data class representing a browsing topic.
data class BrowsingTopic(
val taxonomyVersion: Long,
val modelVersion: Long,
val topicId: Int,
val configVersion: String = "chrome.1",
val version: String = "$configVersion:$modelVersion:$taxonomyVersion"
)A utility object for encoding and decoding Sec-Browsing-Topics header values.
Parses the given Sec-Browsing-Topics header string and returns a list of BrowsingTopic objects.
- Parameters:
headerValue: TheSec-Browsing-Topicsheader string.
- Returns:
- A list of
BrowsingTopicobjects.
- A list of
Encodes a list of Topic objects into a Sec-Browsing-Topics header string.
- Parameters:
topics: The list ofTopicobjects.configVersion: The configuration version string (default is "adfit.1").
- Returns:
- The
Sec-Browsing-Topicsheader string.
- The
import SecBrowsingTopicsHeader.BrowsingTopic
fun main() {
val headerValue = "(1);v=chrome.1:1:2, ();p=P00000000000"
val topics = SecBrowsingTopicsHeader.decode(headerValue)
println(topics) // Output: [BrowsingTopic(taxonomyVersion=2, modelVersion=1, topicId=1, configVersion=chrome.1)]
val topicList = listOf(
BrowsingTopic(1, 1, 1),
BrowsingTopic(1, 1, 2),
BrowsingTopic(1, 1, 100)
)
val encodedHeader = SecBrowsingTopicsHeader.encode(topicList, "chrome.1")
println(encodedHeader) // Output: The encoded header string
}Include the necessary dependencies in your project to use this utility.
This project is licensed under the MIT License.