-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhttp_client.go
151 lines (123 loc) · 3.16 KB
/
http_client.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package fanuc
import (
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"time"
)
var (
ErrForbidden = errForbidden()
ErrUnauthorized = errUnauthorized()
)
func errForbidden() error {
return errors.New("Forbidden. Please unlock KAREL via Setup > Host Comm > HTTP.")
}
func errUnauthorized() error {
return errors.New("Unauthorized. Please unlock KAREL via Setup > Host Comm > HTTP.")
}
type HTTPClient struct {
client *http.Client
baseURL *url.URL
base *ParseClient
}
const (
// units are in seconds
DefaultClientTimeout = 5
DefaultDialTimeout = 1
DefaultHandshakeTimeout = 1
DefaultKeepAlive = 30
)
func NewHTTPClient(s string) (*HTTPClient, error) {
if ip := net.ParseIP(s); ip != nil {
s = "http://" + s
}
baseURL, err := url.ParseRequestURI(s)
if err != nil {
return nil, err
}
c := &HTTPClient{
baseURL: baseURL,
client: &http.Client{
Timeout: DefaultClientTimeout * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: DefaultDialTimeout * time.Second,
KeepAlive: DefaultKeepAlive * time.Second,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: DefaultHandshakeTimeout * time.Second,
},
},
}
c.base = &ParseClient{GetFunc: c.get}
return c, nil
}
func (c *HTTPClient) SetTimeout(t time.Duration) {
c.client.Timeout = t
}
func (c *HTTPClient) get(dev device, path string) (string, error) {
url, err := c.baseURL.Parse(dev.String() + "/" + path)
if err != nil {
return "", err
}
params := url.Query()
params.Add("_TEMPLATE", "")
url.RawQuery = params.Encode()
s := strings.ReplaceAll(url.String(), "+", "%20") // FANUC likes %20 over + for spaces
resp, err := c.client.Get(s)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusForbidden:
return "", ErrForbidden
case http.StatusUnauthorized:
return "", ErrUnauthorized
default:
return "", fmt.Errorf("Request failed: %q (%d)", s, resp.StatusCode)
}
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}
func (c *HTTPClient) Errors() ([]Error, error) {
return c.base.Errors()
}
func (c *HTTPClient) Frames() ([]Frame, error) {
return c.base.Frames()
}
func (c *HTTPClient) IO(types ...Type) ([]IO, error) {
return c.base.IO(types...)
}
func (c *HTTPClient) NumericRegisters() ([]NumericRegister, error) {
return c.base.NumericRegisters()
}
func (c *HTTPClient) PositionRegisters() ([]PositionRegister, error) {
return c.base.PositionRegisters()
}
func (c *HTTPClient) TPPrograms() ([]string, error) {
return c.base.TPPrograms()
}
func (c *HTTPClient) TPPositions(programName string) ([]Position, error) {
return c.base.TPPositions(programName)
}
func (c *HTTPClient) SetComment(t Type, id int, comment string) error {
fc := commentCodeFor(t)
if fc == invalid {
return fmt.Errorf("cannot set comment for %s", t.VerboseName())
}
rel := fmt.Sprintf("ComSet?sFc=%d&sIndx=%d&sComment=%s", fc, id, comment)
_, err := c.get(KAREL, rel)
return err
}