-
Notifications
You must be signed in to change notification settings - Fork 0
/
rio.go
127 lines (102 loc) · 2.99 KB
/
rio.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"path"
"github.com/t-muehlberger/rio-tachograph-downloader/client"
"github.com/t-muehlberger/rio-tachograph-downloader/client/operations"
"github.com/t-muehlberger/rio-tachograph-downloader/models"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
)
const (
metadataPageSize = 100
)
type rio struct {
httpClient *http.Client
authenticator *authenticator
apiClient *client.TachographFileArchive
config config
}
func NewRio(httpClient *http.Client, auth *authenticator, cfg config) *rio {
r := &rio{
httpClient: httpClient,
authenticator: auth,
config: cfg,
}
transport := httptransport.NewWithClient(client.DefaultHost, client.DefaultBasePath, client.DefaultSchemes, httpClient)
transport.DefaultAuthentication = runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
token, err := auth.GetOrCreateToken()
if err != nil {
return fmt.Errorf("failed to get token for request: %v", err)
}
r.SetHeaderParam("Authorization", "Bearer "+token)
return nil
})
r.apiClient = client.New(transport, strfmt.Default)
return r
}
func (r *rio) GetFileMetadata() (<-chan *models.FileMetadataModel, <-chan error) {
fileInfos := make(chan *models.FileMetadataModel)
error := make(chan error, 1)
go func() {
defer close(fileInfos)
defer close(error)
var offset int32 = 0
var limit int32 = metadataPageSize
hasMore := true
for hasMore {
params := operations.NewGetFilesParams()
params.SetOffset(&offset)
params.SetLimit(&limit)
resp, err := r.apiClient.Operations.GetFiles(params, nil, func(op *runtime.ClientOperation) {
op.ConsumesMediaTypes = []string{"application/json"}
})
if err != nil {
error <- err
return
}
files := resp.Payload
for _, file := range files.Items {
fileInfos <- file
}
hasMore = int(files.Pagination.Offset)+len(files.Items) < int(files.TotalCount)
offset += int32(len(files.Items))
}
}()
return fileInfos, error
}
func (r *rio) DownloadFile(id int32, filePath string) (int64, error) {
url := fmt.Sprintf("%s/files/%d", r.config.apiBaseUrl, id)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return 0, fmt.Errorf("failed to create request: %v", err)
}
token, err := r.authenticator.GetOrCreateToken()
if err != nil {
return 0, fmt.Errorf("failed to get token: %v", err)
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Accept", "application/octet-stream")
resp, err := r.httpClient.Do(req)
if err != nil {
return 0, fmt.Errorf("failed to get file: %v", err)
}
defer resp.Body.Close()
dir := path.Dir(filePath)
os.MkdirAll(dir, 0755)
f, err := os.Create(filePath)
if err != nil {
return 0, fmt.Errorf("failed to create file: %v", err)
}
defer f.Close()
size, err := io.Copy(f, resp.Body)
if err != nil {
log.Fatalf("failed to write file: %v", err)
}
return size, nil
}