Skip to content

Commit

Permalink
Expose client.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed May 15, 2024
1 parent 94fe9b1 commit 6e47e5f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
23 changes: 14 additions & 9 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ func (a Agent) Call(canisterID principal.Principal, methodName string, args []an
return call.CallAndWait(values...)
}

// CreateCall creates a new call to the given canister and method.
// Client returns the underlying Client of the Agent.
func (a Agent) Client() *Client {
return &a.client
}

// CreateCall creates a new Call to the given canister and method.
func (a *Agent) CreateCall(canisterID principal.Principal, methodName string, args ...any) (*Call, error) {
rawArgs, err := idl.Marshal(args)
if err != nil {
Expand Down Expand Up @@ -193,7 +198,7 @@ func (a *Agent) CreateCall(canisterID principal.Principal, methodName string, ar
}, nil
}

// CreateQuery creates a new query to the given canister and method.
// CreateQuery creates a new Query to the given canister and method.
func (a *Agent) CreateQuery(canisterID principal.Principal, methodName string, args ...any) (*Query, error) {
rawArgs, err := idl.Marshal(args)
if err != nil {
Expand Down Expand Up @@ -358,7 +363,7 @@ func (a Agent) Sender() principal.Principal {
}

func (a Agent) call(ecID principal.Principal, data []byte) ([]byte, error) {
return a.client.call(ecID, data)
return a.client.Call(ecID, data)
}

func (a Agent) expiryDate() uint64 {
Expand Down Expand Up @@ -405,7 +410,7 @@ func (a Agent) poll(ecID principal.Principal, requestID RequestID) ([]byte, erro
}

func (a Agent) query(canisterID principal.Principal, data []byte) (*Response, error) {
resp, err := a.client.query(canisterID, data)
resp, err := a.client.Query(canisterID, data)
if err != nil {
return nil, err
}
Expand All @@ -414,7 +419,7 @@ func (a Agent) query(canisterID principal.Principal, data []byte) (*Response, er
}

func (a Agent) readState(ecID principal.Principal, data []byte) (map[string][]byte, error) {
resp, err := a.client.readState(ecID, data)
resp, err := a.client.ReadState(ecID, data)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -453,7 +458,7 @@ func (a Agent) sign(request Request) (*RequestID, []byte, error) {
return &requestID, data, nil
}

// Call is an intermediate representation of a call to a canister.
// Call is an intermediate representation of a Call to a canister.
type Call struct {
a *Agent
methodName string
Expand All @@ -477,7 +482,7 @@ func (c Call) CallAndWait(values ...any) error {
return c.Wait(values...)
}

// Wait waits for the result of the call and unmarshals it into the given values.
// Wait waits for the result of the Call and unmarshals it into the given values.
func (c Call) Wait(values ...any) error {
raw, err := c.a.poll(c.effectiveCanisterID, c.requestID)
if err != nil {
Expand All @@ -486,7 +491,7 @@ func (c Call) Wait(values ...any) error {
return idl.Unmarshal(raw, values)
}

// WithEffectiveCanisterID sets the effective canister ID for the call.
// WithEffectiveCanisterID sets the effective canister ID for the Call.
func (c *Call) WithEffectiveCanisterID(canisterID principal.Principal) *Call {
c.effectiveCanisterID = canisterID
return c
Expand All @@ -510,7 +515,7 @@ type Config struct {
PollTimeout time.Duration
}

// Query is an intermediate representation of a query to a canister.
// Query is an intermediate representation of a Query to a canister.
type Query struct {
a *Agent
methodName string
Expand Down
38 changes: 19 additions & 19 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,7 @@ func NewClientWithLogger(cfg ClientConfig, logger Logger) Client {
}
}

// Status returns the status of the IC.
func (c Client) Status() (*Status, error) {
raw, err := c.get("/api/v2/status")
if err != nil {
return nil, err
}
var status Status
return &status, cbor.Unmarshal(raw, &status)
}

func (c Client) call(canisterID principal.Principal, data []byte) ([]byte, error) {
func (c Client) Call(canisterID principal.Principal, data []byte) ([]byte, error) {
u := c.url(fmt.Sprintf("/api/v2/canister/%s/call", canisterID.Encode()))
c.logger.Printf("[CLIENT] CALL %s", u)
resp, err := c.client.Post(u, "application/cbor", bytes.NewBuffer(data))
Expand All @@ -73,6 +63,24 @@ func (c Client) call(canisterID principal.Principal, data []byte) ([]byte, error
}
}

func (c Client) Query(canisterID principal.Principal, data []byte) ([]byte, error) {
return c.post("query", canisterID, data)
}

func (c Client) ReadState(canisterID principal.Principal, data []byte) ([]byte, error) {
return c.post("read_state", canisterID, data)
}

// Status returns the status of the IC.
func (c Client) Status() (*Status, error) {
raw, err := c.get("/api/v2/status")
if err != nil {
return nil, err
}
var status Status
return &status, cbor.Unmarshal(raw, &status)
}

func (c Client) get(path string) ([]byte, error) {
c.logger.Printf("[CLIENT] GET %s", c.url(path))
resp, err := c.client.Get(c.url(path))
Expand All @@ -98,14 +106,6 @@ func (c Client) post(path string, canisterID principal.Principal, data []byte) (
}
}

func (c Client) query(canisterID principal.Principal, data []byte) ([]byte, error) {
return c.post("query", canisterID, data)
}

func (c Client) readState(canisterID principal.Principal, data []byte) ([]byte, error) {
return c.post("read_state", canisterID, data)
}

func (c Client) url(p string) string {
u := *c.config.Host
u.Path = path.Join(u.Path, p)
Expand Down

0 comments on commit 6e47e5f

Please sign in to comment.