Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a session.String() method. #197

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"files": "go.sum|^.secrets.baseline$",
"lines": null
},
"generated_at": "2024-09-25T22:48:22Z",
"generated_at": "2024-09-27T22:05:21Z",
"plugins_used": [
{
"name": "AWSKeyDetector"
Expand Down Expand Up @@ -242,7 +242,7 @@
"hashed_secret": "6f667d3e9627f5549ffeb1055ff294c34430b837",
"is_secret": false,
"is_verified": false,
"line_number": 193,
"line_number": 197,
"type": "Secret Keyword",
"verified_result": null
}
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ make test

```bash
gofmt -w `find . -name '*.go' | grep -v vendor`
go generate ./session
go vet -all $(go list ./... | grep -v datatypes)
go mod vendor
go test $(go list ./... | grep -v '/vendor/') -timeout=30s -coverprofile coverage.out
Expand Down
42 changes: 42 additions & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ type Session struct {
// userAgent is the user agent to send with each API request
// User shouldn't be able to change or set the base user agent
userAgent string

// Last API call made in a human readable format
LastCall string
}

//counterfeiter:generate . SLSession
Expand All @@ -172,6 +175,7 @@ type SLSession interface {
SetRetryWait(retryWait time.Duration) *Session
AppendUserAgent(agent string)
ResetUserAgent()
String() string
}

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

err := r.TransportHandler.DoRequest(r, service, method, args, options, pResult)
r.LastCall = CallToString(service, method, args, options)
if err != nil {
return err
}
Expand Down Expand Up @@ -391,6 +396,11 @@ func (r *Session) RefreshToken() error {
return nil
}

// Returns a string of the last api call made.
func (r *Session) String() string {
return r.LastCall
}

func envFallback(keyName string, value *string) {
if *value == "" {
*value = os.Getenv(keyName)
Expand Down Expand Up @@ -464,3 +474,35 @@ func getDefaultUserAgent() string {
}
return fmt.Sprintf("softlayer-go/%s %s ", sl.Version.String(), envAgent)
}

func CallToString(service string, method string, args []interface{}, options *sl.Options) string {
if options == nil {
options = new(sl.Options)
}
default_id := 0
default_mask := "''"
default_filter := "''"
default_args := ""
if options.Id != nil {
default_id = *options.Id
}
if options.Mask != "" {
default_mask = fmt.Sprintf(`'%s'`, options.Mask)
}
if options.Filter != "" {
default_filter = fmt.Sprintf(`'%s'`, options.Filter)
}
if len(args) > 0 {
// This is what softlayer-go/session/rest.go does
parameters, err := json.Marshal(map[string]interface{}{"parameters": args})
default_args = fmt.Sprintf(`'%s'`, string(parameters))
if err != nil {
default_args = err.Error()
}

}
return fmt.Sprintf(
"%s::%s(id=%d, mask=%s, filter=%s, %s)",
service, method, default_id, default_mask, default_filter, default_args,
)
}
55 changes: 55 additions & 0 deletions session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package session
import (
"fmt"
"github.com/jarcoal/httpmock"
"github.com/softlayer/softlayer-go/datatypes"
"github.com/softlayer/softlayer-go/sl"
"os"
"strings"
Expand Down Expand Up @@ -176,3 +177,57 @@ func TestRefreshToken(t *testing.T) {
}
httpmock.Reset()
}

func TestString(t *testing.T) {
// setup session and mock environment
s = New()
s.Endpoint = restEndpoint
s.IAMToken = "Bearer TestToken"
s.IAMRefreshToken = "TestTokenRefresh"
slOptions := &sl.Options{}
slResults := &datatypes.Account{}
// s.Debug = true
httpmock.Activate()
defer httpmock.DeactivateAndReset()
fmt.Printf("TestString [Happy Path]: ")
expected := ""
if s.String() != expected {
t.Errorf("%s != %s", s.String(), expected)
}
// Happy Path
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/SoftLayer_Account.json", restEndpoint),
httpmock.NewStringResponder(200, `{"id":123,"companyName":"test"}`),
)
err := s.DoRequest("SoftLayer_Account", "getObject", nil, slOptions, slResults)
if err != nil {
t.Errorf("Testing Error: %v\n", err.Error())
}
expected = "SoftLayer_Account::getObject(id=0, mask='', filter='', )"
if s.String() != expected {
t.Errorf("%s != %s", s.String(), expected)
}

// Test Setting args
// httpmock.Reset()
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/SoftLayer_Account/999.json", restEndpoint),
httpmock.NewStringResponder(200, `{"id":123,"companyName":"test"}`),
)
fmt.Printf("TestString [Happy Path with args]: ")
slOptions = &sl.Options{
Mask: "mask[id,companyName]",
Filter: `{"id":{"operation":{123}}`,
Id: sl.Int(999),
}
args := []interface{}{
sl.String("https://example.com"),
}
err = s.DoRequest("SoftLayer_Account", "getObject", args, slOptions, slResults)
if err != nil {
t.Errorf("Testing Error: %v\n", err.Error())
}
expected = `SoftLayer_Account::getObject(id=999, mask='mask[id,companyName]', filter='{"id":{"operation":{123}}', '{"parameters":["https://example.com"]}')`
if s.String() != expected {
t.Errorf("%s != %s", s.String(), expected)
}
httpmock.Reset()
}
65 changes: 65 additions & 0 deletions session/sessionfakes/fake_slsession.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading