yume-runner/client/http.go
Jason Song 6d2200b3d6 Rename to actions (#7)
Co-authored-by: Jason Song <i@wolfogre.com>
Reviewed-on: https://gitea.com/gitea/act_runner/pulls/7
2022-12-06 16:37:38 +08:00

55 lines
1.3 KiB
Go

package client
import (
"code.gitea.io/actions-proto-go/ping/v1/pingv1connect"
"code.gitea.io/actions-proto-go/runner/v1/runnerv1connect"
"context"
"gitea.com/gitea/act_runner/core"
"github.com/bufbuild/connect-go"
"net/http"
"strings"
)
// New returns a new runner client.
func New(endpoint string, uuid, token string, opts ...connect.ClientOption) *HTTPClient {
baseURL := strings.TrimRight(endpoint, "/") + "/api/actions"
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)
}
})))
return &HTTPClient{
PingServiceClient: pingv1connect.NewPingServiceClient(
http.DefaultClient,
baseURL,
opts...,
),
RunnerServiceClient: runnerv1connect.NewRunnerServiceClient(
http.DefaultClient,
baseURL,
opts...,
),
endpoint: endpoint,
}
}
func (c *HTTPClient) Address() string {
return c.endpoint
}
var _ Client = (*HTTPClient)(nil)
// An HTTPClient manages communication with the runner API.
type HTTPClient struct {
pingv1connect.PingServiceClient
runnerv1connect.RunnerServiceClient
endpoint string
}