-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (117 loc) · 3.01 KB
/
main.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
package requestHandler
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
"github.com/labstack/echo"
)
// TokenType type of token
type TokenType int8
// const of token type
const (
Basic = iota
Bearer
)
func (s TokenType) String() string {
switch s {
case Basic:
return "Basic"
case Bearer:
return "Bearer"
default:
return "Bearer"
}
return "Bearer"
}
// RequestModel model
type RequestModel struct {
URL string
TokenType TokenType
Token string
Username string
Password string
Body string
Type string
}
// Base request
func Base(requestModel RequestModel, result *interface{}) error {
body := bytes.NewBufferString(requestModel.Body)
req, err := http.NewRequest(requestModel.Type, requestModel.URL, body)
switch requestModel.TokenType {
case Basic:
req.SetBasicAuth(requestModel.Username, requestModel.Password)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
case Bearer:
header := fmt.Sprintf("Bearer %s", requestModel.Token)
req.Header.Set("Authorization", header)
req.Header.Add("Content-Type", "application/json")
default:
break
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Accept-Encoding", "identity")
req.Header.Add("x-xss-protection", "1; mode=block")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if err = json.NewDecoder(resp.Body).Decode(result); err != nil {
// fmt.Printf("%s-URL:%s-ERROR:%v\n", requestModel.Type, requestModel.URL, err)
return nil
}
if resp.StatusCode >= http.StatusBadRequest {
message := fmt.Sprintf("%s-URL:%s-ERROR CODE:%v\n", requestModel.Type, requestModel.URL, resp.StatusCode)
if result != nil && !reflect.ValueOf(result).IsNil() {
switch (*result).(type) {
case string:
message = fmt.Sprintf("%s %v", message, *result)
default:
temp, ok := (*result).(map[string]interface{})
fmt.Printf("res %+v\n", temp)
if ok && temp != nil && temp["data"] != nil {
data := (temp["data"]).(map[string]interface{})
if data != nil {
msg := data["message"]
if msg != nil {
msgStr := msg.(string)
if msgStr != "" {
message = msgStr
}
}
}
}
}
}
return echo.NewHTTPError(resp.StatusCode, message)
}
return nil
}
// Get request
func Get(requestModel RequestModel, result *interface{}) error {
requestModel.Type = "GET"
return Base(requestModel, result)
}
// Post request
func Post(requestModel RequestModel, result *interface{}) error {
requestModel.Type = "POST"
return Base(requestModel, result)
}
// Put request
func Put(requestModel RequestModel, result *interface{}) error {
requestModel.Type = "PUT"
return Base(requestModel, result)
}
// Patch request
func Patch(requestModel RequestModel, result *interface{}) error {
requestModel.Type = "PATCH"
return Base(requestModel, result)
}
// Delete request
func Delete(requestModel RequestModel, result *interface{}) error {
requestModel.Type = "DELETE"
return Base(requestModel, result)
}