Skip to content

Commit

Permalink
optimize: add context to huawei/obs (dapr#1820)
Browse files Browse the repository at this point in the history
* optimize: add context to huawei/obs

Signed-off-by: 1046102779 <seachen@tencent.com>

* optimize: add context to huawei/obs

Signed-off-by: 1046102779 <seachen@tencent.com>

* optimize: add context to huawei/obs

Signed-off-by: 1046102779 <seachen@tencent.com>

Co-authored-by: Alessandro (Ale) Segala <43508+ItalyPaleAle@users.noreply.github.com>
Co-authored-by: Dapr Bot <56698301+dapr-bot@users.noreply.github.com>
Signed-off-by: Andrew Duss <andy.duss@storable.com>
  • Loading branch information
3 people authored and Andrew Duss committed Aug 18, 2022
1 parent c8105e5 commit 8d37f04
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 87 deletions.
30 changes: 15 additions & 15 deletions bindings/huawei/obs/obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (o *HuaweiOBS) Operations() []bindings.OperationKind {
}
}

func (o *HuaweiOBS) create(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) create(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
d, err := strconv.Unquote(string(req.Data))
if err == nil {
req.Data = []byte(d)
Expand All @@ -151,7 +151,7 @@ func (o *HuaweiOBS) create(req *bindings.InvokeRequest) (*bindings.InvokeRespons
input.Bucket = o.metadata.Bucket
input.Body = r

out, err := o.service.PutObject(input)
out, err := o.service.PutObject(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. putobject: %w", err)
}
Expand All @@ -169,7 +169,7 @@ func (o *HuaweiOBS) create(req *bindings.InvokeRequest) (*bindings.InvokeRespons
}, nil
}

func (o *HuaweiOBS) upload(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) upload(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
var payload uploadPayload
err := json.Unmarshal(req.Data, &payload)
if err != nil {
Expand All @@ -189,7 +189,7 @@ func (o *HuaweiOBS) upload(req *bindings.InvokeRequest) (*bindings.InvokeRespons
input.Bucket = o.metadata.Bucket
input.SourceFile = payload.SourceFile

out, err := o.service.PutFile(input)
out, err := o.service.PutFile(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. putfile: %w", err)
}
Expand All @@ -207,7 +207,7 @@ func (o *HuaweiOBS) upload(req *bindings.InvokeRequest) (*bindings.InvokeRespons
}, nil
}

func (o *HuaweiOBS) get(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) get(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
var key string
if val, ok := req.Metadata[metadataKey]; ok && val != "" {
key = val
Expand All @@ -219,7 +219,7 @@ func (o *HuaweiOBS) get(req *bindings.InvokeRequest) (*bindings.InvokeResponse,
input.Bucket = o.metadata.Bucket
input.Key = key

out, err := o.service.GetObject(input)
out, err := o.service.GetObject(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. error getting obs object: %w", err)
}
Expand All @@ -243,7 +243,7 @@ func (o *HuaweiOBS) get(req *bindings.InvokeRequest) (*bindings.InvokeResponse,
}, nil
}

func (o *HuaweiOBS) delete(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) delete(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
var key string
if val, ok := req.Metadata[metadataKey]; ok && val != "" {
key = val
Expand All @@ -255,7 +255,7 @@ func (o *HuaweiOBS) delete(req *bindings.InvokeRequest) (*bindings.InvokeRespons
input.Bucket = o.metadata.Bucket
input.Key = key

out, err := o.service.DeleteObject(input)
out, err := o.service.DeleteObject(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. error deleting obs object: %w", err)
}
Expand All @@ -273,7 +273,7 @@ func (o *HuaweiOBS) delete(req *bindings.InvokeRequest) (*bindings.InvokeRespons
}, nil
}

func (o *HuaweiOBS) list(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
func (o *HuaweiOBS) list(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
var payload listPayload
err := json.Unmarshal(req.Data, &payload)
if err != nil {
Expand All @@ -292,7 +292,7 @@ func (o *HuaweiOBS) list(req *bindings.InvokeRequest) (*bindings.InvokeResponse,
input.Prefix = payload.Prefix
input.Delimiter = payload.Delimiter

out, err := o.service.ListObjects(input)
out, err := o.service.ListObjects(ctx, input)
if err != nil {
return nil, fmt.Errorf("obs binding error. error listing obs objects: %w", err)
}
Expand All @@ -310,15 +310,15 @@ func (o *HuaweiOBS) list(req *bindings.InvokeRequest) (*bindings.InvokeResponse,
func (o *HuaweiOBS) Invoke(ctx context.Context, req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
switch req.Operation {
case bindings.CreateOperation:
return o.create(req)
return o.create(ctx, req)
case UploadOperation:
return o.upload(req)
return o.upload(ctx, req)
case bindings.GetOperation:
return o.get(req)
return o.get(ctx, req)
case bindings.DeleteOperation:
return o.delete(req)
return o.delete(ctx, req)
case bindings.ListOperation:
return o.list(req)
return o.list(ctx, req)
default:
return nil, fmt.Errorf("obs binding error. unsupported operation %s", req.Operation)
}
Expand Down
36 changes: 20 additions & 16 deletions bindings/huawei/obs/obs_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ limitations under the License.

package obs

import "github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
import (
"context"

"github.com/huaweicloud/huaweicloud-sdk-go-obs/obs"
)

// HuaweiOBSAPI holds only the necessary API functions from the OBS SDK
// The interface can also provide a way to implement stubs for the purpose of unit testing.
type HuaweiOBSAPI interface {
PutObject(input *obs.PutObjectInput) (output *obs.PutObjectOutput, err error)
PutFile(input *obs.PutFileInput) (output *obs.PutObjectOutput, err error)
GetObject(input *obs.GetObjectInput) (output *obs.GetObjectOutput, err error)
DeleteObject(input *obs.DeleteObjectInput) (output *obs.DeleteObjectOutput, err error)
ListObjects(input *obs.ListObjectsInput) (output *obs.ListObjectsOutput, err error)
PutObject(ctx context.Context, input *obs.PutObjectInput) (output *obs.PutObjectOutput, err error)
PutFile(ctx context.Context, input *obs.PutFileInput) (output *obs.PutObjectOutput, err error)
GetObject(ctx context.Context, input *obs.GetObjectInput) (output *obs.GetObjectOutput, err error)
DeleteObject(ctx context.Context, input *obs.DeleteObjectInput) (output *obs.DeleteObjectOutput, err error)
ListObjects(ctx context.Context, input *obs.ListObjectsInput) (output *obs.ListObjectsOutput, err error)
}

// HuaweiOBSService is a service layer which wraps the actual OBS SDK client to provide the API functions
Expand All @@ -31,22 +35,22 @@ type HuaweiOBSService struct {
client *obs.ObsClient
}

func (s *HuaweiOBSService) PutObject(input *obs.PutObjectInput) (output *obs.PutObjectOutput, err error) {
return s.client.PutObject(input)
func (s *HuaweiOBSService) PutObject(ctx context.Context, input *obs.PutObjectInput) (output *obs.PutObjectOutput, err error) {
return s.client.PutObject(input, obs.WithRequestContext(ctx))
}

func (s *HuaweiOBSService) PutFile(input *obs.PutFileInput) (output *obs.PutObjectOutput, err error) {
return s.client.PutFile(input)
func (s *HuaweiOBSService) PutFile(ctx context.Context, input *obs.PutFileInput) (output *obs.PutObjectOutput, err error) {
return s.client.PutFile(input, obs.WithRequestContext(ctx))
}

func (s *HuaweiOBSService) GetObject(input *obs.GetObjectInput) (output *obs.GetObjectOutput, err error) {
return s.client.GetObject(input)
func (s *HuaweiOBSService) GetObject(ctx context.Context, input *obs.GetObjectInput) (output *obs.GetObjectOutput, err error) {
return s.client.GetObject(input, obs.WithRequestContext(ctx))
}

func (s *HuaweiOBSService) DeleteObject(input *obs.DeleteObjectInput) (output *obs.DeleteObjectOutput, err error) {
return s.client.DeleteObject(input)
func (s *HuaweiOBSService) DeleteObject(ctx context.Context, input *obs.DeleteObjectInput) (output *obs.DeleteObjectOutput, err error) {
return s.client.DeleteObject(input, obs.WithRequestContext(ctx))
}

func (s *HuaweiOBSService) ListObjects(input *obs.ListObjectsInput) (output *obs.ListObjectsOutput, err error) {
return s.client.ListObjects(input)
func (s *HuaweiOBSService) ListObjects(ctx context.Context, input *obs.ListObjectsInput) (output *obs.ListObjectsOutput, err error) {
return s.client.ListObjects(input, obs.WithRequestContext(ctx))
}
Loading

0 comments on commit 8d37f04

Please sign in to comment.