/
client.go
53 lines (43 loc) · 1.16 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package dynamodb
import (
"context"
"log"
"time"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
// https://aws.amazon.com/blogs/developer/aws-sdk-for-go-version-2-general-availability/
type DynamoDBClient interface {
DeleteItemAPI
GetItemAPI
PutItemAPI
UpdateItemAPI
QueryAPI
TransactWriteItemsAPI
ScanAPI
}
type concreteDynamoDBClient struct {
awsclient dynamodb.Client
tableName string
timeout time.Duration
}
func GetClient(inputTableName string, region string) DynamoDBClient {
return GetClientWithTimeout(inputTableName, region, defaultTimeout)
}
func GetClientWithTimeout(inputTableName string, region string, timeout time.Duration) DynamoDBClient {
// Create Amazon S3 API client using path style addressing.
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
client := dynamodb.NewFromConfig(cfg)
return concreteDynamoDBClient{
awsclient: *client,
tableName: inputTableName,
timeout: defaultTimeout,
}
}
const (
defaultTimeoutMS = 5000
defaultTimeout = defaultTimeoutMS * time.Millisecond
)