generated from mrz1836/go-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcontacts_api.go
129 lines (110 loc) · 3.05 KB
/
contacts_api.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
package contacts
import (
"context"
"fmt"
"net/url"
"github.com/bitcoin-sv/spv-wallet-go-client/commands"
"github.com/bitcoin-sv/spv-wallet-go-client/internal/api/v1/queryparams"
"github.com/bitcoin-sv/spv-wallet-go-client/queries"
"github.com/bitcoin-sv/spv-wallet/models/filter"
"github.com/bitcoin-sv/spv-wallet/models/response"
"github.com/go-resty/resty/v2"
)
const (
route = "api/v1/contacts"
api = "User Contacts API"
)
type API struct {
url *url.URL
httpClient *resty.Client
}
func (a *API) Contacts(ctx context.Context, opts ...queries.QueryOption[filter.ContactFilter]) (*queries.ContactsPage, error) {
query := queries.NewQuery(opts...)
parser, err := queryparams.NewQueryParser(query)
if err != nil {
return nil, fmt.Errorf("failed to initialize query parser: %w", err)
}
params, err := parser.Parse()
if err != nil {
return nil, fmt.Errorf("failed to build user contacts query params: %w", err)
}
var result queries.ContactsPage
_, err = a.httpClient.
R().
SetContext(ctx).
SetResult(&result).
SetQueryParams(params.ParseToMap()).
Get(a.url.String())
if err != nil {
return nil, fmt.Errorf("HTTP response failure: %w", err)
}
return &result, nil
}
func (a *API) ContactWithPaymail(ctx context.Context, paymail string) (*response.Contact, error) {
var result response.Contact
_, err := a.httpClient.
R().
SetContext(ctx).
SetResult(&result).
Get(a.url.JoinPath(paymail).String())
if err != nil {
return nil, fmt.Errorf("HTTP response failure: %w", err)
}
return &result, nil
}
func (a *API) UpsertContact(ctx context.Context, cmd commands.UpsertContact) (*response.Contact, error) {
var result response.CreateContactResponse
_, err := a.httpClient.
R().
SetBody(cmd).
SetContext(ctx).
SetResult(&result).
Put(a.url.JoinPath(cmd.ContactPaymail).String())
if err != nil {
return nil, fmt.Errorf("HTTP response failure: %w", err)
}
return &response.Contact{
Model: result.Contact.Model,
ID: result.Contact.ID,
FullName: result.Contact.FullName,
Paymail: result.Contact.Paymail,
PubKey: result.Contact.PubKey,
Status: result.Contact.Status,
}, nil
}
func (a *API) RemoveContact(ctx context.Context, paymail string) error {
_, err := a.httpClient.
R().
SetContext(ctx).
Delete(a.url.JoinPath(paymail).String())
if err != nil {
return fmt.Errorf("HTTP response failure: %w", err)
}
return nil
}
func (a *API) ConfirmContact(ctx context.Context, paymail string) error {
_, err := a.httpClient.
R().
SetContext(ctx).
Post(a.url.JoinPath(paymail, "confirmation").String())
if err != nil {
return fmt.Errorf("HTTP response failure: %w", err)
}
return nil
}
func (a *API) UnconfirmContact(ctx context.Context, paymail string) error {
_, err := a.httpClient.
R().
SetContext(ctx).
Delete(a.url.JoinPath(paymail, "confirmation").String())
if err != nil {
return fmt.Errorf("HTTP response failure: %w", err)
}
return nil
}
func NewAPI(url *url.URL, httpClient *resty.Client) *API {
return &API{
url: url.JoinPath(route),
httpClient: httpClient,
}
}