Simplify code

This commit is contained in:
Unknwon 2016-07-17 08:46:54 +08:00
parent ee68cd9eef
commit f2f681f7cb
12 changed files with 29 additions and 41 deletions

View file

@ -8,7 +8,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, error) {
@ -18,5 +17,5 @@ func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization
}
org := new(Organization)
return org, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), org)
jsonHeader, bytes.NewReader(body), org)
}

View file

@ -8,7 +8,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, error) {
@ -18,5 +17,5 @@ func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository
}
repo := new(Repository)
return repo, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
jsonHeader, bytes.NewReader(body), repo)
}

View file

@ -8,7 +8,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type CreateUserOption struct {
@ -26,8 +25,7 @@ func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, error) {
return nil, err
}
user := new(User)
return user, c.getParsedResponse("POST", "/admin/users",
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), user)
return user, c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user)
}
type EditUserOption struct {
@ -49,8 +47,7 @@ func (c *Client) AdminEditUser(user string, opt EditUserOption) error {
if err != nil {
return err
}
_, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
_, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), jsonHeader, bytes.NewReader(body))
return err
}
@ -65,6 +62,5 @@ func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*Pu
return nil, err
}
key := new(PublicKey)
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key)
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, bytes.NewReader(body), key)
}

View file

@ -14,7 +14,7 @@ import (
)
func Version() string {
return "0.8.3"
return "0.9.0"
}
// Client represents a Gogs API client.

View file

@ -8,7 +8,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
@ -63,7 +62,7 @@ func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue,
}
issue := new(Issue)
return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), issue)
jsonHeader, bytes.NewReader(body), issue)
}
type EditIssueOption struct {
@ -80,5 +79,5 @@ func (c *Client) EditIssue(owner, repo string, index int, opt EditIssueOption) (
}
issue := new(Issue)
return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), issue)
jsonHeader, bytes.NewReader(body), issue)
}

4
org.go
View file

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

10
repo.go
View file

@ -8,7 +8,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
@ -62,8 +61,7 @@ func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) {
return nil, err
}
repo := new(Repository)
return repo, c.getParsedResponse("POST", "/user/repos",
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo)
}
// CreateOrgRepo creates an organization repository for authenticated user.
@ -73,8 +71,7 @@ func (c *Client) CreateOrgRepo(org string, opt CreateRepoOption) (*Repository, e
return nil, err
}
repo := new(Repository)
return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo)
}
// GetRepo returns information of a repository of given owner.
@ -111,6 +108,5 @@ func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
return nil, err
}
repo := new(Repository)
return repo, c.getParsedResponse("POST", "/repos/migrate",
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
}

View file

@ -9,7 +9,6 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"
)
@ -47,8 +46,7 @@ func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook,
return nil, err
}
h := new(Hook)
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h)
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
}
type EditHookOption struct {
@ -62,8 +60,12 @@ func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) e
if err != nil {
return err
}
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
return err
}
func (c *Client) DeleteRepoHook(user, repo string, id int64) error {
_, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), nil, nil)
return err
}

View file

@ -8,7 +8,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
@ -42,8 +41,7 @@ func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*Deplo
return nil, err
}
key := new(DeployKey)
return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo),
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key)
return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key)
}
func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) error {

View file

@ -7,7 +7,6 @@ package gogs
import (
"bytes"
"encoding/json"
"net/http"
)
type Email struct {
@ -31,8 +30,7 @@ func (c *Client) AddEmail(opt CreateEmailOption) ([]*Email, error) {
return nil, err
}
emails := make([]*Email, 0, 3)
return emails, c.getParsedResponse("POST", "/user/emails",
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), emails)
return emails, c.getParsedResponse("POST", "/user/emails", jsonHeader, bytes.NewReader(body), emails)
}
func (c *Client) DeleteEmail(opt CreateEmailOption) error {
@ -40,7 +38,6 @@ func (c *Client) DeleteEmail(opt CreateEmailOption) error {
if err != nil {
return err
}
_, err = c.getResponse("DELETE", "/user/emails",
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
_, err = c.getResponse("DELETE", "/user/emails", jsonHeader, bytes.NewReader(body))
return err
}

View file

@ -8,7 +8,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
@ -41,8 +40,7 @@ func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) {
return nil, err
}
key := new(PublicKey)
return key, c.getParsedResponse("POST", "/user/keys",
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key)
return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
}
func (c *Client) DeletePublicKey(keyID int64) error {

View file

@ -4,6 +4,12 @@
package gogs
import (
"net/http"
)
var jsonHeader = http.Header{"content-type": []string{"application/json"}}
func Bool(v bool) *bool {
return &v
}