forked from micromdm/micromdm
/
get_apps.go
86 lines (74 loc) · 2.04 KB
/
get_apps.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package appstore
import (
"context"
"net/http"
"github.com/go-kit/kit/endpoint"
"github.com/groob/plist"
"github.com/micromdm/micromdm/pkg/httputil"
"github.com/pkg/errors"
)
type ListAppsOption struct {
FilterName []string `json:"filter_name"`
}
type AppDTO struct {
Name string `json:"name"`
Payload []byte `json:"payload,omitempty"`
}
func (svc *AppService) ListApplications(ctx context.Context, opts ListAppsOption) ([]AppDTO, error) {
var filter string
if len(opts.FilterName) == 1 {
filter = opts.FilterName[0]
}
apps, err := svc.store.Apps(filter)
if err != nil {
return nil, err
}
var appList []AppDTO
for name, app := range apps {
payload, err := plist.MarshalIndent(&app, " ")
if err != nil {
return nil, errors.Wrap(err, "create dto payload")
}
appList = append(appList, AppDTO{
Name: name,
Payload: payload,
})
}
return appList, nil
}
type appListRequest struct {
Opts ListAppsOption
}
type appListResponse struct {
Apps []AppDTO `json:"apps,omitempty"`
Err error `json:"err,omitempty"`
}
func (r appListResponse) Failed() error { return r.Err }
func decodeListAppsRequest(ctx context.Context, r *http.Request) (interface{}, error) {
var req appListRequest
err := httputil.DecodeJSONRequest(r, &req)
return req, err
}
func decodeListAppsResponse(_ context.Context, r *http.Response) (interface{}, error) {
var resp appListResponse
err := httputil.DecodeJSONResponse(r, &resp)
return resp, err
}
func MakeListAppsEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(appListRequest)
apps, err := svc.ListApplications(ctx, req.Opts)
return appListResponse{
Apps: apps,
Err: err,
}, nil
}
}
func (e Endpoints) ListApplications(ctx context.Context, opts ListAppsOption) ([]AppDTO, error) {
request := appListRequest{opts}
response, err := e.ListAppsEndpoint(ctx, request.Opts)
if err != nil {
return nil, err
}
return response.(appListResponse).Apps, response.(appListResponse).Err
}