From 7445e2a3feb792809ef3cfda757eb8307d3821bf Mon Sep 17 00:00:00 2001 From: Mike Kistler Date: Tue, 11 Apr 2023 13:31:35 -0500 Subject: [PATCH] Add custom_client.go and supporting files --- .../azopenai/custom_client.go | 60 +++++++++++++++++++ .../azopenai/policy_apikey.go | 40 +++++++++++++ sdk/cognitiveservices/azopenai/version.go | 11 ++++ 3 files changed, 111 insertions(+) create mode 100644 sdk/cognitiveservices/azopenai/custom_client.go create mode 100644 sdk/cognitiveservices/azopenai/policy_apikey.go create mode 100644 sdk/cognitiveservices/azopenai/version.go diff --git a/sdk/cognitiveservices/azopenai/custom_client.go b/sdk/cognitiveservices/azopenai/custom_client.go new file mode 100644 index 000000000000..b2b17733b9d3 --- /dev/null +++ b/sdk/cognitiveservices/azopenai/custom_client.go @@ -0,0 +1,60 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package azopenai + +// this file contains handwritten additions to the generated code + +import ( + "github.com/Azure/azure-sdk-for-go/sdk/azcore" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" +) + +const ( + clientName = "azopenai.Client" + apiVersion = "2022-12-01" + tokenScope = "https://cognitiveservices.azure.com/.default" +) + +// Clients + +// ClientOptions contains optional settings for Client. +type ClientOptions struct { + azcore.ClientOptions +} + +// NewClient creates a new instance of Client with the specified values. +// - endpoint - OpenAI service endpoint +// - credential - used to authorize requests. Usually a credential from azidentity. +// - options - client options, pass nil to accept the default values. +func NewClient(endpoint string, credential azcore.TokenCredential, options *ClientOptions) (*Client, error) { + if options == nil { + options = &ClientOptions{} + } + authPolicy := runtime.NewBearerTokenPolicy(credential, []string{tokenScope}, nil) + azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{PerRetry: []policy.Policy{authPolicy}}, &options.ClientOptions) + if err != nil { + return nil, err + } + return &Client{endpoint: endpoint + "/openai", internal: azcoreClient}, nil +} + +// NewClient creates a new instance of Client with the specified values. +// - endpoint - OpenAI service endpoint +// - credential - used to authorize requests with an API Key credential +// - options - client options, pass nil to accept the default values. +func NewClientWithKeyCredential(endpoint string, credential KeyCredential, options *ClientOptions) (*Client, error) { + if options == nil { + options = &ClientOptions{} + } + authPolicy := NewAPIKeyPolicy(credential, "api-key") + azcoreClient, err := azcore.NewClient(clientName, version, runtime.PipelineOptions{PerRetry: []policy.Policy{authPolicy}}, &options.ClientOptions) + if err != nil { + return nil, err + } + return &Client{endpoint: endpoint + "/openai", internal: azcoreClient}, nil +} diff --git a/sdk/cognitiveservices/azopenai/policy_apikey.go b/sdk/cognitiveservices/azopenai/policy_apikey.go new file mode 100644 index 000000000000..1d57d1c9296d --- /dev/null +++ b/sdk/cognitiveservices/azopenai/policy_apikey.go @@ -0,0 +1,40 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package azopenai + +import ( + "net/http" + + "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" +) + +// KeyCredential + +type KeyCredential struct { + APIKey string +} + +// APIKeyPolicy authorizes requests with an API key acquired from a KeyCredential. +type APIKeyPolicy struct { + header string + cred KeyCredential +} + +// NewAPIKeyPolicy creates a policy object that authorizes requests with an API Key. +// cred: a KeyCredential implementation. +func NewAPIKeyPolicy(cred KeyCredential, header string) *APIKeyPolicy { + return &APIKeyPolicy{ + header: header, + cred: cred, + } +} + +// Do returns a function which authorizes req with a token from the policy's credential +func (b *APIKeyPolicy) Do(req *policy.Request) (*http.Response, error) { + req.Raw().Header.Set(b.header, b.cred.APIKey) + return req.Next() +} diff --git a/sdk/cognitiveservices/azopenai/version.go b/sdk/cognitiveservices/azopenai/version.go new file mode 100644 index 000000000000..cecc80db67a5 --- /dev/null +++ b/sdk/cognitiveservices/azopenai/version.go @@ -0,0 +1,11 @@ +//go:build go1.18 +// +build go1.18 + +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +package azopenai + +const ( + version = "v0.1.0" +)