-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkrypton_client.go
148 lines (127 loc) · 3.91 KB
/
krypton_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package soratun
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httputil"
"os"
"strings"
"github.com/soracom/soratun/internal"
)
// A SoracomKryptonClient represents a maybe-over-complicated API client for SORACOM Krypton Provisioning API. See
// https://developers.soracom.io/en/api/krypton/
// https://users.soracom.io/ja-jp/tools/krypton-api/
type SoracomKryptonClient interface {
Bootstrap() (*ArcSession, error)
SetVerbose(v bool)
Verbose() bool
}
// A KryptonClientConfig holds SORACOM Krypton provisioning API client related information.
type KryptonClientConfig struct {
Endpoint string
}
// DefaultSoracomKryptonClient is an implementation of the SoracomKryptonClient for the general use case.
type DefaultSoracomKryptonClient struct {
endpoint string // SORACOM Krypton provisioning API endpoint
client *http.Client // HTTP client
verbose bool
}
// NewDefaultSoracomKryptonClient returns new SoracomClient for caller.
func NewDefaultSoracomKryptonClient(config *KryptonClientConfig) SoracomKryptonClient {
c := DefaultSoracomKryptonClient{
endpoint: config.Endpoint,
client: http.DefaultClient,
verbose: false,
}
return &c
}
// SetVerbose sets if verbose output is enabled or not.
func (c *DefaultSoracomKryptonClient) SetVerbose(v bool) {
c.verbose = v
}
// Verbose returns if verbose output is enabled or not.
func (c *DefaultSoracomKryptonClient) Verbose() bool {
return c.verbose
}
// Bootstrap bootstraps Arc virtual SIM.
func (c *DefaultSoracomKryptonClient) Bootstrap() (*ArcSession, error) {
res, err := c.callAPI(&apiParams{
method: "POST",
path: "/provisioning/soracom/arc/bootstrap",
body: "{}",
})
if err != nil {
return nil, err
}
var arcSession ArcSession
err = json.NewDecoder(res.Body).Decode(&arcSession)
return &arcSession, err
}
// BootstrapWithKeyID bootstraps Arc virtual SIM with SIM authentication.
func (c *DefaultSoracomKryptonClient) BootstrapWithKeyID() (*ArcSession, error) {
res, err := c.callAPI(&apiParams{
method: "POST",
path: "/provisioning/soracom/arc/bootstrap",
body: "{}",
})
if err != nil {
return nil, err
}
var config ArcSession
err = json.NewDecoder(res.Body).Decode(&config)
return &config, err
}
func (c *DefaultSoracomKryptonClient) callAPI(params *apiParams) (*http.Response, error) {
req, err := c.makeRequest(params)
if err != nil {
return nil, err
}
if c.Verbose() {
fmt.Fprintln(os.Stderr, "--- Request dump ---------------------------------")
r, _ := httputil.DumpRequest(req, true)
fmt.Fprintln(os.Stderr, string(r))
fmt.Fprintln(os.Stderr, "--- End of request dump --------------------------")
}
res, err := c.doRequest(req)
return res, err
}
func (c *DefaultSoracomKryptonClient) makeRequest(params *apiParams) (*http.Request, error) {
var body io.Reader
if params.body != "" {
body = strings.NewReader(params.body)
}
req, err := http.NewRequest(params.method,
fmt.Sprintf("%s/v1/%s", strings.TrimSuffix(c.endpoint, "/"), strings.TrimPrefix(params.path, "/")),
body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Soracom-Lang", "en")
req.Header.Set("User-Agent", internal.UserAgent)
return req, nil
}
func (c *DefaultSoracomKryptonClient) doRequest(req *http.Request) (*http.Response, error) {
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
if c.Verbose() && res != nil {
fmt.Fprintln(os.Stderr, "--- Response dump --------------------------------")
r, _ := httputil.DumpResponse(res, true)
fmt.Fprintln(os.Stderr, string(r))
fmt.Fprintln(os.Stderr, "--- End of response dump -------------------------")
}
if res.StatusCode >= http.StatusBadRequest {
defer func() {
err := res.Body.Close()
if err != nil {
fmt.Println("failed to close response", err)
}
}()
r, _ := io.ReadAll(res.Body)
return res, fmt.Errorf("%s: %s %s: %s", res.Status, req.Method, req.URL, r)
}
return res, nil
}