Skip to content

Commit 0f5c024

Browse files
authored
Merge pull request #14 from cto-ai/feat/sdk-user
PROD-572 Add ability to retrieve user information
2 parents 8a76691 + 687437c commit 0f5c024

File tree

10 files changed

+89
-48
lines changed

10 files changed

+89
-48
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
module github.com/cto-ai/sdk-go
1+
module github.com/cto-ai/sdk-go/v2
22

33
go 1.13

internal/daemon/daemon.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,25 @@ func port() int {
2626
return port
2727
}
2828

29-
func daemonRequest(endpoint string, body interface{}) (*http.Response, error) {
29+
func daemonRequest(endpoint string, body interface{}, method string) (*http.Response, error) {
3030
bodyBytes, err := json.Marshal(body)
3131
if err != nil {
3232
return nil, fmt.Errorf("Error marshalling JSON body: %w", err)
3333
}
3434

35-
resp, err := http.Post(
36-
fmt.Sprintf("http://127.0.0.1:%d/%s", port(), endpoint),
37-
"application/json",
38-
bytes.NewBuffer(bodyBytes),
39-
)
35+
var resp *http.Response
36+
37+
if method == "POST" {
38+
resp, err = http.Post(
39+
fmt.Sprintf("http://127.0.0.1:%d/%s", port(), endpoint),
40+
"application/json",
41+
bytes.NewBuffer(bodyBytes),
42+
)
43+
} else if method == "GET" {
44+
resp, err = http.Get(
45+
fmt.Sprintf("http://127.0.0.1:%d/%s", port(), endpoint),
46+
)
47+
}
4048
if err != nil {
4149
return nil, fmt.Errorf("Error in daemon request: %w", err)
4250
}
@@ -54,13 +62,13 @@ func daemonRequest(endpoint string, body interface{}) (*http.Response, error) {
5462
return resp, nil
5563
}
5664

57-
func SimpleRequest(endpoint string, body interface{}) error {
58-
_, err := daemonRequest(endpoint, body)
65+
func SimpleRequest(endpoint string, body interface{}, method string) error {
66+
_, err := daemonRequest(endpoint, body, method)
5967
return err
6068
}
6169

62-
func SyncRequest(endpoint string, body interface{}) (interface{}, error) {
63-
resp, err := daemonRequest(endpoint, body)
70+
func SyncRequest(endpoint string, body interface{}, method string) (interface{}, error) {
71+
resp, err := daemonRequest(endpoint, body, method)
6472
if err != nil {
6573
return nil, err
6674
}
@@ -77,8 +85,8 @@ func SyncRequest(endpoint string, body interface{}) (interface{}, error) {
7785
return responseBody.Value, nil
7886
}
7987

80-
func AsyncRequest(endpoint string, body interface{}) (map[string]interface{}, error) {
81-
resp, err := daemonRequest(endpoint, body)
88+
func AsyncRequest(endpoint string, body interface{}, method string) (map[string]interface{}, error) {
89+
resp, err := daemonRequest(endpoint, body, method)
8290
if err != nil {
8391
return nil, err
8492
}

print_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"reflect"
99
"testing"
1010

11-
"github.com/cto-ai/sdk-go/internal/daemon"
11+
"github.com/cto-ai/sdk-go/v2/internal/daemon"
1212
)
1313

1414
func TestItalic(t *testing.T) {

progressbar_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"reflect"
88
"testing"
99

10-
"github.com/cto-ai/sdk-go/internal/daemon"
10+
"github.com/cto-ai/sdk-go/v2/internal/daemon"
1111
)
1212

1313
func Test_ProgressbarRequest_ProgressbarStart(t *testing.T) {

prompt.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
"time"
66

7-
"github.com/cto-ai/sdk-go/internal/daemon"
7+
"github.com/cto-ai/sdk-go/v2/internal/daemon"
88
)
99

1010
// Prompt is the object that contains the prompt methods
@@ -71,7 +71,7 @@ func (*Prompt) Input(name, msg string, options ...InputOption) (string, error) {
7171
option(&definition)
7272
}
7373

74-
body, err := daemon.AsyncRequest("prompt", definition)
74+
body, err := daemon.AsyncRequest("prompt", definition, "POST")
7575
if err != nil {
7676
return "", err
7777
}
@@ -146,7 +146,7 @@ func (*Prompt) Number(name, msg string, options ...NumberOption) (int, error) {
146146
option(&definition)
147147
}
148148

149-
body, err := daemon.AsyncRequest("prompt", definition)
149+
body, err := daemon.AsyncRequest("prompt", definition, "POST")
150150
if err != nil {
151151
return 0, err
152152
}
@@ -206,7 +206,7 @@ func (*Prompt) Secret(name, msg string, options ...SecretOption) (string, error)
206206
option(&definition)
207207
}
208208

209-
body, err := daemon.AsyncRequest("prompt", definition)
209+
body, err := daemon.AsyncRequest("prompt", definition, "POST")
210210
if err != nil {
211211
return "", err
212212
}
@@ -274,7 +274,7 @@ func (*Prompt) Password(name, msg string, options ...PasswordOption) (string, er
274274
option(&definition)
275275
}
276276

277-
body, err := daemon.AsyncRequest("prompt", definition)
277+
body, err := daemon.AsyncRequest("prompt", definition, "POST")
278278
if err != nil {
279279
return "", err
280280
}
@@ -332,7 +332,7 @@ func (*Prompt) Confirm(name, msg string, options ...ConfirmOption) (bool, error)
332332
option(&definition)
333333
}
334334

335-
body, err := daemon.AsyncRequest("prompt", definition)
335+
body, err := daemon.AsyncRequest("prompt", definition, "POST")
336336
if err != nil {
337337
return false, err
338338
}
@@ -413,7 +413,7 @@ func (*Prompt) List(name, msg string, choices []string, options ...ListOption) (
413413
option(&definition)
414414
}
415415

416-
body, err := daemon.AsyncRequest("prompt", definition)
416+
body, err := daemon.AsyncRequest("prompt", definition, "POST")
417417
if err != nil {
418418
return "", err
419419
}
@@ -484,7 +484,7 @@ func (*Prompt) Checkbox(name, msg string, choices []string, options ...CheckboxO
484484
option(&definition)
485485
}
486486

487-
body, err := daemon.AsyncRequest("prompt", definition)
487+
body, err := daemon.AsyncRequest("prompt", definition, "POST")
488488
if err != nil {
489489
return nil, err
490490
}
@@ -558,7 +558,7 @@ func (*Prompt) Editor(name, msg string, options ...EditorOption) (string, error)
558558
option(&definition)
559559
}
560560

561-
body, err := daemon.AsyncRequest("prompt", definition)
561+
body, err := daemon.AsyncRequest("prompt", definition, "POST")
562562
if err != nil {
563563
return "", err
564564
}
@@ -645,7 +645,7 @@ func (*Prompt) Datetime(name, msg string, options ...DatetimeOption) (time.Time,
645645
option(&definition)
646646
}
647647

648-
body, err := daemon.AsyncRequest("prompt", definition)
648+
body, err := daemon.AsyncRequest("prompt", definition, "POST")
649649
if err != nil {
650650
return time.Unix(0, 0), err
651651
}

prompt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"testing"
1111
"time"
1212

13-
"github.com/cto-ai/sdk-go/internal/daemon"
13+
"github.com/cto-ai/sdk-go/v2/internal/daemon"
1414
)
1515

1616
func Test_PromptRequest_PromptInput(t *testing.T) {

sdk.go

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"os"
77

8-
"github.com/cto-ai/sdk-go/internal/daemon"
8+
"github.com/cto-ai/sdk-go/v2/internal/daemon"
99
)
1010

1111
func getenv(name, fallback string) string {
@@ -60,13 +60,13 @@ func (*Sdk) GetConfigPath() string {
6060
// GetState returns a value from the state (workflow-local) key/value store
6161
// DEPRECATED: state is used by deprecated workflows feature
6262
func (s *Sdk) GetState(key string) (interface{}, error) {
63-
return daemon.SyncRequest("state/get", map[string]interface{}{"key": key})
63+
return daemon.SyncRequest("state/get", map[string]interface{}{"key": key}, "POST")
6464
}
6565

6666
// GetAllState returns a map of all keys to values in the state (workflow-local) key/value store
6767
// DEPRECATED: state is used by deprecated workflows feature
6868
func (s *Sdk) GetAllState() (map[string]interface{}, error) {
69-
value, err := daemon.SyncRequest("state/get-all", map[string]interface{}{})
69+
value, err := daemon.SyncRequest("state/get-all", map[string]interface{}{}, "POST")
7070
if err != nil {
7171
return nil, err
7272
}
@@ -84,12 +84,12 @@ func (s *Sdk) SetState(key string, value interface{}) error {
8484
return daemon.SimpleRequest("state/set", map[string]interface{}{
8585
"key": key,
8686
"value": value,
87-
})
87+
}, "POST")
8888
}
8989

9090
// GetConfig returns a value from the config (user-specific) key/value store
9191
func (s *Sdk) GetConfig(key string) (string, error) {
92-
daemonValue, err := daemon.SyncRequest("config/get", map[string]string{"key": key})
92+
daemonValue, err := daemon.SyncRequest("config/get", map[string]string{"key": key}, "POST")
9393
if err != nil {
9494
return "", err
9595
}
@@ -107,7 +107,7 @@ func (s *Sdk) GetConfig(key string) (string, error) {
107107

108108
// GetAllConfig returns a map of all keys to values in the config (workflow-local) key/value store
109109
func (s *Sdk) GetAllConfig() (map[string]string, error) {
110-
value, err := daemon.SyncRequest("config/get-all", map[string]string{})
110+
value, err := daemon.SyncRequest("config/get-all", map[string]string{}, "POST")
111111
if err != nil {
112112
return nil, err
113113
}
@@ -135,13 +135,13 @@ func (s *Sdk) SetConfig(key string, value string) error {
135135
return daemon.SimpleRequest("config/set", map[string]string{
136136
"key": key,
137137
"value": value,
138-
})
138+
}, "POST")
139139
}
140140

141141
// DeleteConfig deletes a value from the config (user-specific) key/value store
142142
// Returns false if key not found, true if success
143143
func (s *Sdk) DeleteConfig(key string) (bool, error) {
144-
daemonValue, err := daemon.SyncRequest("config/delete", map[string]string{"key": key})
144+
daemonValue, err := daemon.SyncRequest("config/delete", map[string]string{"key": key}, "POST")
145145
if err != nil {
146146
return false, err
147147
}
@@ -174,7 +174,7 @@ func (*Sdk) GetSecret(key string, options ...GetSecretOption) (string, error) {
174174
option(&requestBody)
175175
}
176176

177-
body, err := daemon.AsyncRequest("secret/get", requestBody)
177+
body, err := daemon.AsyncRequest("secret/get", requestBody, "POST")
178178
if err != nil {
179179
return "", err
180180
}
@@ -189,7 +189,7 @@ func (*Sdk) GetSecret(key string, options ...GetSecretOption) (string, error) {
189189
//
190190
// If the secret already exists, the user is prompted on whether to overwrite it.
191191
func (*Sdk) SetSecret(key string, value string) (string, error) {
192-
body, err := daemon.AsyncRequest("secret/set", daemon.SetSecretBody{Key: key, Value: value})
192+
body, err := daemon.AsyncRequest("secret/set", daemon.SetSecretBody{Key: key, Value: value}, "POST")
193193
if err != nil {
194194
return "", err
195195
}
@@ -222,7 +222,7 @@ func (*Sdk) Track(tags []string, event string, metadata map[string]interface{})
222222
}
223223

224224
// We suppress this error to be consistent with other languages
225-
_ = daemon.SimpleRequest("track", requestBody)
225+
_ = daemon.SimpleRequest("track", requestBody, "POST")
226226

227227
return nil
228228
}
@@ -240,7 +240,7 @@ func (*Sdk) Start(workflowName string) error {
240240
}
241241

242242
// We suppress this error to be consistent with other languages
243-
_ = daemon.SimpleRequest("track", requestBody)
243+
_ = daemon.SimpleRequest("track", requestBody, "POST")
244244

245245
return nil
246246
}
@@ -249,7 +249,7 @@ func (*Sdk) Events(start, end string) ([]map[string]interface{}, error) {
249249
result, err := daemon.SyncRequest("events", daemon.EventsBody{
250250
Start: start,
251251
End: end,
252-
})
252+
}, "POST")
253253

254254
if err != nil {
255255
return nil, fmt.Errorf("error getting events from backend: %w", err)
@@ -271,3 +271,36 @@ func (*Sdk) Events(start, end string) ([]map[string]interface{}, error) {
271271

272272
return events, nil
273273
}
274+
275+
// UserInfo contains user info returned by daemon.
276+
type UserInfo struct {
277+
ID string `json:"id"`
278+
Username string `json:"username"`
279+
Email string `json:"email"`
280+
}
281+
282+
// User returns the user info for the current user running the Op.
283+
func (*Sdk) User() (UserInfo, error) {
284+
var body interface{}
285+
method := "GET"
286+
result, err := daemon.SyncRequest("user", body, method)
287+
288+
if err != nil {
289+
return UserInfo{}, fmt.Errorf("error getting user information: %w", err)
290+
}
291+
292+
// map results to UserInfo
293+
mapValue := result.(map[string]interface{})
294+
userInfo := UserInfo{}
295+
if id, ok := mapValue["id"].(string); ok {
296+
userInfo.ID = id
297+
}
298+
if username, ok := mapValue["username"].(string); ok {
299+
userInfo.Username = username
300+
}
301+
if email, ok := mapValue["email"].(string); ok {
302+
userInfo.Email = email
303+
}
304+
305+
return userInfo, nil
306+
}

secrets_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"reflect"
1010
"testing"
1111

12-
"github.com/cto-ai/sdk-go/internal/daemon"
12+
"github.com/cto-ai/sdk-go/v2/internal/daemon"
1313
)
1414

1515
func Test_SecretsRequest_GetSecret(t *testing.T) {

spinner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"reflect"
88
"testing"
99

10-
"github.com/cto-ai/sdk-go/internal/daemon"
10+
"github.com/cto-ai/sdk-go/v2/internal/daemon"
1111
)
1212

1313
func Test_SpinnerRequest_spinnerStart(t *testing.T) {

0 commit comments

Comments
 (0)