-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhostAPI.go
98 lines (85 loc) · 1.99 KB
/
hostAPI.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
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"encoding/json"
"fmt"
"image"
"image/jpeg"
"io/ioutil"
"net"
"net/http"
)
type hostAPIInterface interface {
getIp() string
Get(string) ([]byte, error)
GetFileList() ([]Image, error)
GetStatus() (map[string]interface{}, error)
GetJpeg(string) (image.Image, error)
Delete(string) error
}
type hostAPI struct {
Ip net.IP
Port int
}
func (host hostAPI) getIp() string {
return host.Ip.String()
}
func (host hostAPI) GetStatus() (map[string]interface{}, error) {
var status map[string]interface{}
result, err := host.Get("hello.json")
if err != nil {
return status, err
}
err = json.Unmarshal(result, &status)
if err != nil {
fmt.Println("Body is", err)
return status, err
}
return status, nil
}
func (host hostAPI) Get(path string) ([]byte, error) {
commandString := fmt.Sprintf("http://%s:%d/%s", host.Ip, host.Port, path)
r, err := myClient.Get(commandString)
if err != nil {
return nil, err
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
return body, err
}
func (host hostAPI) GetJpeg(path string) (image.Image, error) {
var file image.Image
commandString := fmt.Sprintf("http://%s:%d/scans/%s", host.Ip, host.Port, path)
r, err := myClient.Get(commandString)
if err != nil {
return file, err
}
defer r.Body.Close()
file, err = jpeg.Decode(r.Body)
return file, err
}
func (host hostAPI) GetFileList() ([]Image, error) {
var scans []Image
result, err := host.Get("scans.json")
if err != nil {
return scans, err
}
err = json.Unmarshal(result, &scans)
return scans, err
}
func (host hostAPI) Delete(scanName string) error {
var req *http.Request
// http client doesnt appear to implement DELETE requests directly
commandString := fmt.Sprintf("http://%s:%d/scans/%s", host.Ip, host.Port, scanName)
fmt.Println("1")
req, err := http.NewRequest("DELETE", commandString, nil)
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}