Skip to content

Commit

Permalink
project access key support
Browse files Browse the repository at this point in the history
  • Loading branch information
pkieltyka committed Nov 24, 2023
1 parent 8b2c374 commit eacd35a
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 0 deletions.
28 changes: 28 additions & 0 deletions _examples/indexer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"context"
"log"

"github.com/0xsequence/go-sequence/indexer"
"github.com/davecgh/go-spew/spew"
)

func main() {
seqIndexer := indexer.NewIndexer("https://polygon-indexer.sequence.app", "kzk9Hdk0EJRoj9qgHWncKtHAAAAAAAAA")

accountAddress := "0x8e3E38fe7367dd3b52D1e281E4e8400447C8d8B9"
includeMetadata := true
metadataOptions := indexer.MetadataOptions{
VerifiedOnly: true,
}

_, tokenBalances, err := seqIndexer.GetTokenBalances(context.Background(), &accountAddress, nil, nil, &includeMetadata, &metadataOptions, nil)
if err != nil {
log.Fatal(err)
}

for _, tokenBalance := range tokenBalances {
spew.Dump(tokenBalance)
}
}
25 changes: 25 additions & 0 deletions _examples/metadata/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"context"
"log"

"github.com/0xsequence/go-sequence/metadata"
"github.com/davecgh/go-spew/spew"
)

func main() {
seqMetadata := metadata.NewMetadata("kzk9Hdk0EJRoj9qgHWncKtHAAAAAAAAA")

contractInfo, err := seqMetadata.GetContractInfo(context.Background(), "polygon", "0x631998e91476DA5B870D741192fc5Cbc55F5a52E")
if err != nil {
log.Fatal(err)
}
spew.Dump(contractInfo)

collectibleInfo, err := seqMetadata.GetTokenMetadata(context.Background(), "polygon", "0x631998e91476DA5B870D741192fc5Cbc55F5a52E", []string{"1", "2"})
if err != nil {
log.Fatal(err)
}
spew.Dump(collectibleInfo)
}
52 changes: 52 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package api

import (
"fmt"
"net/http"
)

type Options struct {
HTTPClient HTTPClient
JWTAuthToken string
APIServiceURL string
}

func NewAPI(projectAccessKey string, options ...Options) API {
opts := Options{}
if len(options) > 0 {
opts = options[0]
}

client := &httpclient{
client: opts.HTTPClient,
projectAccessKey: projectAccessKey,
}
if opts.HTTPClient == nil {
client.client = http.DefaultClient
}
if opts.JWTAuthToken != "" {
client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken)
}

apiServiceURL := "https://api.sequence.app"
if opts.APIServiceURL != "" {
apiServiceURL = opts.APIServiceURL
}
return NewAPIClient(apiServiceURL, client)
}

type httpclient struct {
client HTTPClient
jwtAuthHeader string
projectAccessKey string
}

func (c *httpclient) Do(req *http.Request) (*http.Response, error) {
if c.projectAccessKey != "" {
req.Header.Set("X-Access-Key", c.projectAccessKey)
}
if c.jwtAuthHeader != "" {
req.Header.Set("Authorization", c.jwtAuthHeader)
}
return c.client.Do(req)
}
47 changes: 47 additions & 0 deletions indexer/indexer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package indexer

import (
"fmt"
"net/http"
)

type Options struct {
HTTPClient HTTPClient
JWTAuthToken string
}

func NewIndexer(indexerServiceURL string, projectAccessKey string, options ...Options) Indexer {
opts := Options{}
if len(options) > 0 {
opts = options[0]
}

client := &httpclient{
client: opts.HTTPClient,
projectAccessKey: projectAccessKey,
}
if opts.HTTPClient == nil {
client.client = http.DefaultClient
}
if opts.JWTAuthToken != "" {
client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken)
}

return NewIndexerClient(indexerServiceURL, client)
}

type httpclient struct {
client HTTPClient
jwtAuthHeader string
projectAccessKey string
}

func (c *httpclient) Do(req *http.Request) (*http.Response, error) {
if c.projectAccessKey != "" {
req.Header.Set("X-Access-Key", c.projectAccessKey)
}
if c.jwtAuthHeader != "" {
req.Header.Set("Authorization", c.jwtAuthHeader)
}
return c.client.Do(req)
}
52 changes: 52 additions & 0 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package metadata

import (
"fmt"
"net/http"
)

type Options struct {
HTTPClient HTTPClient
JWTAuthToken string
MetadataServiceURL string
}

func NewMetadata(projectAccessKey string, options ...Options) Metadata {
opts := Options{}
if len(options) > 0 {
opts = options[0]
}

client := &httpclient{
client: opts.HTTPClient,
projectAccessKey: projectAccessKey,
}
if opts.HTTPClient == nil {
client.client = http.DefaultClient
}
if opts.JWTAuthToken != "" {
client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken)
}

metadataServiceURL := "https://metadata.sequence.app"
if opts.MetadataServiceURL != "" {
metadataServiceURL = opts.MetadataServiceURL
}
return NewMetadataClient(metadataServiceURL, client)
}

type httpclient struct {
client HTTPClient
jwtAuthHeader string
projectAccessKey string
}

func (c *httpclient) Do(req *http.Request) (*http.Response, error) {
if c.projectAccessKey != "" {
req.Header.Set("X-Access-Key", c.projectAccessKey)
}
if c.jwtAuthHeader != "" {
req.Header.Set("Authorization", c.jwtAuthHeader)
}
return c.client.Do(req)
}
47 changes: 47 additions & 0 deletions relayer/proto/relayer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package proto

import (
"fmt"
"net/http"
)

type Options struct {
HTTPClient HTTPClient
JWTAuthToken string
}

func NewRelayer(relayerServiceURL string, projectAccessKey string, options ...Options) Relayer {
opts := Options{}
if len(options) > 0 {
opts = options[0]
}

client := &httpclient{
client: opts.HTTPClient,
projectAccessKey: projectAccessKey,
}
if opts.HTTPClient == nil {
client.client = http.DefaultClient
}
if opts.JWTAuthToken != "" {
client.jwtAuthHeader = fmt.Sprintf("BEARER %s", opts.JWTAuthToken)
}

return NewRelayerClient(relayerServiceURL, client)
}

type httpclient struct {
client HTTPClient
jwtAuthHeader string
projectAccessKey string
}

func (c *httpclient) Do(req *http.Request) (*http.Response, error) {
if c.projectAccessKey != "" {
req.Header.Set("X-Access-Key", c.projectAccessKey)
}
if c.jwtAuthHeader != "" {
req.Header.Set("Authorization", c.jwtAuthHeader)
}
return c.client.Do(req)
}

0 comments on commit eacd35a

Please sign in to comment.