Skip to content

Commit

Permalink
Rewrite the client.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Apr 29, 2024
1 parent fba8e21 commit eafda38
Show file tree
Hide file tree
Showing 14 changed files with 815 additions and 937 deletions.
2 changes: 1 addition & 1 deletion agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func New(cfg Config) (*Agent, error) {
if cfg.Identity != nil {
id = cfg.Identity
}
var logger Logger = &defaultLogger{}
var logger Logger = new(NoopLogger)
if cfg.Logger != nil {
logger = cfg.Logger
}
Expand Down
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func NewClient(cfg ClientConfig) Client {
return Client{
client: http.Client{},
config: cfg,
logger: &defaultLogger{},
logger: new(NoopLogger),
}
}

// NewClientWithLogger creates a new client based on the given configuration and logger.
func NewClientWithLogger(cfg ClientConfig, logger Logger) Client {
if logger == nil {
logger = &defaultLogger{}
logger = new(NoopLogger)
}
return Client{
client: http.Client{},
Expand Down
4 changes: 2 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ type Logger interface {
Printf(format string, v ...any)
}

type defaultLogger struct{}
type NoopLogger struct{}

func (l defaultLogger) Printf(format string, v ...any) {}
func (l NoopLogger) Printf(format string, v ...any) {}
48 changes: 0 additions & 48 deletions pocketic/client.go

This file was deleted.

60 changes: 60 additions & 0 deletions pocketic/management.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pocketic

import (
"fmt"
"net/http"

"github.com/aviate-labs/agent-go/candid/idl"
"github.com/aviate-labs/agent-go/ic"
ic0 "github.com/aviate-labs/agent-go/ic/ic"
"github.com/aviate-labs/agent-go/principal"
)

func (pic PocketIC) AddCycles(canisterID principal.Principal, amount int) (int, error) {
var resp struct {
Cycles int `json:"cycles"`
}
if err := pic.do(
http.MethodPost,
fmt.Sprintf("%s/update/add_cycles", pic.instanceURL()),
RawAddCycles{
Amount: amount,
CanisterID: canisterID.Raw,
},
&resp,
); err != nil {
return 0, err
}
return resp.Cycles, nil
}

// CreateCanister creates a canister with default settings as the anonymous principal.
func (pic PocketIC) CreateCanister() (*principal.Principal, error) {
payload, err := idl.Marshal([]any{ProvisionalCreateCanisterArgument{}})
if err != nil {
return nil, err
}
raw, err := pic.updateCallWithEP(
ic.MANAGEMENT_CANISTER_PRINCIPAL,
new(RawEffectivePrincipalNone),
principal.AnonymousID,
"provisional_create_canister_with_cycles",
payload,
)
if err != nil {
return nil, err
}
var resp struct {
CanisterID principal.Principal `ic:"canister_id"`
}
if err := idl.Unmarshal(raw, []any{&resp}); err != nil {
return nil, err
}
return &resp.CanisterID, nil
}

type ProvisionalCreateCanisterArgument struct {
Settings *ic0.CanisterSettings `ic:"settings"`
SpecifiedID *principal.Principal `ic:"specified_id"`
Amount *int `ic:"amount"`
}
23 changes: 23 additions & 0 deletions pocketic/none.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pocketic

import (
"encoding/json"
"fmt"
)

type None struct{}

func (n None) MarshalJSON() ([]byte, error) {
return json.Marshal("None")
}

func (n None) UnmarshalJSON(bytes []byte) error {
var s string
if err := json.Unmarshal(bytes, &s); err != nil {
return err
}
if s != "None" {
return fmt.Errorf("expected None, got %s", s)
}
return nil
}
21 changes: 21 additions & 0 deletions pocketic/none_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pocketic

import (
"encoding/json"
"testing"
)

func TestNone(t *testing.T) {
var none None
raw, err := json.Marshal(none)
if err != nil {
t.Fatal(err)
}
if string(raw) != "\"None\"" {
t.Fatalf("expected None, got %s", string(raw))
}
var decoded None
if err := json.Unmarshal(raw, &decoded); err != nil {
t.Fatal(err)
}
}
Loading

0 comments on commit eafda38

Please sign in to comment.