Skip to content

Commit d961c1a

Browse files
authored
CLOUDP-209888: Remove temp file creation (#233)
1 parent d42cace commit d961c1a

File tree

13 files changed

+62
-92
lines changed

13 files changed

+62
-92
lines changed

admin/api_data_federation.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11-
"os"
1211
"strings"
1312
)
1413

@@ -198,7 +197,7 @@ type DataFederationApi interface {
198197
DownloadFederatedDatabaseQueryLogsWithParams(ctx context.Context, args *DownloadFederatedDatabaseQueryLogsApiParams) DownloadFederatedDatabaseQueryLogsApiRequest
199198

200199
// Interface only available internally
201-
downloadFederatedDatabaseQueryLogsExecute(r DownloadFederatedDatabaseQueryLogsApiRequest) (*os.File, *http.Response, error)
200+
downloadFederatedDatabaseQueryLogsExecute(r DownloadFederatedDatabaseQueryLogsApiRequest) (io.ReadCloser, *http.Response, error)
202201

203202
/*
204203
GetDataFederationPrivateEndpoint Return One Federated Database Instance and Online Archive Private Endpoint in One Project
@@ -1293,7 +1292,7 @@ func (r DownloadFederatedDatabaseQueryLogsApiRequest) StartDate(startDate int64)
12931292
return r
12941293
}
12951294

1296-
func (r DownloadFederatedDatabaseQueryLogsApiRequest) Execute() (*os.File, *http.Response, error) {
1295+
func (r DownloadFederatedDatabaseQueryLogsApiRequest) Execute() (io.ReadCloser, *http.Response, error) {
12971296
return r.ApiService.downloadFederatedDatabaseQueryLogsExecute(r)
12981297
}
12991298

@@ -1318,13 +1317,13 @@ func (a *DataFederationApiService) DownloadFederatedDatabaseQueryLogs(ctx contex
13181317

13191318
// Execute executes the request
13201319
//
1321-
// @return *os.File
1322-
func (a *DataFederationApiService) downloadFederatedDatabaseQueryLogsExecute(r DownloadFederatedDatabaseQueryLogsApiRequest) (*os.File, *http.Response, error) {
1320+
// @return io.ReadCloser
1321+
func (a *DataFederationApiService) downloadFederatedDatabaseQueryLogsExecute(r DownloadFederatedDatabaseQueryLogsApiRequest) (io.ReadCloser, *http.Response, error) {
13231322
var (
13241323
localVarHTTPMethod = http.MethodGet
13251324
localVarPostBody interface{}
13261325
formFiles []formFile
1327-
localVarReturnValue *os.File
1326+
localVarReturnValue io.ReadCloser
13281327
)
13291328

13301329
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "DataFederationApiService.DownloadFederatedDatabaseQueryLogs")

admin/api_monitoring_and_logs.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11-
"os"
1211
"reflect"
1312
"strings"
1413
"time"
@@ -144,7 +143,7 @@ type MonitoringAndLogsApi interface {
144143
GetHostLogsWithParams(ctx context.Context, args *GetHostLogsApiParams) GetHostLogsApiRequest
145144

146145
// Interface only available internally
147-
getHostLogsExecute(r GetHostLogsApiRequest) (*os.File, *http.Response, error)
146+
getHostLogsExecute(r GetHostLogsApiRequest) (io.ReadCloser, *http.Response, error)
148147

149148
/*
150149
GetHostMeasurements Return Measurements for One MongoDB Process
@@ -1118,7 +1117,7 @@ func (r GetHostLogsApiRequest) StartDate(startDate int64) GetHostLogsApiRequest
11181117
return r
11191118
}
11201119

1121-
func (r GetHostLogsApiRequest) Execute() (*os.File, *http.Response, error) {
1120+
func (r GetHostLogsApiRequest) Execute() (io.ReadCloser, *http.Response, error) {
11221121
return r.ApiService.getHostLogsExecute(r)
11231122
}
11241123

@@ -1145,13 +1144,13 @@ func (a *MonitoringAndLogsApiService) GetHostLogs(ctx context.Context, groupId s
11451144

11461145
// Execute executes the request
11471146
//
1148-
// @return *os.File
1149-
func (a *MonitoringAndLogsApiService) getHostLogsExecute(r GetHostLogsApiRequest) (*os.File, *http.Response, error) {
1147+
// @return io.ReadCloser
1148+
func (a *MonitoringAndLogsApiService) getHostLogsExecute(r GetHostLogsApiRequest) (io.ReadCloser, *http.Response, error) {
11501149
var (
11511150
localVarHTTPMethod = http.MethodGet
11521151
localVarPostBody interface{}
11531152
formFiles []formFile
1154-
localVarReturnValue *os.File
1153+
localVarReturnValue io.ReadCloser
11551154
)
11561155

11571156
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "MonitoringAndLogsApiService.GetHostLogs")

admin/api_online_archive.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"io"
99
"net/http"
1010
"net/url"
11-
"os"
1211
"strings"
1312
)
1413

@@ -85,7 +84,7 @@ type OnlineArchiveApi interface {
8584
DownloadOnlineArchiveQueryLogsWithParams(ctx context.Context, args *DownloadOnlineArchiveQueryLogsApiParams) DownloadOnlineArchiveQueryLogsApiRequest
8685

8786
// Interface only available internally
88-
downloadOnlineArchiveQueryLogsExecute(r DownloadOnlineArchiveQueryLogsApiRequest) (*os.File, *http.Response, error)
87+
downloadOnlineArchiveQueryLogsExecute(r DownloadOnlineArchiveQueryLogsApiRequest) (io.ReadCloser, *http.Response, error)
8988

9089
/*
9190
GetOnlineArchive Return One Online Archive
@@ -518,7 +517,7 @@ func (r DownloadOnlineArchiveQueryLogsApiRequest) ArchiveOnly(archiveOnly bool)
518517
return r
519518
}
520519

521-
func (r DownloadOnlineArchiveQueryLogsApiRequest) Execute() (*os.File, *http.Response, error) {
520+
func (r DownloadOnlineArchiveQueryLogsApiRequest) Execute() (io.ReadCloser, *http.Response, error) {
522521
return r.ApiService.downloadOnlineArchiveQueryLogsExecute(r)
523522
}
524523

@@ -543,13 +542,13 @@ func (a *OnlineArchiveApiService) DownloadOnlineArchiveQueryLogs(ctx context.Con
543542

544543
// Execute executes the request
545544
//
546-
// @return *os.File
547-
func (a *OnlineArchiveApiService) downloadOnlineArchiveQueryLogsExecute(r DownloadOnlineArchiveQueryLogsApiRequest) (*os.File, *http.Response, error) {
545+
// @return io.ReadCloser
546+
func (a *OnlineArchiveApiService) downloadOnlineArchiveQueryLogsExecute(r DownloadOnlineArchiveQueryLogsApiRequest) (io.ReadCloser, *http.Response, error) {
548547
var (
549548
localVarHTTPMethod = http.MethodGet
550549
localVarPostBody interface{}
551550
formFiles []formFile
552-
localVarReturnValue *os.File
551+
localVarReturnValue io.ReadCloser
553552
)
554553

555554
localBasePath, err := a.client.cfg.ServerURLWithContext(r.ctx, "OnlineArchiveApiService.DownloadOnlineArchiveQueryLogs")

admin/client.go

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -514,36 +514,15 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err
514514
*s = string(b)
515515
return nil
516516
}
517-
if _, ok := v.(*os.File); ok {
518-
f, err1 := os.CreateTemp("", "HttpClientFile")
519-
if err1 != nil {
520-
if c.cfg.Debug {
521-
log.Printf("\n%s\n", err1)
522-
}
523-
return
524-
}
525-
_, err1 = f.Write(b)
526-
if err != nil {
527-
if c.cfg.Debug {
528-
log.Printf("\n%s\n", err1)
529-
}
530-
return
531-
}
532-
533-
_, _ = f.Seek(0, io.SeekStart)
534-
return
517+
if r, ok := v.(*io.ReadCloser); ok {
518+
reader := io.NopCloser(bytes.NewReader(b))
519+
*r = reader
520+
return nil
535521
}
536-
if f, ok := v.(**os.File); ok {
537-
*f, err = os.CreateTemp("", "HttpClientFile")
538-
if err != nil {
539-
return
540-
}
541-
_, err = (*f).Write(b)
542-
if err != nil {
543-
return
544-
}
545-
_, err = (*f).Seek(0, io.SeekStart)
546-
return
522+
if r, ok := v.(**io.ReadCloser); ok {
523+
reader := io.NopCloser(bytes.NewReader(b))
524+
*r = &reader
525+
return nil
547526
}
548527
if xmlCheck.MatchString(contentType) {
549528
return xml.Unmarshal(b, v)
@@ -598,8 +577,8 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
598577

599578
if reader, ok := body.(io.Reader); ok {
600579
_, err = bodyBuf.ReadFrom(reader)
601-
} else if fp, ok := body.(*os.File); ok {
602-
_, err = bodyBuf.ReadFrom(fp)
580+
} else if fp, ok := body.(*io.ReadCloser); ok {
581+
_, err = bodyBuf.ReadFrom(*fp)
603582
} else if b, ok := body.([]byte); ok {
604583
_, err = bodyBuf.Write(b)
605584
} else if s, ok := body.(string); ok {

docs/doc_5_best_practices.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ When you have confirmed that the `time.Time pointer` is non-nil, you can safely
5454
}
5555
```
5656

57+
## Working with Binary Responses
58+
59+
In the Atlas Go SDK, the `io.ReadCloser` type is used to return binary data from APIs.
60+
61+
1. Use `io.Copy` if the intention is to store on a file or pass through another steam
62+
63+
2. Use `io.ReadAll` if the intention is to read all bytes in memory
64+
65+
3. Call the `.Close()` function after done reading the data
66+
5767
## Use Method for Creating Models
5868

5969
Use dedicated methods for creating new models.

docs/docs/DataFederationApi.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ Name | Type | Description | Notes
487487

488488
## DownloadFederatedDatabaseQueryLogs
489489

490-
> *os.File DownloadFederatedDatabaseQueryLogs(ctx, groupId, tenantName).EndDate(endDate).StartDate(startDate).Execute()
490+
> io.ReadCloser DownloadFederatedDatabaseQueryLogs(ctx, groupId, tenantName).EndDate(endDate).StartDate(startDate).Execute()
491491
492492
Download Query Logs for One Federated Database Instance
493493

@@ -522,7 +522,7 @@ func main() {
522522
apiError := admin.AsError(err)
523523
fmt.Fprintf(os.Stderr, "Error obj: %v\n", apiError)
524524
}
525-
// response from `DownloadFederatedDatabaseQueryLogs`: *os.File
525+
// response from `DownloadFederatedDatabaseQueryLogs`: io.ReadCloser
526526
fmt.Fprintf(os.Stdout, "Response from `DataFederationApi.DownloadFederatedDatabaseQueryLogs`: %v\n", resp)
527527
}
528528
```
@@ -550,7 +550,7 @@ Name | Type | Description | Notes
550550

551551
### Return type
552552

553-
[***os.File**](*os.File.md)
553+
[**io.ReadCloser**](io.ReadCloser.md)
554554

555555
### Authorization
556556
[DigestAuth](../README.md#Authentication)

docs/docs/MonitoringAndLogsApi.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ Name | Type | Description | Notes
361361

362362
## GetHostLogs
363363

364-
> *os.File GetHostLogs(ctx, groupId, hostName, logName).EndDate(endDate).StartDate(startDate).Execute()
364+
> io.ReadCloser GetHostLogs(ctx, groupId, hostName, logName).EndDate(endDate).StartDate(startDate).Execute()
365365
366366
Download Logs for One Cluster Host in One Project
367367

@@ -397,7 +397,7 @@ func main() {
397397
apiError := admin.AsError(err)
398398
fmt.Fprintf(os.Stderr, "Error obj: %v\n", apiError)
399399
}
400-
// response from `GetHostLogs`: *os.File
400+
// response from `GetHostLogs`: io.ReadCloser
401401
fmt.Fprintf(os.Stdout, "Response from `MonitoringAndLogsApi.GetHostLogs`: %v\n", resp)
402402
}
403403
```
@@ -427,7 +427,7 @@ Name | Type | Description | Notes
427427

428428
### Return type
429429

430-
[***os.File**](*os.File.md)
430+
[**io.ReadCloser**](io.ReadCloser.md)
431431

432432
### Authorization
433433
[DigestAuth](../README.md#Authentication)

docs/docs/OnlineArchiveApi.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ Name | Type | Description | Notes
172172

173173
## DownloadOnlineArchiveQueryLogs
174174

175-
> *os.File DownloadOnlineArchiveQueryLogs(ctx, groupId, clusterName).StartDate(startDate).EndDate(endDate).ArchiveOnly(archiveOnly).Execute()
175+
> io.ReadCloser DownloadOnlineArchiveQueryLogs(ctx, groupId, clusterName).StartDate(startDate).EndDate(endDate).ArchiveOnly(archiveOnly).Execute()
176176
177177
Download Online Archive Query Logs
178178

@@ -213,7 +213,7 @@ func main() {
213213
apiError := admin.AsError(err)
214214
fmt.Fprintf(os.Stderr, "Error obj: %v\n", apiError)
215215
}
216-
// response from `DownloadOnlineArchiveQueryLogs`: *os.File
216+
// response from `DownloadOnlineArchiveQueryLogs`: io.ReadCloser
217217
fmt.Fprintf(os.Stdout, "Response from `OnlineArchiveApi.DownloadOnlineArchiveQueryLogs`: %v\n", resp)
218218
}
219219
```
@@ -242,7 +242,7 @@ Name | Type | Description | Notes
242242

243243
### Return type
244244

245-
[***os.File**](*os.File.md)
245+
[**io.ReadCloser**](io.ReadCloser.md)
246246

247247
### Authorization
248248
[DigestAuth](../README.md#Authentication)

examples/download/downloadLogs.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
65
"log"
76
"os"
@@ -41,7 +40,7 @@ func main() {
4140
if projects.GetTotalCount() == 0 {
4241
log.Fatal("account should have at least single project")
4342
}
44-
var out io.Writer
43+
var out io.Writer = os.Stdout
4544
projectId := projects.GetResults()[0].GetId()
4645

4746
// -- 2. Get first Process
@@ -59,6 +58,9 @@ func main() {
5958

6059
logs, response, err := sdk.MonitoringAndLogsApi.GetHostLogsWithParams(ctx, params).Execute()
6160
examples.HandleErr(err, response)
61+
defer func() {
62+
_ = logs.Close()
63+
}()
6264
_, err = io.Copy(out, logs)
63-
fmt.Println(err)
65+
examples.HandleErr(err, nil)
6466
}

tools/config/go-templates/client.mustache

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -481,36 +481,15 @@ func (c *APIClient) decode(v interface{}, b []byte, contentType string) (err err
481481
*s = string(b)
482482
return nil
483483
}
484-
if _, ok := v.(*os.File); ok {
485-
f, err1 := os.CreateTemp("", "HttpClientFile")
486-
if err1 != nil {
487-
if c.cfg.Debug {
488-
log.Printf("\n%s\n", err1)
489-
}
490-
return
491-
}
492-
_, err1 = f.Write(b)
493-
if err != nil {
494-
if c.cfg.Debug {
495-
log.Printf("\n%s\n", err1)
496-
}
497-
return
498-
}
499-
500-
_, _ = f.Seek(0, io.SeekStart)
501-
return
484+
if r, ok := v.(*io.ReadCloser); ok {
485+
reader := io.NopCloser(bytes.NewReader(b))
486+
*r = reader
487+
return nil
502488
}
503-
if f, ok := v.(**os.File); ok {
504-
*f, err = os.CreateTemp("", "HttpClientFile")
505-
if err != nil {
506-
return
507-
}
508-
_, err = (*f).Write(b)
509-
if err != nil {
510-
return
511-
}
512-
_, err = (*f).Seek(0, io.SeekStart)
513-
return
489+
if r, ok := v.(**io.ReadCloser); ok {
490+
reader := io.NopCloser(bytes.NewReader(b))
491+
*r = &reader
492+
return nil
514493
}
515494
if xmlCheck.MatchString(contentType) {
516495
return xml.Unmarshal(b, v);
@@ -565,8 +544,8 @@ func setBody(body interface{}, contentType string) (bodyBuf *bytes.Buffer, err e
565544
566545
if reader, ok := body.(io.Reader); ok {
567546
_, err = bodyBuf.ReadFrom(reader)
568-
} else if fp, ok := body.(*os.File); ok {
569-
_, err = bodyBuf.ReadFrom(fp)
547+
} else if fp, ok := body.(*io.ReadCloser); ok {
548+
_, err = bodyBuf.ReadFrom(*fp)
570549
} else if b, ok := body.([]byte); ok {
571550
_, err = bodyBuf.Write(b)
572551
} else if s, ok := body.(string); ok {

0 commit comments

Comments
 (0)