Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 29fa1bf

Browse files
committed
convert body to query string in case of a GET request
1 parent 2858b80 commit 29fa1bf

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

gitea/gitea.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
package gitea
66

77
import (
8+
"bytes"
89
"encoding/json"
910
"errors"
1011
"fmt"
1112
"io"
1213
"io/ioutil"
1314
"net/http"
15+
"net/url"
1416
"strings"
17+
18+
"github.com/google/go-querystring/query"
1519
)
1620

1721
// Version return the library version
@@ -52,11 +56,59 @@ func (c *Client) SetSudo(sudo string) {
5256
c.sudo = sudo
5357
}
5458

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)
5766
if err != nil {
5867
return nil, err
5968
}
69+
70+
// Set the encoded path data
71+
u.RawPath = "/api/v1" + path
72+
u.Path = "/api/v1" + unescaped
73+
74+
if method == "GET" {
75+
if body != nil {
76+
q, err := query.Values(body)
77+
if err != nil {
78+
return nil, err
79+
}
80+
u.RawQuery = q.Encode()
81+
}
82+
}
83+
84+
req := &http.Request{
85+
Method: method,
86+
URL: u,
87+
Proto: "HTTP/1.1",
88+
ProtoMajor: 1,
89+
ProtoMinor: 1,
90+
Header: make(http.Header),
91+
Host: u.Host,
92+
}
93+
94+
if method == "POST" || method == "PUT" {
95+
bodyBytes, err := json.Marshal(body)
96+
if err != nil {
97+
return nil, err
98+
}
99+
bodyReader := bytes.NewReader(bodyBytes)
100+
101+
u.RawQuery = ""
102+
req.Body = ioutil.NopCloser(bodyReader)
103+
req.GetBody = func() (io.ReadCloser, error) {
104+
return ioutil.NopCloser(bodyReader), nil
105+
}
106+
req.ContentLength = int64(bodyReader.Len())
107+
req.Header.Set("Content-Type", "application/json")
108+
}
109+
110+
req.Header.Set("Accept", "application/json")
111+
60112
if len(c.accessToken) != 0 {
61113
req.Header.Set("Authorization", "token "+c.accessToken)
62114
}
@@ -70,7 +122,7 @@ func (c *Client) doRequest(method, path string, header http.Header, body io.Read
70122
return c.client.Do(req)
71123
}
72124

73-
func (c *Client) getResponse(method, path string, header http.Header, body io.Reader) ([]byte, error) {
125+
func (c *Client) getResponse(method, path string, header http.Header, body interface{}) ([]byte, error) {
74126
resp, err := c.doRequest(method, path, header, body)
75127
if err != nil {
76128
return nil, err
@@ -106,7 +158,7 @@ func (c *Client) getResponse(method, path string, header http.Header, body io.Re
106158
return data, nil
107159
}
108160

109-
func (c *Client) getParsedResponse(method, path string, header http.Header, body io.Reader, obj interface{}) error {
161+
func (c *Client) getParsedResponse(method, path string, header http.Header, body interface{}, obj interface{}) error {
110162
data, err := c.getResponse(method, path, header, body)
111163
if err != nil {
112164
return err

gitea/pull.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,14 @@ type PRBranchInfo struct {
6363

6464
// ListPullRequestsOptions options for listing pull requests
6565
type ListPullRequestsOptions struct {
66-
Page int `json:"page"`
67-
State string `json:"state"`
66+
Page int `url:"page"`
67+
State string `url:"state"`
6868
}
6969

7070
// ListRepoPullRequests list PRs of one repository
71-
func (c *Client) ListRepoPullRequests(owner, repo string, opt ListPullRequestsOptions) ([]*PullRequest, error) {
72-
body, err := json.Marshal(&opt)
73-
if err != nil {
74-
return nil, err
75-
}
71+
func (c *Client) ListRepoPullRequests(owner, repo string, opt *ListPullRequestsOptions) ([]*PullRequest, error) {
7672
prs := make([]*PullRequest, 0, 10)
77-
return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, bytes.NewReader(body), &prs)
73+
return prs, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/pulls", owner, repo), jsonHeader, opt, &prs)
7874
}
7975

8076
// GetPullRequest get information of one PR

0 commit comments

Comments
 (0)