mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-05 18:03:41 +00:00
chore(api): code cleanup with generics
This commit is contained in:
parent
43c0885d0a
commit
109768f785
|
@ -11,10 +11,10 @@ import (
|
|||
"github.com/ncarlier/webhookd/pkg/strcase"
|
||||
)
|
||||
|
||||
// URLValuesToShellVars convert URL values to shell vars.
|
||||
func URLValuesToShellVars(q url.Values) []string {
|
||||
var params []string
|
||||
for k, v := range q {
|
||||
// HTTPParamsToShellVars convert URL values to shell vars.
|
||||
func HTTPParamsToShellVars[T url.Values | http.Header](params T) []string {
|
||||
var result []string
|
||||
for k, v := range params {
|
||||
var buf bytes.Buffer
|
||||
value, err := url.QueryUnescape(strings.Join(v[:], ","))
|
||||
if err != nil {
|
||||
|
@ -23,26 +23,9 @@ func URLValuesToShellVars(q url.Values) []string {
|
|||
buf.WriteString(strcase.ToSnake(k))
|
||||
buf.WriteString("=")
|
||||
buf.WriteString(value)
|
||||
params = append(params, buf.String())
|
||||
result = append(result, buf.String())
|
||||
}
|
||||
return params
|
||||
}
|
||||
|
||||
// HTTPHeadersToShellVars convert HTTP headers to shell vars.
|
||||
func HTTPHeadersToShellVars(h http.Header) []string {
|
||||
var params []string
|
||||
for k, v := range h {
|
||||
var buf bytes.Buffer
|
||||
value, err := url.QueryUnescape(strings.Join(v[:], ","))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
buf.WriteString(strcase.ToSnake(k))
|
||||
buf.WriteString("=")
|
||||
buf.WriteString(value)
|
||||
params = append(params, buf.String())
|
||||
}
|
||||
return params
|
||||
return result
|
||||
}
|
||||
|
||||
func nextRequestID() string {
|
||||
|
|
|
@ -90,8 +90,8 @@ func triggerWebhook(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
params := URLValuesToShellVars(r.Form)
|
||||
params = append(params, HTTPHeadersToShellVars(r.Header)...)
|
||||
params := HTTPParamsToShellVars(r.Form)
|
||||
params = append(params, HTTPParamsToShellVars(r.Header)...)
|
||||
|
||||
// logger.Debug.Printf("API REQUEST: \"%s\" with params %s...\n", p, params)
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ func TestQueryParamsToShellVars(t *testing.T) {
|
|||
"string": []string{"foo"},
|
||||
"list": []string{"foo", "bar"},
|
||||
}
|
||||
values := api.URLValuesToShellVars(tc)
|
||||
values := api.HTTPParamsToShellVars(tc)
|
||||
assert.ContainsStr(t, "string=foo", values, "")
|
||||
assert.ContainsStr(t, "list=foo,bar", values, "")
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ func TestHTTPHeadersToShellVars(t *testing.T) {
|
|||
"Content-Type": []string{"text/plain"},
|
||||
"X-Foo-Bar": []string{"foo", "bar"},
|
||||
}
|
||||
values := api.HTTPHeadersToShellVars(tc)
|
||||
values := api.HTTPParamsToShellVars(tc)
|
||||
assert.ContainsStr(t, "content_type=text/plain", values, "")
|
||||
assert.ContainsStr(t, "x_foo_bar=foo,bar", values, "")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user