Skip to content

Commit dc4dd3f

Browse files
Merge pull request #197 from softlayer/sessionString
Added a session.String() method.
2 parents 91ee005 + 6f53aff commit dc4dd3f

File tree

5 files changed

+165
-2
lines changed

5 files changed

+165
-2
lines changed

.secrets.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"files": "go.sum|^.secrets.baseline$",
44
"lines": null
55
},
6-
"generated_at": "2024-09-25T22:48:22Z",
6+
"generated_at": "2024-09-27T22:05:21Z",
77
"plugins_used": [
88
{
99
"name": "AWSKeyDetector"
@@ -242,7 +242,7 @@
242242
"hashed_secret": "6f667d3e9627f5549ffeb1055ff294c34430b837",
243243
"is_secret": false,
244244
"is_verified": false,
245-
"line_number": 193,
245+
"line_number": 197,
246246
"type": "Secret Keyword",
247247
"verified_result": null
248248
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ make test
374374

375375
```bash
376376
gofmt -w `find . -name '*.go' | grep -v vendor`
377+
go generate ./session
377378
go vet -all $(go list ./... | grep -v datatypes)
378379
go mod vendor
379380
go test $(go list ./... | grep -v '/vendor/') -timeout=30s -coverprofile coverage.out

session/session.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ type Session struct {
162162
// userAgent is the user agent to send with each API request
163163
// User shouldn't be able to change or set the base user agent
164164
userAgent string
165+
166+
// Last API call made in a human readable format
167+
LastCall string
165168
}
166169

167170
//counterfeiter:generate . SLSession
@@ -172,6 +175,7 @@ type SLSession interface {
172175
SetRetryWait(retryWait time.Duration) *Session
173176
AppendUserAgent(agent string)
174177
ResetUserAgent()
178+
String() string
175179
}
176180

177181
func init() {
@@ -285,6 +289,7 @@ func (r *Session) DoRequest(service string, method string, args []interface{}, o
285289
}
286290

287291
err := r.TransportHandler.DoRequest(r, service, method, args, options, pResult)
292+
r.LastCall = CallToString(service, method, args, options)
288293
if err != nil {
289294
return err
290295
}
@@ -391,6 +396,11 @@ func (r *Session) RefreshToken() error {
391396
return nil
392397
}
393398

399+
// Returns a string of the last api call made.
400+
func (r *Session) String() string {
401+
return r.LastCall
402+
}
403+
394404
func envFallback(keyName string, value *string) {
395405
if *value == "" {
396406
*value = os.Getenv(keyName)
@@ -464,3 +474,35 @@ func getDefaultUserAgent() string {
464474
}
465475
return fmt.Sprintf("softlayer-go/%s %s ", sl.Version.String(), envAgent)
466476
}
477+
478+
func CallToString(service string, method string, args []interface{}, options *sl.Options) string {
479+
if options == nil {
480+
options = new(sl.Options)
481+
}
482+
default_id := 0
483+
default_mask := "''"
484+
default_filter := "''"
485+
default_args := ""
486+
if options.Id != nil {
487+
default_id = *options.Id
488+
}
489+
if options.Mask != "" {
490+
default_mask = fmt.Sprintf(`'%s'`, options.Mask)
491+
}
492+
if options.Filter != "" {
493+
default_filter = fmt.Sprintf(`'%s'`, options.Filter)
494+
}
495+
if len(args) > 0 {
496+
// This is what softlayer-go/session/rest.go does
497+
parameters, err := json.Marshal(map[string]interface{}{"parameters": args})
498+
default_args = fmt.Sprintf(`'%s'`, string(parameters))
499+
if err != nil {
500+
default_args = err.Error()
501+
}
502+
503+
}
504+
return fmt.Sprintf(
505+
"%s::%s(id=%d, mask=%s, filter=%s, %s)",
506+
service, method, default_id, default_mask, default_filter, default_args,
507+
)
508+
}

session/session_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package session
33
import (
44
"fmt"
55
"github.com/jarcoal/httpmock"
6+
"github.com/softlayer/softlayer-go/datatypes"
67
"github.com/softlayer/softlayer-go/sl"
78
"os"
89
"strings"
@@ -176,3 +177,57 @@ func TestRefreshToken(t *testing.T) {
176177
}
177178
httpmock.Reset()
178179
}
180+
181+
func TestString(t *testing.T) {
182+
// setup session and mock environment
183+
s = New()
184+
s.Endpoint = restEndpoint
185+
s.IAMToken = "Bearer TestToken"
186+
s.IAMRefreshToken = "TestTokenRefresh"
187+
slOptions := &sl.Options{}
188+
slResults := &datatypes.Account{}
189+
// s.Debug = true
190+
httpmock.Activate()
191+
defer httpmock.DeactivateAndReset()
192+
fmt.Printf("TestString [Happy Path]: ")
193+
expected := ""
194+
if s.String() != expected {
195+
t.Errorf("%s != %s", s.String(), expected)
196+
}
197+
// Happy Path
198+
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/SoftLayer_Account.json", restEndpoint),
199+
httpmock.NewStringResponder(200, `{"id":123,"companyName":"test"}`),
200+
)
201+
err := s.DoRequest("SoftLayer_Account", "getObject", nil, slOptions, slResults)
202+
if err != nil {
203+
t.Errorf("Testing Error: %v\n", err.Error())
204+
}
205+
expected = "SoftLayer_Account::getObject(id=0, mask='', filter='', )"
206+
if s.String() != expected {
207+
t.Errorf("%s != %s", s.String(), expected)
208+
}
209+
210+
// Test Setting args
211+
// httpmock.Reset()
212+
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/SoftLayer_Account/999.json", restEndpoint),
213+
httpmock.NewStringResponder(200, `{"id":123,"companyName":"test"}`),
214+
)
215+
fmt.Printf("TestString [Happy Path with args]: ")
216+
slOptions = &sl.Options{
217+
Mask: "mask[id,companyName]",
218+
Filter: `{"id":{"operation":{123}}`,
219+
Id: sl.Int(999),
220+
}
221+
args := []interface{}{
222+
sl.String("https://example.com"),
223+
}
224+
err = s.DoRequest("SoftLayer_Account", "getObject", args, slOptions, slResults)
225+
if err != nil {
226+
t.Errorf("Testing Error: %v\n", err.Error())
227+
}
228+
expected = `SoftLayer_Account::getObject(id=999, mask='mask[id,companyName]', filter='{"id":{"operation":{123}}', '{"parameters":["https://example.com"]}')`
229+
if s.String() != expected {
230+
t.Errorf("%s != %s", s.String(), expected)
231+
}
232+
httpmock.Reset()
233+
}

session/sessionfakes/fake_slsession.go

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)