Simplify code
This commit is contained in:
parent
ee68cd9eef
commit
f2f681f7cb
12 changed files with 29 additions and 41 deletions
|
@ -8,7 +8,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) AdminCreateOrg(user string, opt CreateOrgOption) (*Organization, error) {
|
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)
|
org := new(Organization)
|
||||||
return org, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/orgs", user),
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (c *Client) AdminCreateRepo(user string, opt CreateRepoOption) (*Repository, error) {
|
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)
|
repo := new(Repository)
|
||||||
return repo, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/repos", user),
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type CreateUserOption struct {
|
type CreateUserOption struct {
|
||||||
|
@ -26,8 +25,7 @@ func (c *Client) AdminCreateUser(opt CreateUserOption) (*User, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
user := new(User)
|
user := new(User)
|
||||||
return user, c.getParsedResponse("POST", "/admin/users",
|
return user, c.getParsedResponse("POST", "/admin/users", jsonHeader, bytes.NewReader(body), user)
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), user)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EditUserOption struct {
|
type EditUserOption struct {
|
||||||
|
@ -49,8 +47,7 @@ func (c *Client) AdminEditUser(user string, opt EditUserOption) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user),
|
_, err = c.getResponse("PATCH", fmt.Sprintf("/admin/users/%s", user), jsonHeader, bytes.NewReader(body))
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,6 +62,5 @@ func (c *Client) AdminCreateUserPublicKey(user string, opt CreateKeyOption) (*Pu
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
key := new(PublicKey)
|
key := new(PublicKey)
|
||||||
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user),
|
return key, c.getParsedResponse("POST", fmt.Sprintf("/admin/users/%s/keys", user), jsonHeader, bytes.NewReader(body), key)
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key)
|
|
||||||
}
|
}
|
||||||
|
|
2
gogs.go
2
gogs.go
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Version() string {
|
func Version() string {
|
||||||
return "0.8.3"
|
return "0.9.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
// Client represents a Gogs API client.
|
// Client represents a Gogs API client.
|
||||||
|
|
5
issue.go
5
issue.go
|
@ -8,7 +8,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -63,7 +62,7 @@ func (c *Client) CreateIssue(owner, repo string, opt CreateIssueOption) (*Issue,
|
||||||
}
|
}
|
||||||
issue := new(Issue)
|
issue := new(Issue)
|
||||||
return issue, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/issues", owner, repo),
|
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 {
|
type EditIssueOption struct {
|
||||||
|
@ -80,5 +79,5 @@ func (c *Client) EditIssue(owner, repo string, index int, opt EditIssueOption) (
|
||||||
}
|
}
|
||||||
issue := new(Issue)
|
issue := new(Issue)
|
||||||
return issue, c.getParsedResponse("PATCH", fmt.Sprintf("/repos/%s/%s/issues/%d", owner, repo, index),
|
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
4
org.go
|
@ -8,7 +8,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Organization struct {
|
type Organization struct {
|
||||||
|
@ -56,7 +55,6 @@ func (c *Client) EditOrg(orgname string, opt EditOrgOption) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname),
|
_, err = c.getResponse("PATCH", fmt.Sprintf("/orgs/%s", orgname), jsonHeader, bytes.NewReader(body))
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
10
repo.go
10
repo.go
|
@ -8,7 +8,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -62,8 +61,7 @@ func (c *Client) CreateRepo(opt CreateRepoOption) (*Repository, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
repo := new(Repository)
|
repo := new(Repository)
|
||||||
return repo, c.getParsedResponse("POST", "/user/repos",
|
return repo, c.getParsedResponse("POST", "/user/repos", jsonHeader, bytes.NewReader(body), repo)
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateOrgRepo creates an organization repository for authenticated user.
|
// 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
|
return nil, err
|
||||||
}
|
}
|
||||||
repo := new(Repository)
|
repo := new(Repository)
|
||||||
return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org),
|
return repo, c.getParsedResponse("POST", fmt.Sprintf("/org/%s/repos", org), jsonHeader, bytes.NewReader(body), repo)
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRepo returns information of a repository of given owner.
|
// GetRepo returns information of a repository of given owner.
|
||||||
|
@ -111,6 +108,5 @@ func (c *Client) MigrateRepo(opt MigrateRepoOption) (*Repository, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
repo := new(Repository)
|
repo := new(Repository)
|
||||||
return repo, c.getParsedResponse("POST", "/repos/migrate",
|
return repo, c.getParsedResponse("POST", "/repos/migrate", jsonHeader, bytes.NewReader(body), repo)
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), repo)
|
|
||||||
}
|
}
|
||||||
|
|
12
repo_hook.go
12
repo_hook.go
|
@ -9,7 +9,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -47,8 +46,7 @@ func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook,
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
h := new(Hook)
|
h := new(Hook)
|
||||||
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
|
return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), jsonHeader, bytes.NewReader(body), h)
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EditHookOption struct {
|
type EditHookOption struct {
|
||||||
|
@ -62,8 +60,12 @@ func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) e
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id),
|
_, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id), jsonHeader, bytes.NewReader(body))
|
||||||
http.Header{"content-type": []string{"application/json"}}, 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -42,8 +41,7 @@ func (c *Client) CreateDeployKey(user, repo string, opt CreateKeyOption) (*Deplo
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
key := new(DeployKey)
|
key := new(DeployKey)
|
||||||
return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo),
|
return key, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/keys", user, repo), jsonHeader, bytes.NewReader(body), key)
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) error {
|
func (c *Client) DeleteDeployKey(owner, repo string, keyID int64) error {
|
||||||
|
|
|
@ -7,7 +7,6 @@ package gogs
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Email struct {
|
type Email struct {
|
||||||
|
@ -31,8 +30,7 @@ func (c *Client) AddEmail(opt CreateEmailOption) ([]*Email, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
emails := make([]*Email, 0, 3)
|
emails := make([]*Email, 0, 3)
|
||||||
return emails, c.getParsedResponse("POST", "/user/emails",
|
return emails, c.getParsedResponse("POST", "/user/emails", jsonHeader, bytes.NewReader(body), emails)
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), emails)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeleteEmail(opt CreateEmailOption) error {
|
func (c *Client) DeleteEmail(opt CreateEmailOption) error {
|
||||||
|
@ -40,7 +38,6 @@ func (c *Client) DeleteEmail(opt CreateEmailOption) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = c.getResponse("DELETE", "/user/emails",
|
_, err = c.getResponse("DELETE", "/user/emails", jsonHeader, bytes.NewReader(body))
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -41,8 +40,7 @@ func (c *Client) CreatePublicKey(opt CreateKeyOption) (*PublicKey, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
key := new(PublicKey)
|
key := new(PublicKey)
|
||||||
return key, c.getParsedResponse("POST", "/user/keys",
|
return key, c.getParsedResponse("POST", "/user/keys", jsonHeader, bytes.NewReader(body), key)
|
||||||
http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), key)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) DeletePublicKey(keyID int64) error {
|
func (c *Client) DeletePublicKey(keyID int64) error {
|
||||||
|
|
6
utils.go
6
utils.go
|
@ -4,6 +4,12 @@
|
||||||
|
|
||||||
package gogs
|
package gogs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
var jsonHeader = http.Header{"content-type": []string{"application/json"}}
|
||||||
|
|
||||||
func Bool(v bool) *bool {
|
func Bool(v bool) *bool {
|
||||||
return &v
|
return &v
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue