-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
68 lines (64 loc) · 1.43 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/gustavohmsilva/Praticando-Go/Aula_XIV/client/config"
"github.com/gustavohmsilva/Praticando-Go/Aula_XIV/client/dataparser"
"github.com/gustavohmsilva/Praticando-Go/Aula_XIV/client/uploader"
)
func main() {
conf, err := config.Load()
if err != nil {
panic(err)
}
f, err := dataparser.Read(conf.GetString("filename"))
if err != nil {
panic(err)
}
ps, err := dataparser.CSV(f, true)
if err != nil {
panic(err)
}
jsonPs, err := json.Marshal(ps)
if err != nil {
panic(err)
}
response, err := http.Post(
conf.GetString("endpoint"),
"application/json",
bytes.NewBuffer(jsonPs),
)
status := response.StatusCode
fmt.Println(status, response.Body)
path := fmt.Sprintf("%s/", conf.GetString("images_path"))
var files []uploader.UplRequestSrc
for _, p := range ps {
fileToUpload, err := uploader.NewUplRequestSrc(
conf.GetString("image_endpoint"),
"file",
path,
p.Image,
)
if err != nil {
fileToUpload.Status = err
files = append(files, fileToUpload)
continue
}
req, err := uploader.NewFileReq(fileToUpload)
if err != nil {
fileToUpload.Status = err
files = append(files, fileToUpload)
continue
}
res, err := http.DefaultClient.Do(req)
if err != nil {
fileToUpload.Status = err
files = append(files, fileToUpload)
fmt.Printf(err.Error())
continue
}
fmt.Println(res.StatusCode, res.Body)
}
}