This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
action_xpub.go
128 lines (99 loc) · 3.16 KB
/
action_xpub.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
package bux
import (
"context"
"github.com/mrz1836/go-datastore"
)
// NewXpub will parse the xPub and save it into the Datastore
//
// xPubKey is the raw public xPub
// opts are options and can include "metadata"
func (c *Client) NewXpub(ctx context.Context, xPubKey string, opts ...ModelOps) (*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "new_xpub")
// Create the model & set the default options (gives options from client->model)
xPub := newXpub(
xPubKey, c.DefaultModelOptions(append(opts, New())...)...,
)
// Save the model
if err := xPub.Save(ctx); err != nil {
return nil, err
}
// Return the created model
return xPub, nil
}
// GetXpub will get an existing xPub from the Datastore
//
// xPubKey is the raw public xPub
func (c *Client) GetXpub(ctx context.Context, xPubKey string) (*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_xpub")
// Attempt to get from cache or datastore
xPub, err := getXpubWithCache(ctx, c, xPubKey, "", c.DefaultModelOptions()...)
if err != nil {
return nil, err
}
// Return the model
return xPub, nil
}
// GetXpubByID will get an existing xPub from the Datastore
//
// xPubID is the hash of the xPub
func (c *Client) GetXpubByID(ctx context.Context, xPubID string) (*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_xpub_by_id")
// Attempt to get from cache or datastore
xPub, err := getXpubWithCache(ctx, c, "", xPubID, c.DefaultModelOptions()...)
if err != nil {
return nil, err
}
// Return the model
return xPub, nil
}
// UpdateXpubMetadata will update the metadata in an existing xPub
//
// xPubID is the hash of the xP
func (c *Client) UpdateXpubMetadata(ctx context.Context, xPubID string, metadata Metadata) (*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "update_xpub_by_id")
// Get the xPub
xPub, err := c.GetXpubByID(ctx, xPubID)
if err != nil {
return nil, err
}
// Update the metadata
xPub.UpdateMetadata(metadata)
// Save the model
if err = xPub.Save(ctx); err != nil {
return nil, err
}
// Return the model
return xPub, nil
}
// GetXPubs gets all xpubs matching the conditions
func (c *Client) GetXPubs(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, queryParams *datastore.QueryParams, opts ...ModelOps) ([]*Xpub, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_destinations")
// Get the count
xPubs, err := getXPubs(
ctx, metadataConditions, conditions, queryParams, c.DefaultModelOptions(opts...)...,
)
if err != nil {
return nil, err
}
return xPubs, nil
}
// GetXPubsCount gets a count of all xpubs matching the conditions
func (c *Client) GetXPubsCount(ctx context.Context, metadataConditions *Metadata,
conditions *map[string]interface{}, opts ...ModelOps) (int64, error) {
// Check for existing NewRelic transaction
ctx = c.GetOrStartTxn(ctx, "get_destinations")
// Get the count
count, err := getXPubsCount(
ctx, metadataConditions, conditions, c.DefaultModelOptions(opts...)...,
)
if err != nil {
return 0, err
}
return count, nil
}