-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_files.go
More file actions
164 lines (136 loc) · 4.81 KB
/
Copy pathbatch_files.go
File metadata and controls
164 lines (136 loc) · 4.81 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package viya
import (
"bytes"
"context"
"fmt"
"io"
"net/url"
"time"
"go.opentelemetry.io/otel/codes"
)
// BatchFile describes a file stored in a SAS Viya Batch file set.
type BatchFile struct {
CreationTimeStamp time.Time `json:"creationTimeStamp"`
CreatedBy string `json:"createdBy"`
ModifiedTimeStamp time.Time `json:"modifiedTimeStamp"`
ModifiedBy string `json:"modifiedBy"`
FileSetID string `json:"fileSetId"`
Name string `json:"name"`
Size int64 `json:"size"`
Links []Link `json:"links"`
Version int `json:"version"`
}
// BatchFileResponse is a collection of SAS Viya Batch files.
type BatchFileResponse = ListResponse[BatchFile]
// GetBatchFile returns files in a SAS Viya Batch file set.
func (c *Client) GetBatchFile(ctx context.Context, fileSetId string) (resp BatchFileResponse, err error) {
if fileSetId == "" {
return resp, &ErrInvalidParameter{Parameter: "fileSetId", Reason: "must not be empty"}
}
ctx, span := tracer.Start(ctx, "GetBatchFileSetFiles")
defer span.End()
r, err := c.client.R().
SetContext(ctx).
SetHeader("Accept", AcceptCollection).
SetResult(&resp).
Get(fmt.Sprintf("/batch/fileSets/%s/files", fileSetId))
if err != nil {
return resp, err
}
if !r.IsSuccess() {
span.SetStatus(codes.Error, r.String())
return resp, fmt.Errorf("failed to get batch file set files, status code: %d", r.StatusCode())
}
return resp, nil
}
// GetBatchFileInfo returns metadata for one file in a SAS Viya Batch file set.
func (c *Client) GetBatchFileInfo(ctx context.Context, fileSetId string, fileName string) (resp BatchFile, err error) {
if fileSetId == "" {
return resp, &ErrInvalidParameter{Parameter: "fileSetId", Reason: "must not be empty"}
}
ctx, span := tracer.Start(ctx, "GetBatchFileInfo")
defer span.End()
r, err := c.client.R().
SetContext(ctx).
SetHeader("Accept", AcceptBatchFile).
SetResult(&resp).
Get(fmt.Sprintf("/batch/fileSets/%s/files/%s", fileSetId, url.PathEscape(fileName)))
if err != nil {
return resp, err
}
if !r.IsSuccess() {
span.SetStatus(codes.Error, r.String())
return resp, fmt.Errorf("failed to get batch file info, status code: %d", r.StatusCode())
}
return resp, nil
}
// DownloadBatchFile downloads a file from a SAS Viya Batch file set.
func (c *Client) DownloadBatchFile(ctx context.Context, fileSetId string, fileName string) (content []byte, err error) {
if fileSetId == "" {
return nil, &ErrInvalidParameter{Parameter: "fileSetId", Reason: "must not be empty"}
}
ctx, span := tracer.Start(ctx, "DownloadBatchFile")
defer span.End()
r, err := c.client.R().
SetContext(ctx).
SetHeader("Accept", "application/octet-stream").
Get(fmt.Sprintf("/batch/fileSets/%s/files/%s", fileSetId, url.PathEscape(fileName)))
if err != nil {
return nil, err
}
if !r.IsSuccess() {
span.SetStatus(codes.Error, r.String())
return nil, fmt.Errorf("failed to download batch file, status code: %d", r.StatusCode())
}
defer r.Body.Close()
content, err = io.ReadAll(r.Body)
if err != nil {
span.SetStatus(codes.Error, err.Error())
return nil, fmt.Errorf("failed to read batch file content: %w", err)
}
return content, nil
}
// UploadBatchFile uploads or replaces a file in a SAS Viya Batch file set.
func (c *Client) UploadBatchFile(ctx context.Context, fileSetId string, fileName string, content []byte) (err error) {
if fileSetId == "" {
return &ErrInvalidParameter{Parameter: "fileSetId", Reason: "must not be empty"}
}
ctx, span := tracer.Start(ctx, "UploadBatchFile")
defer span.End()
err = c.uploadBatchFileFromReader(ctx, fileSetId, fileName, bytes.NewReader(content))
if err != nil {
span.SetStatus(codes.Error, err.Error())
return err
}
return nil
}
// UploadBatchFileFromReader uploads or replaces a file in a SAS Viya Batch file set
// from content read from r.
func (c *Client) UploadBatchFileFromReader(ctx context.Context, fileSetId string, fileName string, r io.Reader) (err error) {
if fileSetId == "" {
return &ErrInvalidParameter{Parameter: "fileSetId", Reason: "must not be empty"}
}
ctx, span := tracer.Start(ctx, "UploadBatchFileFromReader")
defer span.End()
err = c.uploadBatchFileFromReader(ctx, fileSetId, fileName, r)
if err != nil {
span.SetStatus(codes.Error, err.Error())
return err
}
return nil
}
func (c *Client) uploadBatchFileFromReader(ctx context.Context, fileSetId string, fileName string, r io.Reader) (err error) {
resp, err := c.client.R().
SetContext(ctx).
SetHeader("Accept", AcceptErrorOnly).
SetContentType("application/octet-stream").
SetBody(r).
Put(fmt.Sprintf("/batch/fileSets/%s/files/%s", fileSetId, url.PathEscape(fileName)))
if err != nil {
return err
}
if !resp.IsSuccess() {
return fmt.Errorf("failed to upload batch file, status code: %d", resp.StatusCode())
}
return nil
}