/
mutate.go
130 lines (112 loc) · 3.01 KB
/
mutate.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
package http_ns
import (
"errors"
"fmt"
"io"
"strings"
"github.com/inoxlang/inox/internal/commonfmt"
"github.com/inoxlang/inox/internal/core"
)
func IsMutationMethod(method string) bool {
switch strings.ToUpper(method) {
case "POST", "PATCH", "PUT", "DELETE":
return true
}
return false
}
func HttpPost(ctx *core.Context, args ...core.Value) (*Response, error) {
return _httpPostPatch(ctx, false, args...)
}
func HttpPatch(ctx *core.Context, args ...core.Value) (*Response, error) {
return _httpPostPatch(ctx, true, args...)
}
func _httpPostPatch(ctx *core.Context, isPatch bool, args ...core.Value) (*Response, error) {
var contentType core.Mimetype
var u core.URL
var body io.Reader
var requestOptionArgs []core.Value
for _, arg := range args {
switch argVal := arg.(type) {
case core.URL:
if u != "" {
return nil, commonfmt.FmtErrArgumentProvidedAtLeastTwice("url")
}
u = argVal
case core.Mimetype:
if contentType != "" {
return nil, commonfmt.FmtErrArgumentProvidedAtLeastTwice("mime type")
}
contentType = argVal
case core.Readable:
if body != nil {
return nil, commonfmt.FmtErrArgumentProvidedAtLeastTwice("body")
}
body = argVal.Reader()
case *core.List:
if body != nil {
return nil, commonfmt.FmtErrArgumentProvidedAtLeastTwice("body")
}
jsonString := core.ToJSON(ctx, argVal, nil)
body = strings.NewReader(string(jsonString))
case *core.Object:
if body == nil {
jsonString := core.ToJSON(ctx, argVal, nil)
body = strings.NewReader(string(jsonString))
} else {
return nil, commonfmt.FmtErrArgumentProvidedAtLeastTwice("body")
}
case core.Option, core.QuantityRange:
requestOptionArgs = append(requestOptionArgs, argVal)
default:
return nil, fmt.Errorf("only an URL argument is expected, not a(n) %T ", arg)
}
}
//checks
if u == "" {
return nil, errors.New(MISSING_URL_ARG)
}
client, opts, err := getClientAndOptions(ctx, u, requestOptionArgs...)
if err != nil {
return nil, err
}
//
method := "POST"
if isPatch {
method = "PATCH"
}
req, err := client.MakeRequest(ctx, method, u, body, string(contentType), opts)
if err != nil {
return nil, err
}
return client.DoRequest(ctx, req)
}
func HttpDelete(ctx *core.Context, args ...core.Value) (*Response, error) {
var u core.URL
var requestOptionArgs []core.Value
for _, arg := range args {
switch argVal := arg.(type) {
case core.URL:
if u != "" {
return nil, commonfmt.FmtErrArgumentProvidedAtLeastTwice("url")
}
u = argVal
case core.Option, core.QuantityRange:
requestOptionArgs = append(requestOptionArgs, argVal)
default:
return nil, fmt.Errorf("only an core.URL argument is expected, not a(n) %T ", arg)
}
}
//checks
if u == "" {
return nil, errors.New(MISSING_URL_ARG)
}
client, opts, err := getClientAndOptions(ctx, u, requestOptionArgs...)
if err != nil {
return nil, err
}
req, err := client.MakeRequest(ctx, "DELETE", u, nil, "", opts)
if err != nil {
return nil, err
}
return client.DoRequest(ctx, req)
}