Skip to content

Commit

Permalink
feat: add configure
Browse files Browse the repository at this point in the history
  • Loading branch information
MartianGreed committed May 29, 2024
1 parent ef2ceae commit ea1c30b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
59 changes: 58 additions & 1 deletion sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"bytes"
"context"
"encoding/gob"
"encoding/json"
"errors"
"log/slog"
"net/http"
"os"
"regexp"
"time"
Expand Down Expand Up @@ -48,6 +50,13 @@ type (
}
)

var (
indexerToken = os.Getenv("INDEXER_TOKEN")
indexerUrl = os.Getenv("INDEXER_URL")
indexerApi = os.Getenv("INDEXER_API")
indexerApiKey = os.Getenv("INDEXER_API_KEY")
)

func (c Config) FilterByName(name string) Config {
var contracts []Contract
for _, contract := range c.Contracts {
Expand Down Expand Up @@ -78,10 +87,58 @@ func (c Contract) Call(ctx context.Context, client rpc.RpcProvider, fn string, c
return callResp, nil
}

func WithToken(t string) error {
indexerToken = t
return nil
}

func WithUrl(u string) error {
indexerUrl = u
return nil
}

func WithApi(a string) error {
indexerApi = a
return nil
}

func WithApiKey(k string) error {
indexerApiKey = k
return nil
}

func Configure(c Config) (RegisterResponse, error) {
slog.Debug("configure", "app_name", c.Appname)

var r RegisterResponse
client := http.DefaultClient

body, err := json.Marshal(c)
if err != nil {
return r, err
}
req, err := http.NewRequest("POST", indexerApi+"/configure", bytes.NewReader(body))
if err != nil {
return r, err
}

resp, err := client.Do(req)
if err != nil {
return r, err
}

err = json.NewDecoder(resp.Body).Decode(&r)
if err != nil {
return r, err
}
defer resp.Body.Close()
return r, nil
}

func RegisterHandler(n string, s string, cb ConsumerHandleFunc) (HandlerCancelFunc, error) {
slog.Debug("register handler", "app_name", n)

nc, err := nats.Connect(os.Getenv("NATS_URL"), nats.Token(os.Getenv("NATS_TOKEN")))
nc, err := nats.Connect(indexerUrl, nats.Token(indexerToken))
if err != nil {
slog.Error("failed to connect to nats", "error", err)
return nil, err
Expand Down
21 changes: 21 additions & 0 deletions sdk/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package sdk

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/NethermindEth/starknet.go/rpc"
Expand Down Expand Up @@ -75,3 +77,22 @@ func TestConfigCall(t *testing.T) {
assert.Equal(t, 0, len(resp))
assert.Equal(t, &rpc.RPCError{Code: -32603, Message: "Internal Error", Data: "Contract error"}, err)
}

func TestConfigure(t *testing.T) {
err := WithToken("test_token")
assert.Equal(t, nil, err)
assert.Equal(t, "test_token", indexerToken)

server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := `{ "app_name": "test_config", "hash": "test_hash" }`
_, _ = w.Write([]byte(response))
}))
defer server.Close()

_ = WithApi(server.URL)
conf, err := Configure(testConfigs)

assert.Equal(t, nil, err)
assert.Equal(t, "test_config", conf.AppName)
assert.Equal(t, "test_hash", conf.Hash)
}

0 comments on commit ea1c30b

Please sign in to comment.