gogs-cli/cmd/util.go
2016-07-28 06:46:02 -04:00

71 lines
1.4 KiB
Go

package cmd
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
gogs "github.com/gogits/go-gogs-client"
"github.com/spf13/viper"
)
type expRes struct {
Data []*gogs.Repository `json:"data"`
}
func getResponse(client *http.Client, method, url string, header http.Header, body io.Reader) ([]byte, error) {
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "token "+viper.GetString("token"))
for k, v := range header {
req.Header[k] = v
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 403:
return nil, errors.New("403 Forbidden")
case 404:
return nil, errors.New("404 Not Found")
}
if resp.StatusCode/100 != 2 {
errMap := make(map[string]interface{})
if err = json.Unmarshal(data, &errMap); err != nil {
return nil, err
}
return nil, errors.New(errMap["message"].(string))
}
return data, nil
}
func getParsedResponse(client *http.Client, method, path string, header http.Header, body io.Reader) ([]*gogs.Repository, error) {
data, err := getResponse(client, method, path, header, body)
if err != nil {
return nil, err
}
var res expRes
err = json.Unmarshal(data, &res)
if err != nil {
return nil, err
}
return res.Data, err
// return json.Unmarshal(data, obj)
}