-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
cancelPendingMetadataUpdate.go
49 lines (44 loc) · 1.21 KB
/
cancelPendingMetadataUpdate.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
package service
import (
"fmt"
"net/http"
"github.com/google/go-querystring/query"
"github.com/gorilla/mux"
"github.com/jtacoma/uritemplates"
"github.com/plgd-dev/hub/v2/http-gateway/serverMux"
"github.com/plgd-dev/hub/v2/http-gateway/uri"
)
func (requestHandler *RequestHandler) cancelPendingMetadataUpdate(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
deviceID := vars[uri.DeviceIDKey]
correlationID := vars[uri.CorrelationIDKey]
cannotCancelError := func(err error) error {
return fmt.Errorf("cannot cancel device('%v') metadata update: %w", deviceID, err)
}
type Options struct {
CorrelationID string `url:"correlationIdFilter"`
}
opt := Options{
CorrelationID: correlationID,
}
q, err := query.Values(opt)
if err != nil {
serverMux.WriteError(w, cannotCancelError(err))
return
}
tmp, err := uritemplates.Parse(uri.AliasDevicePendingMetadataUpdates)
if err != nil {
serverMux.WriteError(w, cannotCancelError(err))
return
}
urlPath, err := tmp.Expand(map[string]interface{}{
uri.DeviceIDKey: deviceID,
})
if err != nil {
serverMux.WriteError(w, cannotCancelError(err))
return
}
r.URL.Path = urlPath
r.URL.RawQuery = q.Encode()
requestHandler.mux.ServeHTTP(w, r)
}