-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
41 lines (34 loc) · 992 Bytes
/
post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package examples
import (
"errors"
"net/http"
)
type GithubError struct {
StatusCode int `json:"-"`
Message string `json:"message"`
DocumentationURL string `json:"documentation_url"`
}
type Repository struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Private bool `json:"private"`
}
func CreateRepo(request Repository) (*Repository, error) {
response, err := httpClient.Post("https://api.github.com/user/repos", request)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusCreated {
var githubError GithubError
if err := response.UnmarshalJson(&githubError); err != nil {
return nil, errors.New("An error occured while processing github error response " +
"while attempting to create a new repo.")
}
return nil, errors.New(githubError.Message)
}
var result Repository
if err := response.UnmarshalJson(&result); err != nil {
return nil, err
}
return &result, nil
}