-
Notifications
You must be signed in to change notification settings - Fork 3
/
issuer.go
133 lines (109 loc) · 2.38 KB
/
issuer.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
package gpass
import (
"context"
"errors"
"github.com/sonda2208/gpass/walletobjects"
)
type Issuer struct {
IssuerID int64
c *Client
}
func (c *Client) Issuer(id int64) *Issuer {
return &Issuer{
IssuerID: id,
c: c,
}
}
func (c *Client) Issuers(ctx context.Context) ([]*Issuer, error) {
res, err := c.wos.Issuer.List().Context(ctx).Do()
if err != nil {
return nil, err
}
issuers := make([]*Issuer, len(res.Resources))
for i, iss := range res.Resources {
issuers[i] = toIssuer(iss, c)
}
return issuers, nil
}
func toIssuer(iss *walletobjects.Issuer, c *Client) *Issuer {
return &Issuer{
IssuerID: iss.IssuerId,
c: c,
}
}
func (iss *Issuer) Create(ctx context.Context, im *IssuerMetadata) error {
i, err := im.toWO()
if err != nil {
return err
}
_, err = iss.c.wos.Issuer.Insert(i).Context(ctx).Do()
if err != nil {
return err
}
return nil
}
func (iss *Issuer) Metadata(ctx context.Context) (*IssuerMetadata, error) {
i, err := iss.c.wos.Issuer.Get(iss.IssuerID).Context(ctx).Do()
if err != nil {
return nil, err
}
meta := woToIssuerMetadata(i)
if meta == nil {
return nil, errors.New("invalid metadata")
}
return meta, nil
}
type IssuerMetadata struct {
ContactInfo *IssuerContactInfo
HomepageURL string
Name string
}
func (im *IssuerMetadata) toWO() (*walletobjects.Issuer, error) {
i := &walletobjects.Issuer{}
if im == nil {
return i, nil
}
i.Name = im.Name
i.HomepageUrl = im.HomepageURL
i.ContactInfo = im.ContactInfo.toWO()
return i, nil
}
func woToIssuerMetadata(i *walletobjects.Issuer) *IssuerMetadata {
if i == nil {
return nil
}
meta := &IssuerMetadata{
ContactInfo: woToIssuerContactInfo(i.ContactInfo),
HomepageURL: i.HomepageUrl,
Name: i.Name,
}
return meta
}
type IssuerContactInfo struct {
AlertsEmails []string
Email string
Name string
Phone string
}
func (ici *IssuerContactInfo) toWO() *walletobjects.IssuerContactInfo {
i := &walletobjects.IssuerContactInfo{}
if ici == nil {
return nil
}
i.AlertsEmails = ici.AlertsEmails
i.Email = ici.Email
i.Name = ici.Name
i.Phone = ici.Phone
return i
}
func woToIssuerContactInfo(i *walletobjects.IssuerContactInfo) *IssuerContactInfo {
if i == nil {
return nil
}
return &IssuerContactInfo{
AlertsEmails: i.AlertsEmails,
Email: i.Email,
Name: i.Name,
Phone: i.Phone,
}
}