Skip to content

Commit 980754e

Browse files
committed
initial implementation of Client
Signed-off-by: 卜部 昌平 <s-urabe@sakura.ad.jp>
1 parent 0ea2c77 commit 980754e

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

client.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

client_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2026- The sacloud/apprun-dedicated-api-go authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package apprun_dedicated_test
5+
6+
import (
7+
"testing"
8+
9+
. "github.com/sacloud/apprun-dedicated-api-go"
10+
"github.com/sacloud/saclient-go"
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestNewClient(t *testing.T) {
15+
assert := require.New(t)
16+
17+
var theClient saclient.Client
18+
actual, err := NewClient(&theClient)
19+
assert.NoError(err)
20+
assert.NotNil(actual)
21+
}

0 commit comments

Comments
 (0)