-
Notifications
You must be signed in to change notification settings - Fork 4
/
viewFiles.go
61 lines (52 loc) · 1.4 KB
/
viewFiles.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
type FileResponse struct {
Message string `json:"message"`
Data []FileItem `json:"data"`
}
type FileItem struct {
FileID string `json:"fileId"`
FileSize float64 `json:"fileSize"`
FileName string `json:"fileName"`
FileKey string `json:"fileKey"`
Urls int `json:"urls"`
CreatedAt string `json:"createdAt"`
}
func viewFiles() {
endpoint := fmt.Sprintf("%s/viewFiles", apiBaseURL)
client := &http.Client{}
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return
}
req.Header.Set("X-Jsmon-Key", strings.TrimSpace(getAPIKey()))
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to send request: %v", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response body: %v", err)
return
}
var response FileResponse
err = json.Unmarshal(body, &response)
if err != nil {
fmt.Printf("Failed to unmarshal JSON response: %v", err)
return
}
fmt.Println(response.Message)
for _, fileItem := range response.Data {
fmt.Printf("File Name: %s\nFile Size: %.3f MB\nFile ID: %s\nFile Key: %s\nNumber of URLs: %d\nCreated At: %s\n\n",
fileItem.FileName, fileItem.FileSize, fileItem.FileID, fileItem.FileKey, fileItem.Urls, fileItem.CreatedAt)
}
}