-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
176 lines (137 loc) · 3.71 KB
/
utils.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
package gocouchlib
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
)
// CouchResponse contains the elements of the HTTP response returned by the CouchDB Server
type CouchResponse struct {
// Json payload returned by the CouchDB server as a response to the request
Json JsonObj
// HTTP Status code returned by the CouchDB server
StatusCode int
// HTTP Response Headers returned by the CouchDB server
Headers http.Header
}
type JsonObj interface{}
var hc = &http.Client{}
type HttpClient struct {
}
var httpClient = &HttpClient{}
func (this *HttpClient) Get(url string, headers http.Header) (*CouchResponse, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
throwError(err)
}
req.Header.Add("Content-Type", "application/json")
resp, err := hc.Do(req)
if err != nil {
throwError(err)
}
defer resp.Body.Close()
return &CouchResponse{Json: getResponseJson(resp), StatusCode: resp.StatusCode, Headers: resp.Header}, nil
}
func (this *HttpClient) Head(url string, headers http.Header) (*CouchResponse, error) {
fmt.Println("=> URL", url)
req, err := http.NewRequest("HEAD", url, nil)
if err != nil {
throwError(err)
}
if headers != nil {
req.Header = headers
}
resp, err := hc.Do(req)
if err != nil {
throwError(err)
}
return &CouchResponse{Json: getResponseJson(resp), StatusCode: resp.StatusCode, Headers: resp.Header}, nil
}
func (this *HttpClient) Delete(url string) (*CouchResponse, error) {
fmt.Println("URL: ", url)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
throwError(err)
}
req.Header.Add("Content-Type", "application/json")
resp, err := hc.Do(req)
if err != nil {
throwError(err)
}
return &CouchResponse{Json: getResponseJson(resp), StatusCode: resp.StatusCode, Headers: resp.Header}, nil
}
func (this *HttpClient) Put(url string, jsonObj JsonObj, headers http.Header) (*CouchResponse, error) {
var req *http.Request
var err error
if jsonObj != nil {
jsonBytes, err := json.Marshal(&jsonObj)
if err != nil {
throwError(err)
}
req, err = http.NewRequest("PUT", url, bytes.NewBuffer(jsonBytes))
if err != nil {
throwError(err)
}
} else {
req, err = http.NewRequest("PUT", url, nil)
if err != nil {
throwError(err)
}
}
if headers != nil {
req.Header = headers
}
req.Header.Add("Content-Type", "application/json")
resp, err := hc.Do(req)
if err != nil {
throwError(err)
}
return &CouchResponse{Json: getResponseJson(resp), StatusCode: resp.StatusCode, Headers: resp.Header}, nil
}
func (this *HttpClient) Post(url string, jsonObj JsonObj) (*CouchResponse, error) {
// fmt.Println("=> utils.Save() entry: ")
var reqBody *bytes.Buffer
if jsonObj == nil {
_ = json.Unmarshal([]byte(`{}`), &jsonObj)
}
jsonBytes, err := json.Marshal(&jsonObj)
if err != nil {
throwError(err)
}
// fmt.Println("=> utils.Save(): ", string(jsonBytes))
reqBody = bytes.NewBuffer(jsonBytes)
req, err := http.NewRequest("POST", url, reqBody)
if err != nil {
throwError(err)
}
req.Header.Add("Content-Type", "application/json")
resp, err := hc.Do(req)
if err != nil {
throwError(err)
}
return &CouchResponse{Json: getResponseJson(resp), StatusCode: resp.StatusCode, Headers: resp.Header}, nil
}
func throwError(err error) (JsonObj, error) {
return nil, &CouchError{
time.Now(), err.Error(),
}
}
func getResponseJson(resp *http.Response) JsonObj {
var jsonRespObj JsonObj
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
throwError(err)
}
//fmt.Println("=> [Log]: HTTP response dump:", string(body))
err = json.Unmarshal(body, &jsonRespObj)
if err != nil {
throwError(err)
}
return jsonRespObj
}
func TrimEtag(etag string) string {
return strings.Trim(etag, "\"")
}