-
Notifications
You must be signed in to change notification settings - Fork 0
/
curl.go
110 lines (105 loc) · 2.65 KB
/
curl.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
package main
import(
"net/http"
"io/ioutil"
"strings"
"net/url"
"errors"
"io"
"encoding/json"
)
//Curl ----------------------------------------------------------
type Curl struct {
getStr curlGetStr
getInterface curlGetInterface
setRequest curlSetRequest
}
var curl Curl
//getRequest
func (*Curl) getRequest(urlStr string, AllCapsMethod string, body io.Reader) (request *http.Request, err error){
request, err = http.NewRequest(AllCapsMethod, urlStr, body)
if err != nil {
logger.Println(err.Error())
return nil, err
}
return request, err
}
//getStr
type curlGetStr struct{}
//getStr.results
func (*curlGetStr) results(urlStr string) (resStr string, err error){
if urlStr == ""{
errStr := `error : getStr.results got urlStr of ""`
logger.Printf(errStr)
return "" , errors.New(errStr)
}
response, err := http.Get(urlStr)
if err != nil {
logger.Println(err.Error())
return "", err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
logger.Println(err.Error())
return "", err
}
defer response.Body.Close()
return string(body), nil
}
//getStr.requestResults
func (*curlGetStr) requestResults(request *http.Request) (resStr string, err error){
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
logger.Println(err.Error())
return
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
logger.Println(err.Error())
return "", err
}
defer response.Body.Close()
return string(body), nil
}
//getStr.formatedURL
func (*curlGetStr) formatedURL(urlStr string)(resStr string){
return strings.ReplaceAll(urlStr,"\\","")
}
//getStr.decoded
func (*curlGetStr) decoded(encodedStr string)(resStr string, err error){
resStr, err = url.QueryUnescape(encodedStr)
if err != nil {
logger.Println(err.Error())
return "", err
}
return resStr, err
}
//getInterface
type curlGetInterface struct{}
//getInterface.requestResults
func (*curlGetInterface) requestResults(request *http.Request, resInterf interface{}) (err error){
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
logger.Println(err.Error())
return
}
// body, err := ioutil.ReadAll(response.Body)
// if err != nil {
// logger.Println(err.Error())
// return err
// }
defer response.Body.Close()
return json.NewDecoder(response.Body).Decode(resInterf)
}
//setRequest
type curlSetRequest struct{}
//setRequest.header
func (*curlSetRequest) header(request *http.Request, keyStr string, valStr string){
request.Header.Set(keyStr, valStr)
}
//setRequest.userNamePassword
func (*curlSetRequest) userNamePassword(request *http.Request, userNameStr string, passwordStr string){
request.SetBasicAuth(userNameStr, passwordStr)
}