forked from Lepidopteron/micromdm
/
get_bplueprints.go
71 lines (62 loc) · 1.87 KB
/
get_bplueprints.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package blueprint
import (
"context"
"encoding/json"
"net/http"
"github.com/go-kit/kit/endpoint"
"github.com/micromdm/micromdm/pkg/httputil"
)
func (svc *BlueprintService) GetBlueprints(ctx context.Context, opt GetBlueprintsOption) ([]Blueprint, error) {
if opt.FilterName != "" {
bp, err := svc.store.BlueprintByName(opt.FilterName)
if err != nil {
return nil, err
}
return []Blueprint{*bp}, err
} else {
bps, err := svc.store.List()
if err != nil {
return nil, err
}
return bps, nil
}
}
type getBlueprintsRequest struct{ Opts GetBlueprintsOption }
type getBlueprintsResponse struct {
Blueprints []Blueprint `json:"blueprints"`
Err error `json:"err,omitempty"`
}
func (r getBlueprintsResponse) Failed() error { return r.Err }
func decodeGetBlueprintsRequest(ctx context.Context, r *http.Request) (interface{}, error) {
var opts GetBlueprintsOption
if err := json.NewDecoder(r.Body).Decode(&opts); err != nil {
return nil, err
}
req := getBlueprintsRequest{
Opts: opts,
}
return req, nil
}
func decodeGetBlueprintsResponse(_ context.Context, r *http.Response) (interface{}, error) {
var resp getBlueprintsResponse
err := httputil.DecodeJSONResponse(r, &resp)
return resp, err
}
func MakeGetBlueprintsEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(getBlueprintsRequest)
blueprints, err := svc.GetBlueprints(ctx, req.Opts)
return getBlueprintsResponse{
Blueprints: blueprints,
Err: err,
}, nil
}
}
func (e Endpoints) GetBlueprints(ctx context.Context, opt GetBlueprintsOption) ([]Blueprint, error) {
request := getBlueprintsRequest{opt}
response, err := e.GetBlueprintsEndpoint(ctx, request.Opts)
if err != nil {
return nil, err
}
return response.(getBlueprintsResponse).Blueprints, response.(getBlueprintsResponse).Err
}