Skip to content

Commit

Permalink
Feat: support passing do key in http header
Browse files Browse the repository at this point in the history
Signed-off-by: Yin Da <yd219913@alibaba-inc.com>
  • Loading branch information
Somefive committed Feb 22, 2023
1 parent 48b398d commit 5635c3a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions cue/cuex/runtime/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ func (in *externalPackage) GetProviderFn(do string) ProviderFn {
if in.src.Spec.Provider == nil {
return nil
}
fn := ExternalProviderFn(*in.src.Spec.Provider)
return &fn
return &ExternalProviderFn{
Provider: *in.src.Spec.Provider,
Do: do,
}
}

func (in *externalPackage) GetName() string {
Expand Down
9 changes: 8 additions & 1 deletion cue/cuex/runtime/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ func (fn GenericProviderFn[T, U]) Call(ctx context.Context, value cue.Value) (cu
var _ ProviderFn = (*ExternalProviderFn)(nil)

// ExternalProviderFn external provider that implements ProviderFn interface
type ExternalProviderFn v1alpha1.Provider
type ExternalProviderFn struct {
v1alpha1.Provider
Do string
}

// DefaultClientInsecureSkipVerify set if the default external provider client
// use insecure-skip-verify
Expand All @@ -74,6 +77,9 @@ var DefaultClient = singleton.NewSingleton(func() *http.Client {
}
})

// FunctionHeaderKey http header for recording cuex provider function
const FunctionHeaderKey = "CueX-External-Provider-Function"

// Call dial external endpoints by passing the json data of the input parameter,
// then fill back returned values
func (in *ExternalProviderFn) Call(ctx context.Context, value cue.Value) (cue.Value, error) {
Expand All @@ -89,6 +95,7 @@ func (in *ExternalProviderFn) Call(ctx context.Context, value cue.Value) (cue.Va
return value, err
}
req.Header.Set("Content-Type", runtime.ContentTypeJSON)
req.Header.Set(FunctionHeaderKey, in.Do)
resp, err := DefaultClient.Get().Do(req.WithContext(ctx))
if err != nil {
return value, err
Expand Down

0 comments on commit 5635c3a

Please sign in to comment.