|
| 1 | +// Copyright 2026- The sacloud/apprun-dedicated-api-go authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package apprun_dedicated |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "runtime" |
| 10 | + |
| 11 | + v1 "github.com/sacloud/apprun-dedicated-api-go/apis/v1" |
| 12 | + "github.com/sacloud/apprun-dedicated-api-go/common" |
| 13 | + "github.com/sacloud/saclient-go" |
| 14 | +) |
| 15 | + |
| 16 | +// DefaultAPIRootURL デフォルトのAPIルートURL |
| 17 | +const DefaultAPIRootURL = "https://secure.sakura.ad.jp/cloud/api/apprun-dedicated/1.0/" |
| 18 | + |
| 19 | +// UserAgent APIリクエスト時のユーザーエージェント |
| 20 | +var UserAgent = fmt.Sprintf( |
| 21 | + "apprun-dedicated-api-go/%s (%s/%s; +https://github.com/sacloud/apprun-dedicated-api-go)", |
| 22 | + Version, |
| 23 | + runtime.GOOS, |
| 24 | + runtime.GOARCH, |
| 25 | +) |
| 26 | + |
| 27 | +// voidSecuritySource is a placeholder to satisfy the SecuritySource interface. |
| 28 | +// saclientにて処理するためここにはロジック不要だが何か渡さないといけないので空の構造体を用意する |
| 29 | +type voidSecuritySource struct{} |
| 30 | + |
| 31 | +func (voidSecuritySource) BasicAuth(context.Context, v1.OperationName) (v1.BasicAuth, error) { |
| 32 | + return v1.BasicAuth{}, nil |
| 33 | +} |
| 34 | + |
| 35 | +func NewClient(client saclient.ClientAPI) (*v1.Client, error) { |
| 36 | + return NewClientWithAPIRootURL(client, DefaultAPIRootURL) |
| 37 | +} |
| 38 | + |
| 39 | +func NewClientWithAPIRootURL(client saclient.ClientAPI, apiRootURL string) (*v1.Client, error) { |
| 40 | + dupable, ok := client.(saclient.ClientOptionAPI) |
| 41 | + |
| 42 | + if !ok { |
| 43 | + return nil, common.NewError("client does not implement saclient.ClientOptionAPI", nil) |
| 44 | + } |
| 45 | + |
| 46 | + augmented, err := dupable.DupWith( |
| 47 | + saclient.WithUserAgent(UserAgent), |
| 48 | + // これはなにか: |
| 49 | + // voidSecuritySource.BasicAuth()がBasic認証を生成 |
| 50 | + // しかし実際の通信で必ずしもBasic認証が使われると限らない |
| 51 | + // そのあたりをsaclient-go側で吸収させる設定が下記↓ |
| 52 | + saclient.WithForceAutomaticAuthentication(), |
| 53 | + ) |
| 54 | + |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + |
| 59 | + return v1.NewClient(apiRootURL, voidSecuritySource{}, v1.WithClient(augmented)) |
| 60 | +} |
0 commit comments