71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
|
package gapi
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"io/ioutil"
|
||
|
)
|
||
|
|
||
|
type Org struct {
|
||
|
Id int64
|
||
|
Name string
|
||
|
}
|
||
|
|
||
|
func (c *Client) Orgs() ([]Org, error) {
|
||
|
orgs := make([]Org, 0)
|
||
|
|
||
|
req, err := c.newRequest("GET", "/api/orgs/", nil)
|
||
|
if err != nil {
|
||
|
return orgs, err
|
||
|
}
|
||
|
resp, err := c.Do(req)
|
||
|
if err != nil {
|
||
|
return orgs, err
|
||
|
}
|
||
|
if resp.StatusCode != 200 {
|
||
|
return orgs, errors.New(resp.Status)
|
||
|
}
|
||
|
data, err := ioutil.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return orgs, err
|
||
|
}
|
||
|
err = json.Unmarshal(data, &orgs)
|
||
|
return orgs, err
|
||
|
}
|
||
|
|
||
|
func (c *Client) NewOrg(name string) error {
|
||
|
settings := map[string]string{
|
||
|
"name": name,
|
||
|
}
|
||
|
data, err := json.Marshal(settings)
|
||
|
req, err := c.newRequest("POST", "/api/orgs", bytes.NewBuffer(data))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
resp, err := c.Do(req)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if resp.StatusCode != 200 {
|
||
|
return errors.New(resp.Status)
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (c *Client) DeleteOrg(id int64) error {
|
||
|
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/orgs/%d", id), nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
resp, err := c.Do(req)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if resp.StatusCode != 200 {
|
||
|
return errors.New(resp.Status)
|
||
|
}
|
||
|
return err
|
||
|
}
|