-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.gen.go
127 lines (113 loc) · 2.72 KB
/
client.gen.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
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
// Code generated by oto; DO NOT EDIT.
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/pkg/errors"
"github.com/peterbourgon/unixtransport"
)
type basicAuth struct {
username string
password string
}
type GatewayClientOptions struct {
basicAuth *basicAuth
timeout *time.Duration
transport http.RoundTripper
}
func NewGatewayClientOptions() *GatewayClientOptions {
return &GatewayClientOptions{}
}
func (o *GatewayClientOptions) SetBasicAuth(username, password string) *GatewayClientOptions {
o.basicAuth = &basicAuth{
username: username,
password: password,
}
return o
}
func (o *GatewayClientOptions) SetTimeout(timeout time.Duration) *GatewayClientOptions {
o.timeout = &timeout
return o
}
func (o *GatewayClientOptions) SetTransport(transport http.RoundTripper) *GatewayClientOptions {
o.transport = transport
return o
}
func (o *GatewayClientOptions) apply(c *gatewayClient) {
c.basicAuth = o.basicAuth
if o.timeout != nil {
c.cl.Timeout = *o.timeout
}
if o.transport != nil {
c.cl.Transport = o.transport
}
}
type gatewayClient struct {
endpoint string
basicAuth *basicAuth
cl *http.Client
}
func NewGatewayClient(
endpoint string,
options ...*GatewayClientOptions,
) Gateway {
var transport http.RoundTripper
if strings.HasPrefix(endpoint, "http+unix://") || strings.HasPrefix(endpoint, "https+unix://") {
if !strings.HasSuffix(endpoint, ":") {
endpoint += ":"
}
t := &http.Transport{}
unixtransport.Register(t)
transport = t
}
c := &gatewayClient{
endpoint: endpoint,
cl: &http.Client{Transport: transport},
}
for _, option := range options {
option.apply(c)
}
return c
}
func (c *gatewayClient) PushEvent(ctx context.Context, req Event) (*Void, error) {
var body bytes.Buffer
if err := json.NewEncoder(&body).Encode(&req); err != nil {
return nil, errors.Wrap(err, "encode")
}
request, err := http.NewRequestWithContext(
ctx,
"POST",
fmt.Sprintf("%s/oto/Gateway.PushEvent", c.endpoint),
&body,
)
if err != nil {
return nil, errors.Wrap(err, "build request")
}
if c.basicAuth != nil {
request.SetBasicAuth(c.basicAuth.username, c.basicAuth.password)
}
request.Header.Set("Content-Type", "application/json")
resp, err := c.cl.Do(request)
if err != nil {
return nil, errors.Wrap(err, "http")
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode == 500 {
return nil, errors.New(string(body))
}
return nil, fmt.Errorf("status code %d: %s", resp.StatusCode, string(body))
}
var response Void
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, errors.Wrap(err, "decode")
}
return &response, nil
}