-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtelemetry.go
57 lines (53 loc) · 1.67 KB
/
telemetry.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package notificationhubs
import (
"context"
"encoding/xml"
"errors"
"net/http"
"net/url"
"path"
"regexp"
)
// NotificationDetails reads one specific registration
func (h *NotificationHub) NotificationDetails(ctx context.Context, notificationID string) (details *NotificationDetails, raw []byte, err error) {
var (
_url = h.generateAPIURL(path.Join("messages", notificationID))
)
_url.RawQuery = url.Values{apiVersionParam: {telemetryAPIVersionValue}}.Encode()
raw, _, err = h.exec(ctx, getMethod, _url, Headers{}, nil)
if err != nil {
return
}
if err = xml.Unmarshal(raw, &details); err != nil {
return
}
return
}
// NewNotificationTelemetryFromLocationURL create Telemetry from Location URL
func NewNotificationTelemetryFromLocationURL(url string) *NotificationTelemetry {
var re = regexp.MustCompile(`/messages/(?P<id>.*)\?api-version=`)
groupNames := re.SubexpNames()
for _, match := range re.FindAllStringSubmatch(url, -1) {
for groupIdx, group := range match {
name := groupNames[groupIdx]
if name == "id" {
return &NotificationTelemetry{
NotificationMessageID: group,
}
}
}
}
return nil
}
// NewNotificationTelemetryFromHTTPResponse reads the Location header from URL
// Notification Telemetry is only available for Standard tier Notification Hubs.
func NewNotificationTelemetryFromHTTPResponse(response *http.Response) (*NotificationTelemetry, error) {
if response == nil || response.Header == nil {
return nil, errors.New("could not parse telemetry from response")
}
location := response.Header.Get("location")
if len(location) == 0 {
return &NotificationTelemetry{}, nil
}
return NewNotificationTelemetryFromLocationURL(location), nil
}