forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmw_method_transform.go
54 lines (48 loc) · 1.3 KB
/
mw_method_transform.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
package main
import (
"errors"
"net/http"
"strings"
"github.com/TykTechnologies/tyk/apidef"
)
// TransformMiddleware is a middleware that will apply a template to a request body to transform it's contents ready for an upstream API
type TransformMethod struct {
BaseMiddleware
}
func (t *TransformMethod) Name() string {
return "TransformMethod"
}
func (t *TransformMethod) EnabledForSpec() bool {
for _, version := range t.Spec.VersionData.Versions {
if len(version.ExtendedPaths.MethodTransforms) > 0 {
return true
}
}
return false
}
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (t *TransformMethod) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
_, versionPaths, _, _ := t.Spec.Version(r)
found, meta := t.Spec.CheckSpecMatchesStatus(r, versionPaths, MethodTransformed)
if !found {
return nil, 200
}
mmeta := meta.(*apidef.MethodTransformMeta)
switch strings.ToUpper(mmeta.ToMethod) {
case "GET":
r.Method = "GET"
case "POST":
r.Method = "POST"
case "PUT":
r.Method = "PUT"
case "DELETE":
r.Method = "DELETE"
case "OPTIONS":
r.Method = "OPTIONS"
case "PATCH":
r.Method = "PATCH"
default:
return errors.New("Method not allowed"), 405
}
return nil, 200
}