Repository navigation
Expand file tree
/
Copy pathprotocol.go
More file actions
124 lines (106 loc) · 2.65 KB
/
Copy pathprotocol.go
File metadata and controls
124 lines (106 loc) · 2.65 KB
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
package diecast
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/PerformLine/go-stockutil/maputil"
"github.com/PerformLine/go-stockutil/typeutil"
)
type Protocol interface {
Retrieve(*ProtocolRequest) (*ProtocolResponse, error)
}
type ProtocolConfig map[string]interface{}
func (self ProtocolConfig) Get(key string, fallbacks ...interface{}) typeutil.Variant {
var v = maputil.M(self).Get(key)
if v.IsNil() {
if len(fallbacks) > 0 {
return typeutil.V(fallbacks[0])
} else {
return typeutil.V(nil)
}
} else {
return v
}
}
type ProtocolRequest struct {
Verb string
URL *url.URL
Binding *Binding
Request *http.Request
Header *TemplateHeader
TemplateData map[string]interface{}
TemplateFuncs FuncMap
DefaultTimeout time.Duration
AdditionalHeaders map[string]interface{}
}
func (self *ProtocolRequest) ReadFile(filename string) ([]byte, error) {
if b := self.Binding; b != nil {
if s := b.server; s != nil {
return readFromFS(s.fs, filename)
}
}
return nil, fmt.Errorf("no such file or directory")
}
func (self *ProtocolRequest) Template(input interface{}) (typeutil.Variant, error) {
// only do template evaluation if the input is a string that contains "{{" and "}}"
if vS := typeutil.String(input); strings.Contains(vS, `{{`) && strings.Contains(vS, `}}`) {
if len(self.TemplateFuncs) > 0 {
if v, err := EvalInline(vS, self.TemplateData, self.TemplateFuncs); err == nil {
return typeutil.V(v), nil
} else {
return typeutil.V(nil), err
}
}
}
return typeutil.V(input), nil
}
func (self *ProtocolRequest) Conf(proto string, key string, fallbacks ...interface{}) typeutil.Variant {
if self.Binding != nil {
if self.Binding.server != nil {
if len(self.Binding.server.Protocols) > 0 {
if cnf, ok := self.Binding.server.Protocols[proto]; ok {
return cnf.Get(key, fallbacks...)
}
}
}
}
if len(fallbacks) > 0 {
return typeutil.V(fallbacks[0])
} else {
return typeutil.V(nil)
}
}
type ProtocolResponse struct {
MimeType string
StatusCode int
Raw interface{}
data io.ReadCloser
}
func (self *ProtocolResponse) PeekLen() (int64, error) {
var buf = bytes.NewBuffer(nil)
if n, err := io.Copy(buf, self.data); err == nil {
self.data = ioutil.NopCloser(buf)
return n, nil
} else {
return 0, err
}
}
func (self *ProtocolResponse) Read(b []byte) (int, error) {
if self.data == nil {
return 0, io.EOF
} else {
return self.data.Read(b)
}
}
func (self *ProtocolResponse) Close() error {
if self.data == nil {
return nil
} else {
return self.data.Close()
}
}