Skip to content

Commit

Permalink
example: Update SDK examples to use Send Context
Browse files Browse the repository at this point in the history
  • Loading branch information
jasdel committed Mar 18, 2019
1 parent bbee60b commit 751d6b8
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 28 deletions.
10 changes: 6 additions & 4 deletions example/aws/endpoints/customEndpoint/customEndpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package main

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/endpoints"
"github.com/aws/aws-sdk-go-v2/aws/external"
Expand Down Expand Up @@ -40,7 +42,7 @@ func main() {
Bucket: aws.String("myBucket"),
Key: aws.String("myObjectKey"),
})
getReq.Send()
getReq.Send(context.Background())

// Create the SQS service client with the shared config. This will
// fallback to the default endpoint resolver because the customization
Expand All @@ -51,7 +53,7 @@ func main() {
msgReq := sqsSvc.ReceiveMessageRequest(&sqs.ReceiveMessageInput{
QueueUrl: aws.String("my-queue-url"),
})
msgReq.Send()
msgReq.Send(context.Background())

// Create a DynamoDB service client that will use a custom endpoint
// resolver that overrides the shared config's. This is useful when
Expand All @@ -71,7 +73,7 @@ func main() {
// Operation calls will be made to the custom endpoint set in the
// ddCustResolverFn.
listReq := ddbSvc.ListTablesRequest(&dynamodb.ListTablesInput{})
listReq.Send()
listReq.Send(context.Background())

// Setting Config's Endpoint will override the EndpointResolver. Forcing
// the service clien to make all operation to the endpoint specified
Expand All @@ -81,5 +83,5 @@ func main() {

ddbSvcLocal := dynamodb.New(cfgCp)
listReq = ddbSvcLocal.ListTablesRequest(&dynamodb.ListTablesInput{})
listReq.Send()
listReq.Send(context.Background())
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -42,7 +43,7 @@ func main() {
Bucket: aws.String(os.Args[1]),
Key: aws.String(os.Args[2]),
})
resp, err := req.Send()
resp, err := req.Send(context.Background())
if err != nil {
// Casting to the awserr.Error type will allow you to inspect the error
// code returned by the service in code. The error code can be used
Expand Down
3 changes: 1 addition & 2 deletions example/aws/request/withContext/withContext.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ func main() {
Key: aws.String(key),
Body: os.Stdin,
})
req.SetContext(ctx)

if _, err := req.Send(); err != nil {
if _, err := req.Send(ctx); err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == request.ErrCodeRequestCanceled {
// If the SDK can determine the request or retry delay was canceled
// by a context the ErrCodeRequestCanceled error code will be returned.
Expand Down
3 changes: 2 additions & 1 deletion example/service/dynamodb/expression/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -61,7 +62,7 @@ func main() {

// Make the DynamoDB Query API call
req := svc.ScanRequest(params)
result, err := req.Send()
result, err := req.Send(context.Background())
if err != nil {
exitErrorf("failed to make Query API call, %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion example/service/dynamodb/scanItems/scanItems.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -49,7 +50,7 @@ func main() {

// Make the DynamoDB Query API call
scanReq := svc.ScanRequest(params)
result, err := scanReq.Send()
result, err := scanReq.Send(context.Background())
if err != nil {
exitErrorf("failed to make Query API call, %v", err)
}
Expand Down
4 changes: 3 additions & 1 deletion example/service/dynamodb/unitTest/unitTest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package unitTest

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/dynamodbattribute"
Expand Down Expand Up @@ -33,7 +35,7 @@ func (ig *ItemGetter) Get(id string) (value string) {
},
}
req := ig.DynamoDB.GetItemRequest(input)
if output, err := req.Send(); err == nil {
if output, err := req.Send(context.Background()); err == nil {
if v, ok := output.Item["value"]; ok {
dynamodbattribute.Unmarshal(&v, &value)
}
Expand Down
2 changes: 2 additions & 0 deletions example/service/dynamodb/unitTest/unitTest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package unitTest

import (
"errors"
"net/http"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
Expand Down Expand Up @@ -34,6 +35,7 @@ func (fd *fakeDynamoDB) GetItemRequest(input *dynamodb.GetItemInput) dynamodb.Ge
Request: &aws.Request{
Data: output,
Error: fd.err,
HTTPRequest: &http.Request{},
},
}

Expand Down
3 changes: 2 additions & 1 deletion example/service/ec2/filterInstances/filter_ec2_by_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -41,7 +42,7 @@ func main() {
}

req := svc.DescribeInstancesRequest(params)
resp, err := req.Send()
resp, err := req.Send(context.Background())
if err != nil {
exitErrorf("failed to describe instances, %s, %v", awsRegion, err)
}
Expand Down
5 changes: 3 additions & 2 deletions example/service/ec2/instancesbyRegion/instancesByRegion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -61,7 +62,7 @@ func main() {
}

req := ec2Svc.DescribeInstancesRequest(params)
result, err := req.Send()
result, err := req.Send(context.Background())
if err != nil {
exitErrorf("failed to describe instances, %v", err)
} else {
Expand Down Expand Up @@ -90,7 +91,7 @@ func fetchRegion() ([]string, error) {

svc := ec2.New(cfg)
req := svc.DescribeRegionsRequest(&ec2.DescribeRegionsInput{})
awsRegions, err := req.Send()
awsRegions, err := req.Send(context.Background())
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ package main
import (
"database/sql"
"fmt"
"log"
"os"

"github.com/go-sql-driver/mysql"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/aws/stscreds"
"github.com/aws/aws-sdk-go-v2/service/rds/rdsutils"
"github.com/aws/aws-sdk-go-v2/service/sts"
)

// Usage ./iam_authentication <region> <db user> <db name> <endpoint to database> <iam arn>
Expand All @@ -37,8 +36,7 @@ func main() {

credProvider := stscreds.NewAssumeRoleProvider(sts.New(cfg), os.Args[5])

awsCreds := aws.NewCredentials(credProvider)
authToken, err := rdsutils.BuildAuthToken(dbEndpoint, awsRegion, dbUser, awsCreds)
authToken, err := rdsutils.BuildAuthToken(dbEndpoint, awsRegion, dbUser, credProvider)

// Create the MySQL DNS string for the DB connection
// user:password@protocol(endpoint)/dbname?<params>
Expand Down
9 changes: 5 additions & 4 deletions example/service/s3/concatObjects/concatObjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"fmt"
"log"
"net/url"
Expand All @@ -28,7 +29,7 @@ func (c *client) concatenate(key1, key2, key3 string, uploadID *string) (*string
Key: &key3,
UploadId: uploadID,
})
foo, err := req.Send()
foo, err := req.Send(context.Background())
if err != nil {
return nil, nil, err
}
Expand All @@ -42,7 +43,7 @@ func (c *client) concatenate(key1, key2, key3 string, uploadID *string) (*string
Key: &key3,
UploadId: uploadID,
})
bar, err := req.Send()
bar, err := req.Send(context.Background())
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -79,7 +80,7 @@ func main() {
Bucket: &bucket,
Key: &key3,
})
output, err := createReq.Send()
output, err := createReq.Send(context.Background())

if err != nil {
exitErrorf("failed to create upload, %v", err)
Expand Down Expand Up @@ -108,7 +109,7 @@ func main() {
},
},
})
if _, err := compReq.Send(); err != nil {
if _, err := compReq.Send(context.Background()); err != nil {
exitErrorf("failed to complete CompleteMultipartUpload, %v", err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -172,7 +173,7 @@ func (b *Bucket) encryptedObjects() []Object {

func listBuckets(svc *s3.S3) ([]Bucket, error) {
listReq := svc.ListBucketsRequest(&s3.ListBucketsInput{})
res, err := listReq.Send()
res, err := listReq.Send(context.Background())
if err != nil {
return nil, err
}
Expand All @@ -187,7 +188,7 @@ func listBuckets(svc *s3.S3) ([]Bucket, error) {
getReq := svc.GetBucketLocationRequest(&s3.GetBucketLocationInput{
Bucket: b.Name,
})
locRes, err := getReq.Send()
locRes, err := getReq.Send(context.Background())
if err != nil {
buckets[i].Error = err
continue
Expand All @@ -207,7 +208,7 @@ func listBucketObjects(svc *s3.S3, bucket string) ([]Object, []ErrObject, error)
listReq := svc.ListObjectsRequest(&s3.ListObjectsInput{
Bucket: &bucket,
})
listRes, err := listReq.Send()
listRes, err := listReq.Send(context.Background())
if err != nil {
return nil, nil, err
}
Expand All @@ -219,7 +220,7 @@ func listBucketObjects(svc *s3.S3, bucket string) ([]Object, []ErrObject, error)
Bucket: &bucket,
Key: listObj.Key,
})
objData, err := objReq.Send()
objData, err := objReq.Send(context.Background())
if err != nil {
errObjs = append(errObjs, ErrObject{Bucket: bucket, Key: *listObj.Key, Error: err})
continue
Expand Down
3 changes: 2 additions & 1 deletion example/service/s3/putObjectAcl/putObjectAcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"flag"
"fmt"
"os"
Expand Down Expand Up @@ -86,7 +87,7 @@ func main() {
},
},
})
resp, err := req.Send()
resp, err := req.Send(context.Background())
if err != nil {
exitErrorf("failed, %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion example/service/sqs/mockingClientsForTests/ifaceExample.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package main

import (
"context"
"encoding/json"
"fmt"
"os"
Expand Down Expand Up @@ -67,7 +68,7 @@ func (q *Queue) GetMessages(waitTimeout int64) ([]Message, error) {
params.WaitTimeSeconds = aws.Int64(waitTimeout)
}
req := q.Client.ReceiveMessageRequest(&params)
resp, err := req.Send()
resp, err := req.Send(context.Background())
if err != nil {
return nil, fmt.Errorf("failed to get messages, %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package main

import (
"fmt"
"net/http"
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -20,7 +21,8 @@ func (m mockedReceiveMsgs) ReceiveMessageRequest(in *sqs.ReceiveMessageInput) sq
// Only need to return mocked response output
return sqs.ReceiveMessageRequest{
Request: &aws.Request{
Data: &m.Resp,
Data: &m.Resp,
HTTPRequest: &http.Request{},
},
}
}
Expand Down

0 comments on commit 751d6b8

Please sign in to comment.