-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
91 lines (70 loc) · 2.9 KB
/
event.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
package openseaapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/xTransact/errx/v3"
"github.com/xTransact/openseaapi/openseaapiutils"
"github.com/xTransact/openseaapi/openseamodels"
)
// ListEventsByAccount gets a list of events for an account. The list will be paginated and include up to 100 events per page.
// DOC: https://docs.opensea.io/reference/list_events_by_account
func (c *client) ListEventsByAccount(ctx context.Context, payload *openseamodels.GetEventsByAccountPayload,
opts ...RequestOptionFn) (resp *openseamodels.AssetEventResponse, err error) {
o := new(requestOptions)
for _, apply := range opts {
apply(o)
}
// GET /api/v2/events/accounts/{address}
url := fmt.Sprintf("%s/api/v2/events/accounts/%s",
openseaapiutils.GetBaseURL(o.testnets), payload.Address)
return c.getEvents(ctx, payload, o.testnets, url)
}
// ListEventsByNft gets a list of events for a single NFT. The list will be paginated and include up to 100 events per page.
// DOC: https://docs.opensea.io/reference/list_events_by_nft
func (c *client) ListEventsByNft(ctx context.Context,
payload *openseamodels.GetEventsByNftPayload) (resp *openseamodels.AssetEventResponse, err error) {
// GET /api/v2/events/chain/{chain}/contract/{address}/nfts/{identifier}
url := fmt.Sprintf("%s/api/v2/events/chain/%s/contract/%s/nfts/%s",
openseaapiutils.GetBaseURLByChain(payload.Chain), payload.Chain.Value(), payload.Address, payload.Identifier)
return c.getEvents(ctx, payload, payload.Chain.IsTestNet(), url)
}
// ListEventsByCollection gets a list of events for a collection.
// The list will be paginated and include up to 100 events per page.
// DOC: https://docs.opensea.io/reference/list_events_by_collection
func (c *client) ListEventsByCollection(ctx context.Context, payload *openseamodels.GetEventsByCollectionPayload,
opts ...RequestOptionFn) (resp *openseamodels.AssetEventResponse, err error) {
o := new(requestOptions)
for _, apply := range opts {
apply(o)
}
// GET /api/v2/events/collection/{collection_slug}
url := fmt.Sprintf("%s/api/v2/events/collection/%s",
openseaapiutils.GetBaseURL(o.testnets), payload.CollectionSlug)
return c.getEvents(ctx, payload, o.testnets, url)
}
func (c *client) getEvents(ctx context.Context, payload Payloader, testnets bool, url string) (
*openseamodels.AssetEventResponse, error) {
if err := payload.Validate(); err != nil {
return nil, errx.Wrap(err, "invalid payload")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, errx.WithStack(err)
}
qs := payload.ToQuery()
if len(qs) > 0 {
req.URL.RawQuery = qs.Encode()
}
c.acceptJson(req)
body, err := c.doRequest(req, testnets)
if err != nil {
return nil, errx.WithStack(err)
}
resp := new(openseamodels.AssetEventResponse)
if err = json.Unmarshal(body, resp); err != nil {
return nil, errx.Wrap(err, "unmarshal response body")
}
return resp, nil
}