-
Notifications
You must be signed in to change notification settings - Fork 28
/
url.go
134 lines (116 loc) · 3.69 KB
/
url.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
package postman2
import (
"net/url"
"regexp"
"strings"
"github.com/grokify/mogo/net/urlutil"
)
// URL is the Postman URL used in the Postman 2.0 Collection Spec.
type URL struct {
Raw string `json:"raw,omitempty"`
Protocol string `json:"protocol,omitempty"`
Auth map[string]string `json:"auth,omitempty"`
Host []string `json:"host,omitempty"`
Path []string `json:"path,omitempty"`
Query []URLQuery `json:"query,omitempty"`
Variable []URLVariable `json:"variable,omitempty"`
}
// URLParameters is a temp struct to hold parsed parameters.
type URLParameters struct {
Query []URLQuery `json:"query,omitempty"`
Variable []URLVariable `json:"variable,omitempty"`
}
// NewURLParameters returns an initialized empty struct.
func NewURLParameters() URLParameters {
return URLParameters{
Query: []URLQuery{},
Variable: []URLVariable{},
}
}
func (pmURL *URL) IsRawOnly() bool {
pmURL.Protocol = strings.TrimSpace(pmURL.Protocol)
if len(pmURL.Protocol) > 0 ||
len(pmURL.Host) > 0 ||
len(pmURL.Path) > 0 {
return false
}
return true
}
type URLQuery struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
Description string `json:"description,omitempty"`
Disabled bool `json:"disabled,omitempty"`
}
type URLVariable struct {
Key string `json:"key,omitempty"`
Value interface{} `json:"value,omitempty"`
Description URLVariableDescription `json:"description,omitempty"`
Disabled bool `json:"disabled,omitempty"`
ID string `json:"id,omitempty"` // Old, pre 2.0.1
}
type URLVariableDescription struct {
Content string `json:"content,omitempty"`
Type string `json:"type,omitempty"`
}
func NewURLForGoURL(goURL url.URL) URL {
pmURL := URL{Variable: []URLVariable{}}
goURL.Scheme = strings.TrimSpace(goURL.Scheme)
goURL.Host = strings.TrimSpace(goURL.Host)
goURL.Path = strings.TrimSpace(goURL.Path)
urlParts := []string{}
if len(goURL.Host) > 0 {
pmURL.Host = strings.Split(goURL.Host, ".")
urlParts = append(urlParts, goURL.Host)
}
if len(goURL.Path) > 0 {
pmURL.Path = strings.Split(goURL.Path, "/")
urlParts = append(urlParts, goURL.Path)
}
rawURL := strings.Join(urlParts, "/")
if len(goURL.Scheme) > 0 {
pmURL.Protocol = goURL.Scheme
rawURL = goURL.Scheme + "://" + rawURL
}
pmURL.Raw = rawURL
return pmURL
}
var simpleURLRx = regexp.MustCompile(`^([a-z][0-9a-z]+)://([^/]+)/(.*)$`)
func NewURL(rawURL string) URL {
rawURL = strings.TrimSpace(rawURL)
pmURL := URL{Raw: rawURL, Variable: []URLVariable{}}
rs1 := simpleURLRx.FindAllStringSubmatch(rawURL, -1)
if len(rs1) > 0 {
for _, m := range rs1 {
pmURL.Protocol = m[1]
hostname := m[2]
path := m[3]
pmURL.Host = strings.Split(hostname, ".")
pmURL.Path = urlutil.SplitPath(path, true, true)
}
} else if strings.Index(rawURL, "{") == 0 {
parts := urlutil.SplitPath(rawURL, true, true)
if len(parts) > 0 {
pmURL.Host = []string{parts[0]}
}
if len(parts) > 1 {
pmURL.Path = parts[1:]
}
}
return pmURL
}
// AddVariable adds a Postman Variable to the struct.
func (pmURL *URL) AddVariable(key string, value interface{}) {
variable := URLVariable{ID: key, Value: value}
pmURL.Variable = append(pmURL.Variable, variable)
}
const (
apiURLOasToPostmanVarMatch string = `(^|[^\{])\{([^\/\{\}]+)\}([^\}]|$)`
apiURLOasToPostmanVarReplace string = "$1:$2$3"
)
var apiURLOasToPostmanVarMatchRx = regexp.MustCompile(
apiURLOasToPostmanVarMatch)
func APIURLOasToPostman(urlWithOasVars string) string {
return apiURLOasToPostmanVarMatchRx.ReplaceAllString(
urlWithOasVars, apiURLOasToPostmanVarReplace)
}