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
/
identity.go
69 lines (50 loc) · 1.58 KB
/
identity.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
package ca
import (
"context"
"fmt"
"net/http"
"github.com/pkg/errors"
"github.com/atomyze-ru/hlf-sdk-go/api/ca"
)
const (
endpointIdentityList = "%s/api/v1/identities"
endpointIdentityGet = "%s/api/v1/identities/%s"
)
func (c *core) IdentityList(ctx context.Context) ([]ca.Identity, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(endpointIdentityList, c.config.Host), nil)
if err != nil {
return nil, errors.Wrap(err, `failed to create request`)
}
req = req.WithContext(ctx)
if err = c.setAuthToken(req, nil); err != nil {
return nil, errors.Wrap(err, `failed to set auth token`)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, `failed to process request`)
}
var identityListResp ca.ResponseIdentityList
if err = c.processResponse(resp, &identityListResp, http.StatusOK); err != nil {
return nil, err
}
return identityListResp.Identities, nil
}
func (c *core) IdentityGet(ctx context.Context, enrollId string) (*ca.Identity, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(endpointIdentityGet, c.config.Host, enrollId), nil)
if err != nil {
return nil, errors.Wrap(err, `failed to create request`)
}
req = req.WithContext(ctx)
if err = c.setAuthToken(req, nil); err != nil {
return nil, errors.Wrap(err, `failed to set auth token`)
}
resp, err := c.client.Do(req)
if err != nil {
return nil, errors.Wrap(err, `failed to process request`)
}
var identity ca.Identity
if err = c.processResponse(resp, &identity, http.StatusOK); err != nil {
return nil, err
}
return &identity, nil
}