/
response.go
125 lines (103 loc) · 2.67 KB
/
response.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
/*
* Revision History:
* Initial: 2018/05/28 Li Zebang
*/
package client
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"github.com/TechCatsLab/apix/file"
)
const (
HeaderContentType = "Content-Type"
MIMEApplicationJSON = "application/json"
MIMEApplicationXML = "application/xml"
MIMETextXML = "text/xml"
MIMEApplicationForm = "application/x-www-form-urlencoded"
MIMEMultipartForm = "multipart/form-data"
)
var (
ErrUnsupportedMediaType = errors.New("unsupported media type")
)
// Response represents the response from an HTTP request.
type Response struct {
HTTPResponse *http.Response
}
// ToBytes -
func (r *Response) ToBytes() ([]byte, error) {
defer r.HTTPResponse.Body.Close()
return ioutil.ReadAll(r.HTTPResponse.Body)
}
// ToString -
func (r *Response) ToString() (string, error) {
defer r.HTTPResponse.Body.Close()
bs, err := r.ToBytes()
return string(bs), err
}
// ToObject -
func (r *Response) ToObject(i interface{}) error {
defer r.HTTPResponse.Body.Close()
ctype := r.HTTPResponse.Header.Get(HeaderContentType)
switch {
case strings.HasPrefix(ctype, MIMEApplicationJSON):
err := json.NewDecoder(r.HTTPResponse.Body).Decode(i)
if err != nil {
return err
}
case strings.HasPrefix(ctype, MIMEApplicationXML), strings.HasPrefix(ctype, MIMETextXML):
err := xml.NewDecoder(r.HTTPResponse.Body).Decode(i)
if err != nil {
return err
}
default:
return ErrUnsupportedMediaType
}
return nil
}
// SaveAsFile -
func (r *Response) SaveAsFile(directory string) (written int64, err error) {
defer r.HTTPResponse.Body.Close()
var name, ext, filename string
directory, err = filepath.Abs(directory)
if err != nil {
return 0, err
}
name = file.FileName(r.HTTPResponse.Request.URL.String())
ext = path.Ext(r.HTTPResponse.Request.URL.String())
if ext == "" {
contentType := r.HTTPResponse.Header.Get(HeaderContentType)
if contentType == "" {
exts, err := mime.ExtensionsByType(contentType)
if err == nil {
ext = exts[0]
}
}
}
filename = fmt.Sprintf("%s/%s%s", directory, name, ext)
for i := 1; file.IsExist(filename); i++ {
filename = fmt.Sprintf("%s/%s(%d)%s", directory, name, i, ext)
}
fmt.Println(directory, name, ext)
if !file.IsDirExist(directory) {
return 0, fmt.Errorf("target directory (%s) cann't be found", directory)
}
if !file.IsPermissionDenied(directory) {
return 0, fmt.Errorf("target directory (%s): permission denied", directory)
}
file, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return 0, err
}
defer file.Close()
return io.Copy(file, r.HTTPResponse.Body)
}