Skip to content

Commit b4cdf6b

Browse files
committed
generate custom route client
1 parent b466576 commit b4cdf6b

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package v3
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"fmt"
8+
"io"
9+
"net/http"
10+
11+
"github.com/grafana/grafana-app-sdk/resource"
12+
)
13+
14+
type VersionRouteClient struct {
15+
client resource.GroupVersionClient
16+
}
17+
18+
func NewVersionRouteClient(client resource.GroupVersionClient) *VersionRouteClient {
19+
return &VersionRouteClient{
20+
client: client,
21+
}
22+
}
23+
24+
func NewVersionRouteClientFromGenerator(generator resource.ClientGenerator) (*VersionRouteClient, error) {
25+
client, err := generator.ClientForGV(GroupVersion)
26+
if err != nil {
27+
return nil, err
28+
}
29+
return NewVersionRouteClient(client), nil
30+
}
31+
32+
type CreateFoobarRequest struct {
33+
Body CreateFoobarRequestBody
34+
Headers http.Header
35+
}
36+
37+
func (c *VersionRouteClient) CreateFoobar(ctx context.Context, namespace string, request CreateFoobarRequest) (*CreateFoobarResponse, error) {
38+
body, err := json.Marshal(request.Body)
39+
if err != nil {
40+
return nil, fmt.Errorf("unable to marshal body to JSON: %w", err)
41+
}
42+
resp, err := c.client.CustomRouteRequest(ctx, namespace, "", "", resource.CustomRouteRequestOptions{
43+
Path: "/foobar",
44+
Verb: "POST",
45+
Body: io.NopCloser(bytes.NewReader(body)),
46+
Headers: request.Headers,
47+
})
48+
if err != nil {
49+
return nil, err
50+
}
51+
cast := CreateFoobarResponse{}
52+
err = json.Unmarshal(resp, &cast)
53+
if err != nil {
54+
return nil, fmt.Errorf("unable to unmarshal response bytes into CreateFoobarResponse: %w", err)
55+
}
56+
return &cast, nil
57+
}

0 commit comments

Comments
 (0)