This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
forked from s7techlab/hlf-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
affiliation.go
142 lines (110 loc) · 3.65 KB
/
affiliation.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package ca
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/pkg/errors"
"github.com/atomyze-ru/hlf-sdk-go/api/ca"
)
const (
endpointAffiliationList = "%s/api/v1/affiliations%s"
endpointAffiliationCreate = "%s/api/v1/affiliations%s"
endpointAffiliationDelete = "%s/api/v1/affiliations/%s"
)
func (c *core) AffiliationList(ctx context.Context, rootAffiliation ...string) ([]ca.Identity, []ca.Affiliation, error) {
var reqUrl string
if len(rootAffiliation) == 1 {
reqUrl = fmt.Sprintf(endpointAffiliationList, c.config.Host, `/`+rootAffiliation[0])
} else {
reqUrl = fmt.Sprintf(endpointAffiliationList, c.config.Host, ``)
}
req, err := http.NewRequest(http.MethodGet, reqUrl, nil)
if err != nil {
return nil, nil, errors.Wrap(err, `failed to create request`)
}
if err = c.setAuthToken(req, nil); err != nil {
return nil, nil, errors.Wrap(err, `failed to set auth token`)
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return nil, nil, errors.Wrap(err, `failed to process request`)
}
var affiliationResponse ca.ResponseAffiliationList
if err = c.processResponse(resp, &affiliationResponse, http.StatusOK, http.StatusCreated); err != nil {
return nil, nil, err
}
return affiliationResponse.Identities, affiliationResponse.Affiliations, nil
}
func (c *core) AffiliationCreate(ctx context.Context, name string, opts ...ca.AffiliationOpt) error {
var (
reqUrl string
err error
)
u := url.Values{}
for _, opt := range opts {
if err = opt(&u); err != nil {
return errors.Wrap(err, `failed to apply option`)
}
}
if v := u.Encode(); v == `` {
reqUrl = fmt.Sprintf(endpointAffiliationCreate, c.config.Host, ``)
} else {
reqUrl = fmt.Sprintf(endpointAffiliationCreate, c.config.Host, `?`+v)
}
reqBytes, err := json.Marshal(ca.AddAffiliationRequest{Name: name})
if err != nil {
return errors.Wrap(err, `failed to marshal JSON request`)
}
req, err := http.NewRequest(http.MethodPost, reqUrl, bytes.NewBuffer(reqBytes))
if err != nil {
return errors.Wrap(err, `failed to create request`)
}
if err = c.setAuthToken(req, reqBytes); err != nil {
return errors.Wrap(err, `failed to set auth token`)
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return errors.Wrap(err, `failed to process request`)
}
var affiliationCreateResponse ca.ResponseAffiliationCreate
if err = c.processResponse(resp, &affiliationCreateResponse, http.StatusCreated); err != nil {
return err
}
return nil
}
func (c *core) AffiliationDelete(ctx context.Context, name string, opts ...ca.AffiliationOpt) ([]ca.Identity, []ca.Affiliation, error) {
var (
reqUrl string
err error
)
u := url.Values{}
for _, opt := range opts {
if err = opt(&u); err != nil {
return nil, nil, errors.Wrap(err, `failed to apply option`)
}
}
if v := u.Encode(); v == `` {
reqUrl = fmt.Sprintf(endpointAffiliationDelete, c.config.Host, name)
} else {
reqUrl = fmt.Sprintf(endpointAffiliationDelete, c.config.Host, name+`?`+v)
}
req, err := http.NewRequest(http.MethodDelete, reqUrl, nil)
if err != nil {
return nil, nil, errors.Wrap(err, `failed to create request`)
}
if err = c.setAuthToken(req, nil); err != nil {
return nil, nil, errors.Wrap(err, `failed to set auth token`)
}
resp, err := c.client.Do(req.WithContext(ctx))
if err != nil {
return nil, nil, errors.Wrap(err, `failed to process request`)
}
var affiliationDeleteResponse ca.ResponseAffiliationDelete
if err = c.processResponse(resp, &affiliationDeleteResponse, http.StatusOK); err != nil {
return nil, nil, err
}
return affiliationDeleteResponse.Identities, affiliationDeleteResponse.Affiliations, nil
}