-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbody.go
More file actions
127 lines (106 loc) · 2.67 KB
/
body.go
File metadata and controls
127 lines (106 loc) · 2.67 KB
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
package grequest
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
)
type Body struct {
savePath string
Client *HTTPClient
}
const filesPath = "files"
// Sets the request body with io.Reader
func (c *HTTPClient) Body() *Body {
return &Body{Client: c}
}
// Gets http client object
func (c *Body) client() *HTTPClient {
return c.Client
}
func (c *Body) loadPath() string {
toPath := filesPath
if c.savePath != "" {
return c.savePath
}
currentHost := c.client().GetCurrentUrl()
path := fmt.Sprintf("%s/%s", toPath, currentHost.Host)
return path
}
// Gets raw io.ReadCloser from body
func (c *Body) get() io.Reader {
return io.NopCloser(bytes.NewReader(c.client().BodyBytes))
}
func (c *Body) getBytes() []byte {
return c.client().BodyBytes
}
// Sets the request body with io.Reader
func (c *Body) Set(body io.Reader) *HTTPClient {
c.client().request.body = body
return c.client()
}
// Sets the request body with only string
func (c *Body) SetString(body string) *HTTPClient {
c.client().request.body = strings.NewReader(body)
return c.client()
}
// Sets the request body with bytes
func (c *Body) SetByte(body []byte) *HTTPClient {
c.client().request.body = bytes.NewBuffer(body)
return c.client()
}
// Sets the request body with json
func (c *Body) SetJson(data interface{}) *HTTPClient {
jsonData, _ := json.Marshal(data)
c.client().request.body = bytes.NewBuffer(jsonData)
return c.client()
}
// Gets raw io.ReadCloser from body
func (c *Body) GetRaw() io.Reader {
return c.get()
}
// Gets response body and return in bytes
func (c *Body) GetBytes() []byte {
b := c.getBytes()
return b
}
// Gets response body and return as string
func (c *Body) GetStrings() string {
b := c.getBytes()
return string(b)
}
// Gets response body and return as interface map array
func (c *Body) GetWithJson() (interface{}, error) {
content := c.getBytes()
var WithJson interface{}
err := json.Unmarshal(content, &WithJson)
return WithJson, err
}
// Gets response body and make to struct
func (c *Body) GetWithJsonStruct(target interface{}) error {
b := c.getBytes()
err := json.Unmarshal(b, &target)
if err != nil {
return err
}
return nil
}
// Sets path for save
func (c *Body) Path(path string) *Body {
c.savePath = path
return c
}
// Save response body to file
func (c *Body) ToFile(fileName string) {
saveToFile(fileName, c.get())
}
// Save response body to file
func (c *Body) SaveFile() *Body {
extension := getFileExtensionByContentType(c.client().ContentType().Get())
currentHost := c.client().GetCurrentUrl()
name := getFileNameByPath(currentHost.Path)
fileName := fmt.Sprintf("%s/%s%s", c.loadPath(), name, extension)
saveToFile(fileName, c.get())
return c
}