2022-08-13 14:41:01 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2022-12-06 08:37:38 +00:00
|
|
|
"code.gitea.io/actions-proto-go/ping/v1/pingv1connect"
|
|
|
|
"code.gitea.io/actions-proto-go/runner/v1/runnerv1connect"
|
2022-11-29 02:35:59 +00:00
|
|
|
"context"
|
|
|
|
"gitea.com/gitea/act_runner/core"
|
|
|
|
"github.com/bufbuild/connect-go"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2022-08-13 14:41:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a new runner client.
|
2022-11-29 02:35:59 +00:00
|
|
|
func New(endpoint string, uuid, token string, opts ...connect.ClientOption) *HTTPClient {
|
2022-12-06 08:37:38 +00:00
|
|
|
baseURL := strings.TrimRight(endpoint, "/") + "/api/actions"
|
2022-08-13 14:41:01 +00:00
|
|
|
|
2022-11-29 02:35:59 +00:00
|
|
|
opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
|
|
|
|
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
|
|
|
|
if uuid != "" {
|
|
|
|
req.Header().Set(core.UUIDHeader, uuid)
|
|
|
|
}
|
|
|
|
if token != "" {
|
|
|
|
req.Header().Set(core.TokenHeader, token)
|
|
|
|
}
|
|
|
|
return next(ctx, req)
|
2022-08-13 14:41:01 +00:00
|
|
|
}
|
2022-11-29 02:35:59 +00:00
|
|
|
})))
|
2022-11-23 03:42:02 +00:00
|
|
|
|
2022-09-25 10:54:00 +00:00
|
|
|
return &HTTPClient{
|
|
|
|
PingServiceClient: pingv1connect.NewPingServiceClient(
|
2022-11-29 02:35:59 +00:00
|
|
|
http.DefaultClient,
|
2022-11-23 03:42:02 +00:00
|
|
|
baseURL,
|
2022-11-29 02:35:59 +00:00
|
|
|
opts...,
|
2022-09-25 10:54:00 +00:00
|
|
|
),
|
|
|
|
RunnerServiceClient: runnerv1connect.NewRunnerServiceClient(
|
2022-11-29 02:35:59 +00:00
|
|
|
http.DefaultClient,
|
2022-11-23 03:42:02 +00:00
|
|
|
baseURL,
|
2022-11-29 02:35:59 +00:00
|
|
|
opts...,
|
2022-09-25 10:54:00 +00:00
|
|
|
),
|
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
|
|
|
}
|