-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
61 lines (54 loc) · 1.54 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package client
import (
"context"
"fmt"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
"net/http"
"net/url"
)
const version = "v1beta"
type serverMode string
const (
Gateway = "https://api-gateway.kyivstar.ua"
ServerModeLive serverMode = ""
ServerModeMock serverMode = "mock"
ServerModeSandbox serverMode = "sandbox"
)
type Config struct {
ServerUrl string `json:"serverUrl,omitempty" yaml:"serverUrl"`
ClientID string `json:"clientId,omitempty" yaml:"clientId"`
ClientSecret string `json:"clientSecret,omitempty" yaml:"clientSecret"`
ServerMode serverMode `json:"serverMode,omitempty" yaml:"serverMode"`
DebugEnabled bool `json:"debugEnabled" yaml:"debugEnabled"`
}
func New(ctx context.Context, conf Config) Client {
authConf := &clientcredentials.Config{
ClientID: conf.ClientID,
ClientSecret: conf.ClientSecret,
TokenURL: fmt.Sprintf("%s/idp/oauth2/token", conf.ServerUrl),
EndpointParams: url.Values{
"grant_type": []string{"client_credentials"},
},
}
client := authConf.Client(ctx)
var serverMode string
if conf.ServerMode != "" {
serverMode = fmt.Sprint("/", conf.ServerMode)
}
ver := version
if conf.DebugEnabled {
ver = fmt.Sprint(ver, "/debug")
}
return v1beta{
requester: &requester{
Client: client,
Url: fmt.Sprint(conf.ServerUrl, serverMode),
Version: ver,
},
}
}
func Wrap(ctx context.Context, client *http.Client, conf Config) Client {
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
return New(ctx, conf)
}