Files
ark/pkg/client-sdk/grpc/grpc_client.go
Dusan Sekulic 1f42843a69 Add first version of client SDK (#193)
* Add gRPC, REST, and gRPC-Web clients for server access

This commit introduces clients for gRPC, REST, and gRPC-Web to access the server.

- gRPC client: Includes additional argument opts ...grpc.CallOption in the interface for future extensibility.
- REST client: Factory function accepts http.Client as an argument to allow user customization.
- gRPC-Web client: Added a Log method for fast debugging in JavaScript.

The decision to use different interfaces for each client type is to accommodate specific features and extensibility requirements for each protocol.

* remove grpc web

* generate rest

* use grpc sdk in CLI

* temp wasm

* ark sdk

* renaming

* pr review refactor

* pr review refactor

* walletStore & configStore

* ark sdk wasm wrapper

* handle event stream with rest

* wip on supporting rest

* store init

* simulate event stream with rest

* fix rest sdk wip

* Fix returning forfeit txs in round event

* wasm first working e2e example

* pr review refactor

* pr review refactor

* pr review refactor

* Fixes

---------

Co-authored-by: altafan <18440657+altafan@users.noreply.github.com>
2024-07-18 16:27:14 +02:00

64 lines
1.3 KiB
Go

package arkgrpcclient
import (
"errors"
"fmt"
"strings"
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
var (
errAspUrlEmpty = errors.New("asp url is empty")
)
func New(aspUrl string) (ArkGrpcClient, func(), error) {
if aspUrl == "" {
return nil, nil, errAspUrlEmpty
}
creds := insecure.NewCredentials()
port := 80
if strings.HasPrefix(aspUrl, "https://") {
aspUrl = strings.TrimPrefix(aspUrl, "https://")
creds = credentials.NewTLS(nil)
port = 443
}
if !strings.Contains(aspUrl, ":") {
aspUrl = fmt.Sprintf("%s:%d", aspUrl, port)
}
conn, err := grpc.NewClient(aspUrl, grpc.WithTransportCredentials(creds))
if err != nil {
return nil, nil, err
}
closeFn := func() {
err := conn.Close()
if err != nil {
fmt.Printf("error closing connection: %s\n", err)
}
}
return &arkGrpcClient{conn: conn}, closeFn, nil
}
type ArkGrpcClient interface {
Admin() arkv1.AdminServiceClient
Service() arkv1.ArkServiceClient
}
type arkGrpcClient struct {
conn *grpc.ClientConn
}
func (a *arkGrpcClient) Admin() arkv1.AdminServiceClient {
return arkv1.NewAdminServiceClient(a.conn)
}
func (a *arkGrpcClient) Service() arkv1.ArkServiceClient {
return arkv1.NewArkServiceClient(a.conn)
}