2022-08-14 05:59:09 +03:00
package client
2022-09-25 13:54:00 +03:00
import (
2022-10-15 15:03:33 +03:00
"context"
2022-09-25 13:54:00 +03:00
"net/http"
2022-10-15 15:03:33 +03:00
"gitea.com/gitea/act_runner/core"
2022-09-25 13:54:00 +03:00
"github.com/bufbuild/connect-go"
)
type config struct {
httpClient * http . Client
skipVerify bool
opts [ ] connect . ClientOption
}
2022-08-14 05:59:09 +03:00
// An Option configures a mutex.
type Option interface {
2022-09-25 13:54:00 +03:00
apply ( * config )
2022-08-14 05:59:09 +03:00
}
// OptionFunc is a function that configure a value.
2022-09-25 13:54:00 +03:00
type OptionFunc func ( * config )
2022-08-14 05:59:09 +03:00
// Apply calls f(option)
2022-09-25 13:54:00 +03:00
func ( f OptionFunc ) apply ( cfg * config ) {
f ( cfg )
}
func WithSkipVerify ( c bool ) Option {
return OptionFunc ( func ( cfg * config ) {
cfg . skipVerify = c
} )
}
func WithClientOptions ( opts ... connect . ClientOption ) Option {
return OptionFunc ( func ( cfg * config ) {
cfg . opts = append ( cfg . opts , opts ... )
} )
2022-08-14 05:59:09 +03:00
}
// WithGRPC configures clients to use the HTTP/2 gRPC protocol.
func WithGRPC ( c bool ) Option {
2022-09-25 13:54:00 +03:00
return OptionFunc ( func ( cfg * config ) {
2022-08-14 05:59:09 +03:00
if ! c {
return
}
2022-09-25 13:54:00 +03:00
cfg . opts = append ( cfg . opts , connect . WithGRPC ( ) )
2022-08-14 05:59:09 +03:00
} )
}
// WithGRPCWeb configures clients to use the gRPC-Web protocol.
func WithGRPCWeb ( c bool ) Option {
2022-09-25 13:54:00 +03:00
return OptionFunc ( func ( cfg * config ) {
2022-08-14 05:59:09 +03:00
if ! c {
return
}
2022-09-25 13:54:00 +03:00
cfg . opts = append ( cfg . opts , connect . WithGRPCWeb ( ) )
2022-08-14 05:59:09 +03:00
} )
}
2022-10-15 15:03:33 +03:00
// WithUUIDHeader add runner uuid in header
func WithUUIDHeader ( uuid string ) Option {
return OptionFunc ( func ( cfg * config ) {
if uuid == "" {
return
}
cfg . opts = append (
cfg . opts ,
connect . WithInterceptors ( connect . UnaryInterceptorFunc ( func ( next connect . UnaryFunc ) connect . UnaryFunc {
return func ( ctx context . Context , req connect . AnyRequest ) ( connect . AnyResponse , error ) {
req . Header ( ) . Set ( core . UUIDHeader , uuid )
return next ( ctx , req )
}
} ) ) ,
)
} )
}
2022-10-28 18:23:19 +03:00
// WithTokenHeader add runner token in header
func WithTokenHeader ( token string ) Option {
return OptionFunc ( func ( cfg * config ) {
if token == "" {
return
}
cfg . opts = append (
cfg . opts ,
connect . WithInterceptors ( connect . UnaryInterceptorFunc ( func ( next connect . UnaryFunc ) connect . UnaryFunc {
return func ( ctx context . Context , req connect . AnyRequest ) ( connect . AnyResponse , error ) {
req . Header ( ) . Set ( core . TokenHeader , token )
return next ( ctx , req )
}
} ) ) ,
)
} )
}