81 lines
2.1 KiB
Go
81 lines
2.1 KiB
Go
package contract
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/client"
|
|
"git.dragonchain.com/dragonchain/dragonchain-prime-sdk-go/models"
|
|
)
|
|
|
|
type ContractClient struct {
|
|
client *client.Client
|
|
}
|
|
|
|
func NewContractClient(c *client.Client) *ContractClient {
|
|
return &ContractClient{client: c}
|
|
}
|
|
|
|
func (cc *ContractClient) Create(req *models.SmartContractCreateRequest) (*models.SmartContract, error) {
|
|
var resp models.SmartContract
|
|
err := cc.client.Post("/api/v1/contract", models.ContentTypeJSON, req, &resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
func (cc *ContractClient) Get(contractID string) (*models.SmartContract, error) {
|
|
var resp models.SmartContract
|
|
path := fmt.Sprintf("/api/v1/contract/%s", contractID)
|
|
err := cc.client.Get(path, &resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
func (cc *ContractClient) List() (*models.ListResponse, error) {
|
|
var resp models.ListResponse
|
|
err := cc.client.Get("/api/v1/contract", &resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
func (cc *ContractClient) Update(contractID string, req *models.SmartContractUpdateRequest) (*models.SuccessResponse, error) {
|
|
var resp models.SuccessResponse
|
|
path := fmt.Sprintf("/api/v1/contract/%s", contractID)
|
|
err := cc.client.Put(path, models.ContentTypeJSON, req, &resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
func (cc *ContractClient) Upload(contractID string, filepath string) (*models.SuccessResponse, error) {
|
|
fileContent, err := os.ReadFile(filepath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read file: %w", err)
|
|
}
|
|
|
|
var resp models.SuccessResponse
|
|
path := fmt.Sprintf("/api/v1/contract/%s/upload", contractID)
|
|
err = cc.client.Put(path, "application/octet-stream", fileContent, &resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
func (cc *ContractClient) Delete(contractID string) (*models.SuccessResponse, error) {
|
|
var resp models.SuccessResponse
|
|
path := fmt.Sprintf("/api/v1/contract/%s", contractID)
|
|
err := cc.client.Delete(path, &resp)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|