-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathnet.go
201 lines (173 loc) · 4.79 KB
/
net.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package module
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/NuruProgramming/Nuru/object"
)
var NetFunctions = map[string]object.ModuleFunction{}
func init() {
NetFunctions["peruzi"] = getRequest
NetFunctions["tuma"] = postRequest
}
func getRequest(args []object.Object, defs map[string]object.Object) object.Object {
if len(defs) != 0 {
var url *object.String
var headers, params *object.Dict
for k, v := range defs {
switch k {
case "yuareli":
strUrl, ok := v.(*object.String)
if !ok {
return &object.Error{Message: "Yuareli iwe neno"}
}
url = strUrl
case "vichwa":
dictHead, ok := v.(*object.Dict)
if !ok {
return &object.Error{Message: "Vichwa lazima viwe kamusi"}
}
headers = dictHead
case "mwili":
dictHead, ok := v.(*object.Dict)
if !ok {
return &object.Error{Message: "Mwili lazima iwe kamusi"}
}
params = dictHead
default:
return &object.Error{Message: "Hoja si sahihi. Tumia yuareli na vichwa."}
}
}
if url.Value == "" {
return &object.Error{Message: "Yuareli ni lazima"}
}
var responseBody *bytes.Buffer
if params != nil {
booty := convertObjectToWhatever(params)
jsonBody, err := json.Marshal(booty)
if err != nil {
return &object.Error{Message: "Huku format query yako vizuri."}
}
responseBody = bytes.NewBuffer(jsonBody)
}
var req *http.Request
var err error
if responseBody != nil {
req, err = http.NewRequest("GET", url.Value, responseBody)
} else {
req, err = http.NewRequest("GET", url.Value, nil)
}
if err != nil {
return &object.Error{Message: "Tumeshindwa kufanya request"}
}
if headers != nil {
for _, val := range headers.Pairs {
req.Header.Set(val.Key.Inspect(), val.Value.Inspect())
}
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return &object.Error{Message: "Tumeshindwa kutuma request."}
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &object.Error{Message: "Tumeshindwa kusoma majibu."}
}
return &object.String{Value: string(respBody)}
}
if len(args) == 1 {
url, ok := args[0].(*object.String)
if !ok {
return &object.Error{Message: "Yuareli lazima iwe neno"}
}
req, err := http.NewRequest("GET", url.Value, nil)
if err != nil {
return &object.Error{Message: "Tumeshindwa kufanya request"}
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return &object.Error{Message: "Tumeshindwa kutuma request."}
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &object.Error{Message: "Tumeshindwa kusoma majibu."}
}
return &object.String{Value: string(respBody)}
}
return &object.Error{Message: "Hoja si sahihi. Tumia yuareli na vichwa."}
}
func postRequest(args []object.Object, defs map[string]object.Object) object.Object {
if len(defs) != 0 {
var url *object.String
var headers, params *object.Dict
for k, v := range defs {
switch k {
case "yuareli":
strUrl, ok := v.(*object.String)
if !ok {
return &object.Error{Message: "Yuareli iwe neno"}
}
url = strUrl
case "vichwa":
dictHead, ok := v.(*object.Dict)
if !ok {
return &object.Error{Message: "Vichwa lazima viwe kamusi"}
}
headers = dictHead
case "mwili":
dictHead, ok := v.(*object.Dict)
if !ok {
return &object.Error{Message: "Mwili lazima iwe kamusi"}
}
params = dictHead
default:
return &object.Error{Message: "Hoja si sahihi. Tumia yuareli na vichwa."}
}
}
if url.Value == "" {
return &object.Error{Message: "Yuareli ni lazima"}
}
var responseBody *bytes.Buffer
if params != nil {
booty := convertObjectToWhatever(params)
jsonBody, err := json.Marshal(booty)
if err != nil {
return &object.Error{Message: "Huku format query yako vizuri."}
}
responseBody = bytes.NewBuffer(jsonBody)
}
var req *http.Request
var err error
if responseBody != nil {
req, err = http.NewRequest("POST", url.Value, responseBody)
} else {
req, err = http.NewRequest("POST", url.Value, nil)
}
if err != nil {
return &object.Error{Message: "Tumeshindwa kufanya request"}
}
if headers != nil {
for _, val := range headers.Pairs {
req.Header.Set(val.Key.Inspect(), val.Value.Inspect())
}
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return &object.Error{Message: "Tumeshindwa kutuma request."}
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &object.Error{Message: "Tumeshindwa kusoma majibu."}
}
return &object.String{Value: string(respBody)}
}
return &object.Error{Message: "Hoja si sahihi. Tumia yuareli na vichwa."}
}