forked from zambien/go-apigee-edge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
company.go
102 lines (72 loc) · 2.08 KB
/
company.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package apigee
import (
"path"
)
// CompanyService is an interface for interfacing with the Apigee Edge Admin API
// dealing with companys.
type CompanyService interface {
Get(string) (*Company, *Response, error)
Create(Company) (*Company, *Response, error)
Delete(string) (*Response, error)
Update(Company) (*Company, *Response, error)
}
type CompanyServiceOp struct {
client *EdgeClient
}
var _ CompanyService = &CompanyServiceOp{}
type Company struct {
Name string `json:"name,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Attributes []Attribute `json:"attributes,omitempty"`
Status string `json:"status,omitempty"`
Apps []string `json:"apps,omitempty"`
}
func (s *CompanyServiceOp) Get(name string) (*Company, *Response, error) {
path := path.Join("companies", name)
req, e := s.client.NewRequest("GET", path, nil, "")
if e != nil {
return nil, nil, e
}
returnedCompany := Company{}
resp, e := s.client.Do(req, &returnedCompany)
if e != nil {
return nil, resp, e
}
return &returnedCompany, resp, e
}
func (s *CompanyServiceOp) Create(company Company) (*Company, *Response, error) {
return postOrPutCompany(company, "POST", s)
}
func (s *CompanyServiceOp) Update(company Company) (*Company, *Response, error) {
return postOrPutCompany(company, "PUT", s)
}
func (s *CompanyServiceOp) Delete(name string) (*Response, error) {
path := path.Join("companies", name)
req, e := s.client.NewRequest("DELETE", path, nil, "")
if e != nil {
return nil, e
}
resp, e := s.client.Do(req, nil)
if e != nil {
return resp, e
}
return resp, e
}
func postOrPutCompany(company Company, opType string, s *CompanyServiceOp) (*Company, *Response, error) {
uripath := ""
if opType == "PUT" {
uripath = path.Join("companies", company.Name)
} else {
uripath = path.Join("companies")
}
req, e := s.client.NewRequest(opType, uripath, company, "")
if e != nil {
return nil, nil, e
}
returnedCompany := Company{}
resp, e := s.client.Do(req, &returnedCompany)
if e != nil {
return nil, resp, e
}
return &returnedCompany, resp, e
}