Skip to content

Commit

Permalink
Support Anonymous API Call (#126)
Browse files Browse the repository at this point in the history
* Support Anonymous API Call

* Add test for Anonymous API Call
  • Loading branch information
zhaohuxing authored Jan 27, 2021
1 parent dee32b4 commit e326d0b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ func NewDefault() (c *Config, err error) {

// Check checks the configuration.
func (c *Config) Check() (err error) {
if c.AccessKeyID == "" {
if c.AccessKeyID == "" && c.SecretAccessKey != "" {
err = errors.New("access key ID not specified")
return
}
if c.SecretAccessKey == "" {
if c.SecretAccessKey == "" && c.AccessKeyID != "" {
err = errors.New("secret access key not specified")
return
}
Expand Down
12 changes: 7 additions & 5 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ func (r *Request) SignWithContext(ctx context.Context) error {
ctx = context.Background()
}

err := r.sign(ctx)
if err != nil {
return err
if r.Operation.Config.AccessKeyID != "" && r.Operation.Config.SecretAccessKey != "" {
err := r.sign(ctx)
if err != nil {
return err
}
}

return nil
Expand Down Expand Up @@ -201,14 +203,14 @@ func (r *Request) ApplyQuerySignature(accessKeyID string, expires int, signature
}

func (r *Request) check(ctx context.Context) error {
if r.Operation.Config.AccessKeyID == "" {
if r.Operation.Config.AccessKeyID == "" && r.Operation.Config.SecretAccessKey != "" {
return errors.NewSDKError(
errors.WithAction("check request"),
errors.WithError(fmt.Errorf("access key not provided")),
)
}

if r.Operation.Config.SecretAccessKey == "" {
if r.Operation.Config.SecretAccessKey == "" && r.Operation.Config.AccessKeyID != "" {
return errors.NewSDKError(
errors.WithAction("check request"),
errors.WithError(fmt.Errorf("secret access key not provided")),
Expand Down
36 changes: 36 additions & 0 deletions request/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,39 @@ func TestRequestSend(t *testing.T) {
assert.Equal(t, "1e588695254aa08cf7a43f612e6ce14b", e.RequestID)
}
}

func TestAnonymousAPICall(t *testing.T) {
conf, err := config.New("", "")
assert.Nil(t, err)

operation := &data.Operation{
Config: conf,
Properties: &SomeActionProperties{
A: String("aaa"),
B: String("bbb"),
CD: String("ccc-ddd"),
},
APIName: "Some Action",
RequestMethod: "GET",
RequestURI: "/<a>/<b>/<c-d>",
StatusCodes: []int{
200, // OK
206, // Partial content
304, // Not modified
412, // Precondition failed
},
}

output := &SomeActionOutput{}
r, err := New(operation, &SomeActionInput{
Date: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, time.UTC)),
IfModifiedSince: Time(time.Date(2016, 9, 1, 15, 30, 0, 0, time.UTC)),
Range: String("100-"),
UploadID: String("0"),
Count: Int(23),
}, output)
assert.Nil(t, err)

r.SendWithContext(nil)
assert.Equal(t, r.HTTPRequest.Header.Get("Authorization"), "")
}

0 comments on commit e326d0b

Please sign in to comment.