2022-08-13 14:41:01 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2022-11-22 04:22:19 +00:00
|
|
|
"code.gitea.io/bots-proto-go/ping/v1/pingv1connect"
|
|
|
|
"code.gitea.io/bots-proto-go/runner/v1/runnerv1connect"
|
2022-08-13 14:41:01 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new runner client.
|
2022-10-15 12:03:33 +00:00
|
|
|
func New(endpoint string, opts ...Option) *HTTPClient {
|
2022-09-25 10:54:00 +00:00
|
|
|
cfg := &config{}
|
2022-08-13 14:41:01 +00:00
|
|
|
|
2022-08-14 02:59:09 +00:00
|
|
|
// Loop through each option
|
|
|
|
for _, opt := range opts {
|
|
|
|
// Call the option giving the instantiated
|
2022-09-25 10:54:00 +00:00
|
|
|
opt.apply(cfg)
|
2022-08-14 02:59:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 10:54:00 +00:00
|
|
|
if cfg.httpClient == nil {
|
|
|
|
cfg.httpClient = &http.Client{
|
|
|
|
Timeout: 1 * time.Minute,
|
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
Transport: &http2.Transport{
|
|
|
|
AllowHTTP: true,
|
|
|
|
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
|
|
|
|
return net.Dial(netw, addr)
|
|
|
|
},
|
2022-08-13 14:41:01 +00:00
|
|
|
},
|
2022-09-25 10:54:00 +00:00
|
|
|
}
|
2022-08-13 14:41:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-25 10:54:00 +00:00
|
|
|
if cfg.skipVerify {
|
|
|
|
cfg.httpClient = &http.Client{
|
2022-08-13 14:41:01 +00:00
|
|
|
CheckRedirect: func(*http.Request, []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-09-25 10:54:00 +00:00
|
|
|
|
|
|
|
return &HTTPClient{
|
|
|
|
PingServiceClient: pingv1connect.NewPingServiceClient(
|
|
|
|
cfg.httpClient,
|
|
|
|
endpoint,
|
|
|
|
cfg.opts...,
|
|
|
|
),
|
|
|
|
RunnerServiceClient: runnerv1connect.NewRunnerServiceClient(
|
|
|
|
cfg.httpClient,
|
|
|
|
endpoint,
|
|
|
|
cfg.opts...,
|
|
|
|
),
|
2022-11-15 14:42:41 +00:00
|
|
|
endpoint: endpoint,
|
2022-09-25 10:54:00 +00:00
|
|
|
}
|
2022-08-13 14:41:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:42:41 +00:00
|
|
|
func (c *HTTPClient) Address() string {
|
|
|
|
return c.endpoint
|
|
|
|
}
|
|
|
|
|
2022-08-28 06:05:56 +00:00
|
|
|
var _ Client = (*HTTPClient)(nil)
|
|
|
|
|
2022-08-13 14:41:01 +00:00
|
|
|
// An HTTPClient manages communication with the runner API.
|
|
|
|
type HTTPClient struct {
|
2022-09-25 10:54:00 +00:00
|
|
|
pingv1connect.PingServiceClient
|
|
|
|
runnerv1connect.RunnerServiceClient
|
2022-11-15 14:42:41 +00:00
|
|
|
endpoint string
|
2022-09-03 12:57:32 +00:00
|
|
|
}
|