Skip to content

Commit f2f681f

Browse files
committed
Simplify code
1 parent ee68cd9 commit f2f681f

File tree

12 files changed

+29
-41
lines changed

12 files changed

+29
-41
lines changed

admin_org.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11-
"net/http"
1211
)
1312

1413
func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, error) {
@@ -18,5 +17,5 @@ func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization
1817
}
1918
org := new(Organization)
2019
return org, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user),
21-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), org)
20+
jsonHeader, bytes.NewReader(body), org)
2221
}

admin_repo.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11-
"net/http"
1211
)
1312

1413
func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, error) {
@@ -18,5 +17,5 @@ func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository
1817
}
1918
repo := new(Repository)
2019
return repo, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user),
21-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
20+
jsonHeader, bytes.NewReader(body), repo)
2221
}

admin_user.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11-
"net/http"
1211
)
1312

1413
type CreateUserOption struct {
@@ -26,8 +25,7 @@ func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, error) {
2625
return nil, err
2726
}
2827
user := new(User)
29-
return user, c.getParsedResponse("POST", "/admin/users",
30-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), user)
28+
return user, c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user)
3129
}
3230

3331
type EditUserOption struct {
@@ -49,8 +47,7 @@ func (c *Client) AdminEditUser(user string, opt EditUserOption) error {
4947
if err != nil {
5048
return err
5149
}
52-
_, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user),
53-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
50+
_, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), jsonHeader, bytes.NewReader(body))
5451
return err
5552
}
5653

@@ -65,6 +62,5 @@ func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*Pu
6562
return nil, err
6663
}
6764
key := new(PublicKey)
68-
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user),
69-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key)
65+
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, bytes.NewReader(body), key)
7066
}

gogs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func Version() string {
17-
return "0.8.3"
17+
return "0.9.0"
1818
}
1919

2020
// Client represents a Gogs API client.

issue.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11-
"net/http"
1211
"time"
1312
)
1413

@@ -63,7 +62,7 @@ func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue,
6362
}
6463
issue := new(Issue)
6564
return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
66-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), issue)
65+
jsonHeader, bytes.NewReader(body), issue)
6766
}
6867

6968
type EditIssueOption struct {
@@ -80,5 +79,5 @@ func (c *Client) EditIssue(owner, repo string, index int, opt EditIssueOption) (
8079
}
8180
issue := new(Issue)
8281
return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
83-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), issue)
82+
jsonHeader, bytes.NewReader(body), issue)
8483
}

org.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11-
"net/http"
1211
)
1312

1413
type Organization struct {
@@ -56,7 +55,6 @@ func (c *Client) EditOrg(orgname string, opt EditOrgOption) error {
5655
if err != nil {
5756
return err
5857
}
59-
_, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname),
60-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
58+
_, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname), jsonHeader, bytes.NewReader(body))
6159
return err
6260
}

repo.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11-
"net/http"
1211
"time"
1312
)
1413

@@ -62,8 +61,7 @@ func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) {
6261
return nil, err
6362
}
6463
repo := new(Repository)
65-
return repo, c.getParsedResponse("POST", "/user/repos",
66-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
64+
return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo)
6765
}
6866

6967
// CreateOrgRepo creates an organization repository for authenticated user.
@@ -73,8 +71,7 @@ func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, e
7371
return nil, err
7472
}
7573
repo := new(Repository)
76-
return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org),
77-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
74+
return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo)
7875
}
7976

8077
// GetRepo returns information of a repository of given owner.
@@ -111,6 +108,5 @@ func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
111108
return nil, err
112109
}
113110
repo := new(Repository)
114-
return repo, c.getParsedResponse("POST", "/repos/migrate",
115-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
111+
return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
116112
}

repo_hook.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"encoding/json"
1010
"errors"
1111
"fmt"
12-
"net/http"
1312
"strings"
1413
"time"
1514
)
@@ -47,8 +46,7 @@ func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook,
4746
return nil, err
4847
}
4948
h := new(Hook)
50-
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
51-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h)
49+
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
5250
}
5351

5452
type EditHookOption struct {
@@ -62,8 +60,12 @@ func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) e
6260
if err != nil {
6361
return err
6462
}
65-
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id),
66-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
63+
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
64+
return err
65+
}
66+
67+
func (c *Client) DeleteRepoHook(user, repo string, id int64) error {
68+
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
6769
return err
6870
}
6971

repo_key.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11-
"net/http"
1211
"time"
1312
)
1413

@@ -42,8 +41,7 @@ func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*Deplo
4241
return nil, err
4342
}
4443
key := new(DeployKey)
45-
return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo),
46-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key)
44+
return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key)
4745
}
4846

4947
func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) error {

user_email.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package gogs
77
import (
88
"bytes"
99
"encoding/json"
10-
"net/http"
1110
)
1211

1312
type Email struct {
@@ -31,16 +30,14 @@ func (c *Client) AddEmail(opt CreateEmailOption) ([]*Email, error) {
3130
return nil, err
3231
}
3332
emails := make([]*Email, 0, 3)
34-
return emails, c.getParsedResponse("POST", "/user/emails",
35-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), emails)
33+
return emails, c.getParsedResponse("POST", "/user/emails", jsonHeader, bytes.NewReader(body), emails)
3634
}
3735

3836
func (c *Client) DeleteEmail(opt CreateEmailOption) error {
3937
body, err := json.Marshal(&opt)
4038
if err != nil {
4139
return err
4240
}
43-
_, err = c.getResponse("DELETE", "/user/emails",
44-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
41+
_, err = c.getResponse("DELETE", "/user/emails", jsonHeader, bytes.NewReader(body))
4542
return err
4643
}

user_key.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"bytes"
99
"encoding/json"
1010
"fmt"
11-
"net/http"
1211
"time"
1312
)
1413

@@ -41,8 +40,7 @@ func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) {
4140
return nil, err
4241
}
4342
key := new(PublicKey)
44-
return key, c.getParsedResponse("POST", "/user/keys",
45-
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key)
43+
return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
4644
}
4745

4846
func (c *Client) DeletePublicKey(keyID int64) error {

utils.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
package gogs
66

7+
import (
8+
"net/http"
9+
)
10+
11+
var jsonHeader = http.Header{"content-type": []string{"application/json"}}
12+
713
func Bool(v bool) *bool {
814
return &v
915
}

0 commit comments

Comments
 (0)