-
Notifications
You must be signed in to change notification settings - Fork 0
/
connections.go
70 lines (53 loc) · 1.29 KB
/
connections.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
package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
)
func readResponse(resp *http.Response, err error) []byte {
if err != nil {
log.Println(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
}
return body
}
func connectWithHash(jarFingerprint int) []byte {
requestbody := []byte("[" + strconv.Itoa(jarFingerprint) + "]")
resp, err := http.Post(API+"fingerprint", "application/json", bytes.NewBuffer(requestbody))
return readResponse(resp, err)
}
func connectWithProjectID(projectID string) []byte {
resp, err := http.Get(API + "addon/" + projectID + "/files")
return readResponse(resp, err)
}
/*
Source: https://golangcode.com/download-a-file-from-a-url/
*/
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(fileName string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file within the folder
out, err := os.Create(path.Join(*downloadPath, fileName))
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}