From f2f681f7cbaad80077a20f2e116b756a8ce8186c Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sun, 17 Jul 2016 08:46:54 +0800 Subject: [PATCH] Simplify code --- admin_org.go | 3 +-- admin_repo.go | 3 +-- admin_user.go | 10 +++------- gogs.go | 2 +- issue.go | 5 ++--- org.go | 4 +--- repo.go | 10 +++------- repo_hook.go | 12 +++++++----- repo_key.go | 4 +--- user_email.go | 7 ++----- user_key.go | 4 +--- utils.go | 6 ++++++ 12 files changed, 29 insertions(+), 41 deletions(-) diff --git a/admin_org.go b/admin_org.go index 92f2cd9..be14062 100644 --- a/admin_org.go +++ b/admin_org.go @@ -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) } diff --git a/admin_repo.go b/admin_repo.go index 8a213cb..50ba2be 100644 --- a/admin_repo.go +++ b/admin_repo.go @@ -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) } diff --git a/admin_user.go b/admin_user.go index f8e26d9..4053150 100644 --- a/admin_user.go +++ b/admin_user.go @@ -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) } diff --git a/gogs.go b/gogs.go index 1259929..e3cbe41 100644 --- a/gogs.go +++ b/gogs.go @@ -14,7 +14,7 @@ import ( ) func Version() string { - return "0.8.3" + return "0.9.0" } // Client represents a Gogs API client. diff --git a/issue.go b/issue.go index 3204395..69e5c2e 100644 --- a/issue.go +++ b/issue.go @@ -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) } diff --git a/org.go b/org.go index a68462d..08d0088 100644 --- a/org.go +++ b/org.go @@ -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 } diff --git a/repo.go b/repo.go index adfb21a..4f48592 100644 --- a/repo.go +++ b/repo.go @@ -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) } diff --git a/repo_hook.go b/repo_hook.go index 348458c..47fa13e 100644 --- a/repo_hook.go +++ b/repo_hook.go @@ -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 } diff --git a/repo_key.go b/repo_key.go index dfd15c9..2201602 100644 --- a/repo_key.go +++ b/repo_key.go @@ -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 { diff --git a/user_email.go b/user_email.go index b346599..02dd402 100644 --- a/user_email.go +++ b/user_email.go @@ -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 } diff --git a/user_key.go b/user_key.go index 99e8c09..c0278e0 100644 --- a/user_key.go +++ b/user_key.go @@ -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 { diff --git a/utils.go b/utils.go index 0083e05..a4d673e 100644 --- a/utils.go +++ b/utils.go @@ -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 }