5
5
package gitea
6
6
7
7
import (
8
+ "bytes"
8
9
"encoding/json"
9
10
"errors"
10
11
"fmt"
11
12
"io"
12
13
"io/ioutil"
13
14
"net/http"
15
+ "net/url"
14
16
"strings"
17
+
18
+ "github.com/google/go-querystring/query"
15
19
)
16
20
17
21
// Version return the library version
@@ -52,11 +56,57 @@ func (c *Client) SetSudo(sudo string) {
52
56
c .sudo = sudo
53
57
}
54
58
55
- func (c * Client ) doRequest (method , path string , header http.Header , body io.Reader ) (* http.Response , error ) {
56
- req , err := http .NewRequest (method , c .url + "/api/v1" + path , body )
59
+ func (c * Client ) doRequest (method , path string , header http.Header , body interface {}) (* http.Response , error ) {
60
+ u , err := url .Parse (c .url )
61
+ if err != nil {
62
+ return nil , err
63
+ }
64
+
65
+ unescaped , err := url .PathUnescape (path )
57
66
if err != nil {
58
67
return nil , err
59
68
}
69
+
70
+ // Set the encoded path data
71
+ u .RawPath = "/api/v1" + path
72
+ u .Path = "/api/v1" + unescaped
73
+
74
+ if body != nil {
75
+ q , err := query .Values (body )
76
+ if err != nil {
77
+ return nil , err
78
+ }
79
+ u .RawQuery = q .Encode ()
80
+ }
81
+
82
+ req := & http.Request {
83
+ Method : method ,
84
+ URL : u ,
85
+ Proto : "HTTP/1.1" ,
86
+ ProtoMajor : 1 ,
87
+ ProtoMinor : 1 ,
88
+ Header : make (http.Header ),
89
+ Host : u .Host ,
90
+ }
91
+
92
+ if method == "POST" || method == "PUT" {
93
+ bodyBytes , err := json .Marshal (body )
94
+ if err != nil {
95
+ return nil , err
96
+ }
97
+ bodyReader := bytes .NewReader (bodyBytes )
98
+
99
+ u .RawQuery = ""
100
+ req .Body = ioutil .NopCloser (bodyReader )
101
+ req .GetBody = func () (io.ReadCloser , error ) {
102
+ return ioutil .NopCloser (bodyReader ), nil
103
+ }
104
+ req .ContentLength = int64 (bodyReader .Len ())
105
+ req .Header .Set ("Content-Type" , "application/json" )
106
+ }
107
+
108
+ req .Header .Set ("Accept" , "application/json" )
109
+
60
110
if len (c .accessToken ) != 0 {
61
111
req .Header .Set ("Authorization" , "token " + c .accessToken )
62
112
}
@@ -70,7 +120,7 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read
70
120
return c .client .Do (req )
71
121
}
72
122
73
- func (c * Client ) getResponse (method , path string , header http.Header , body io. Reader ) ([]byte , error ) {
123
+ func (c * Client ) getResponse (method , path string , header http.Header , body interface {} ) ([]byte , error ) {
74
124
resp , err := c .doRequest (method , path , header , body )
75
125
if err != nil {
76
126
return nil , err
@@ -106,7 +156,7 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
106
156
return data , nil
107
157
}
108
158
109
- func (c * Client ) getParsedResponse (method , path string , header http.Header , body io. Reader , obj interface {}) error {
159
+ func (c * Client ) getParsedResponse (method , path string , header http.Header , body interface {} , obj interface {}) error {
110
160
data , err := c .getResponse (method , path , header , body )
111
161
if err != nil {
112
162
return err
0 commit comments