Skip to content

Commit

Permalink
Add custom_client.go and supporting files
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekistler committed Apr 12, 2023
1 parent 539897f commit 79bbeb9
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
57 changes: 57 additions & 0 deletions sdk/cognitiveservices/azopenai/custom_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//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 client that accesses Azure Monitor logs data.
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
}
40 changes: 40 additions & 0 deletions sdk/cognitiveservices/azopenai/policy_apikey.go
Original file line number Diff line number Diff line change
@@ -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()
}
11 changes: 11 additions & 0 deletions sdk/cognitiveservices/azopenai/version.go
Original file line number Diff line number Diff line change
@@ -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"
)

0 comments on commit 79bbeb9

Please sign in to comment.