/
client-applyLinks.go
38 lines (29 loc) · 978 Bytes
/
client-applyLinks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package sherlock
import (
"github.com/benpate/hannibal/vocab"
"github.com/benpate/remote"
"github.com/benpate/rosetta/mapof"
"github.com/tomnomnom/linkheader"
)
// applyLinks searches for common link headers in the response, and applies them to the data map
func (client *Client) applyLinks(txn *remote.Transaction, data mapof.Any) {
links := linkheader.ParseMultiple(txn.Response().Header["Link"])
for _, link := range links {
switch link.Rel {
case LinkRelationIcon:
// Add an icon if it doesn't already exist
if _, ok := data[vocab.PropertyIcon]; !ok {
data[vocab.PropertyIcon] = link.URL
}
case LinkRelationHub:
// Guarantee that the `endpoints` value exists
if _, ok := data[vocab.PropertyEndpoints]; !ok {
data[vocab.PropertyEndpoints] = make(map[string]any)
}
// Set the `endpoints.websub` value
if endpoints, ok := data[vocab.PropertyEndpoints].(map[string]any); ok {
endpoints["websub"] = link.URL
}
}
}
}