/
get_can_bus_dump_files_by_eth_address.go
64 lines (53 loc) · 1.91 KB
/
get_can_bus_dump_files_by_eth_address.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
54
55
56
57
58
59
60
61
62
63
64
package queries
import (
"context"
"fmt"
"path/filepath"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/config"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/core/common"
"github.com/DIMO-Network/vehicle-signal-decoding/internal/infrastructure/exceptions"
pgrpc "github.com/DIMO-Network/vehicle-signal-decoding/pkg/grpc"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/rs/zerolog"
"google.golang.org/protobuf/types/known/timestamppb"
)
type GetCanBusDumpFileByEthAddressQueryHandler struct {
logger *zerolog.Logger
s3Client *s3.Client
settings *config.Settings
}
func NewGetCanBusDumpFileByEthAddressQueryHandler(logger *zerolog.Logger, s3Client *s3.Client, settings *config.Settings) GetCanBusDumpFileByEthAddressQueryHandler {
return GetCanBusDumpFileByEthAddressQueryHandler{
logger: logger,
s3Client: s3Client,
settings: settings,
}
}
type GetCanBusDumpFileByEthAddressQueryRequest struct {
EthAddress string
}
func (h GetCanBusDumpFileByEthAddressQueryHandler) Handle(ctx context.Context, query *GetCanBusDumpFileByEthAddressQueryRequest) (*pgrpc.GetCanBusDumpFileResponse, error) {
response, err := h.s3Client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{
Bucket: aws.String(h.settings.AWSCandumpsBucketName),
Prefix: aws.String(query.EthAddress + "/"),
})
if err != nil {
return nil, &exceptions.NotFoundError{
Err: fmt.Errorf("the bucket does not exist"),
}
}
files := []*pgrpc.GetCanBusDumpFileItemResponse{}
for _, item := range response.Contents {
if item.Size > 0 {
files = append(files, &pgrpc.GetCanBusDumpFileItemResponse{
Id: common.RemoveSpecialCharacter(*item.ETag),
Name: filepath.Base(*item.Key),
FullName: *item.Key,
Type: filepath.Ext(*item.Key),
CreatedAt: timestamppb.New(*item.LastModified),
})
}
}
return &pgrpc.GetCanBusDumpFileResponse{Items: files}, nil
}