-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
87 lines (75 loc) · 1.98 KB
/
example_test.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
// example_test.go
//
// Copyright (c) 2018-2023 Junpei Kawamoto
//
// This software is released under the MIT License.
//
// http://opensource.org/licenses/mit-license.php
package pixeldrain_test
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"github.com/go-openapi/runtime/client"
"github.com/go-openapi/swag"
"github.com/jkawamoto/go-pixeldrain"
"github.com/jkawamoto/go-pixeldrain/client/file"
)
func Example_upload() {
// Open the target file.
f, err := os.Open("example_test.go")
if err != nil {
log.Fatal(err)
}
defer func() {
if err := f.Close(); err != nil {
log.Fatal(err)
}
}()
// Upload the file with the default client. API key can be empty.
res, err := pixeldrain.Default.File.UploadFile(
file.NewUploadFileParamsWithContext(context.Background()).WithFile(f),
client.BasicAuth("", "YOUR API KEY IF NECESSARY"),
)
if err != nil {
log.Fatal(err)
}
// File ID is used to download the file by this client.
fmt.Println("File ID:", swag.StringValue(res.Payload.ID))
// Download URL is for browsers, wget, curl, etc.
fmt.Println("Download URL:", pixeldrain.DownloadURL(swag.StringValue(res.Payload.ID)))
}
func Example_download() {
ctx := context.Background()
id := "FILE_ID"
apiKey := "YOUR API KEY IF NECESSARY"
// Get the file information.
info, err := pixeldrain.Default.File.GetFileInfo(
file.NewGetFileInfoParamsWithContext(ctx).WithID(id),
client.BasicAuth("", apiKey),
)
if err != nil {
log.Fatal(err)
}
// Open a file to store the downloaded contents.
f, err := os.OpenFile(filepath.Join("~/Downloads", info.Payload.Name), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer func() {
if err := f.Close(); err != nil {
log.Fatal(err)
}
}()
// If a directory path is given, the downloaded file will be stored in the directory.
_, err = pixeldrain.Default.File.DownloadFile(
file.NewDownloadFileParamsWithContext(ctx).WithID(id),
client.BasicAuth("", apiKey),
f,
)
if err != nil {
log.Fatal(err)
}
}