File size: 4,297 Bytes
04f1444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Code generated by @openmeter/typespec-go. DO NOT EDIT.

package openmeter

import (
	"context"
	"fmt"
	"iter"
	"net/http"
	"net/url"
)

type AppsService struct {
	client *Client
}

type AppListParams struct {
	Page *PageParams
}

func (p AppListParams) values() url.Values {
	q := url.Values{}

	addPageParams(q, p.Page)

	return q
}

type AppCatalogItemListParams struct {
	Page *PageParams
}

func (p AppCatalogItemListParams) values() url.Values {
	q := url.Values{}

	addPageParams(q, p.Page)

	return q
}

// List installed apps.
func (s *AppsService) List(ctx context.Context, params AppListParams) (*AppPagePaginatedResponse, error) {
	path := "/openmeter/apps"

	req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, params.values(), nil, "", "application/json")
	if err != nil {
		return nil, err
	}

	var out AppPagePaginatedResponse
	if err := s.client.doJSON(req, &out); err != nil {
		return nil, err
	}

	return &out, nil
}

// ListAll returns an iterator over all App results, fetching pages of List transparently. Iteration stops at the first error, which is yielded as the second value.
func (s *AppsService) ListAll(ctx context.Context, params AppListParams) iter.Seq2[App, error] {
	return paginate(params.Page, func(page, size int) ([]App, int, error) {
		pageParams := params
		pageParams.Page = &PageParams{Size: Int(size), Number: Int(page)}

		resp, err := s.List(ctx, pageParams)
		if err != nil {
			return nil, 0, err
		}

		return resp.Data, resp.Meta.Page.Total, nil
	})
}

// Get an installed app.
func (s *AppsService) Get(ctx context.Context, appID string) (*App, error) {
	if appID == "" {
		return nil, fmt.Errorf("openmeter: %s must not be empty: %w", "appID", ErrEmptyID)
	}

	path := "/openmeter/apps/{appId}"

	path = replacePathParam(path, "appId", appID)

	req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, nil, nil, "", "application/json")
	if err != nil {
		return nil, err
	}

	var out App
	if err := s.client.doJSON(req, &out); err != nil {
		return nil, err
	}

	return &out, nil
}

// List available apps.
func (s *AppsService) ListCatalog(ctx context.Context, params AppCatalogItemListParams) (*AppCatalogItemPagePaginatedResponse, error) {
	path := "/openmeter/app-catalog"

	req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, params.values(), nil, "", "application/json")
	if err != nil {
		return nil, err
	}

	var out AppCatalogItemPagePaginatedResponse
	if err := s.client.doJSON(req, &out); err != nil {
		return nil, err
	}

	return &out, nil
}

// ListCatalogAll returns an iterator over all AppCatalogItem results, fetching pages of ListCatalog transparently. Iteration stops at the first error, which is yielded as the second value.
func (s *AppsService) ListCatalogAll(ctx context.Context, params AppCatalogItemListParams) iter.Seq2[AppCatalogItem, error] {
	return paginate(params.Page, func(page, size int) ([]AppCatalogItem, int, error) {
		pageParams := params
		pageParams.Page = &PageParams{Size: Int(size), Number: Int(page)}

		resp, err := s.ListCatalog(ctx, pageParams)
		if err != nil {
			return nil, 0, err
		}

		return resp.Data, resp.Meta.Page.Total, nil
	})
}

// Get an app catalog item by type.
func (s *AppsService) GetCatalogItem(ctx context.Context, appType string) (*AppCatalogItem, error) {
	if appType == "" {
		return nil, fmt.Errorf("openmeter: %s must not be empty: %w", "appType", ErrEmptyID)
	}

	path := "/openmeter/app-catalog/{appType}"

	path = replacePathParam(path, "appType", appType)

	req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, nil, nil, "", "application/json")
	if err != nil {
		return nil, err
	}

	var out AppCatalogItem
	if err := s.client.doJSON(req, &out); err != nil {
		return nil, err
	}

	return &out, nil
}

// Install an app from the catalog.
func (s *AppsService) Install(ctx context.Context, request InstallAppRequest) (*InstallAppResponse, error) {
	path := "/openmeter/app-catalog/install"

	req, err := s.client.newRequestWithContentType(ctx, http.MethodPost, path, nil, request, "application/json", "application/json")
	if err != nil {
		return nil, err
	}

	var out InstallAppResponse
	if err := s.client.doJSON(req, &out); err != nil {
		return nil, err
	}

	return &out, nil
}