Skip to content

swanchain/go-swan-sdk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GO SWAN SDK

Made by Swan Chain Chat on discord

go-swan-sdk is a comprehensive toolkit designed to facilitate seamless interactions with the SwanChain API.

Table Of Contents

Getting started

Go Version

go-swan-sdk requires Go version 1.21 or above.

Swan API Key

To use swan-sdk, an Swan API key is required. Steps to get an API Key:

Using go-swan-sdk

With Go's module support, go [build|run|test] automatically fetches the necessary dependencies when you add importin your project:

import "github.com/swanchain/go-swan-sdk"

To update the SDK use go get -u to retrieve the latest version of the SDK:

go get -u github.com/swanchain/go-swan-sdk

Quickstart

To use go-swan-sdk, you must first import it, and you can create and deploy instance applications quickly.

package main

import (
	"github.com/swanchain/go-swan-sdk"
	"log"
	"time"
)

func main() {
	client, err := swan.NewAPIClient("<YOUR_API_KEY>")
	if err != nil {
		log.Fatalf("failed to init swan client, error: %v \n", err)
	}
	task, err := client.CreateTask(&swan.CreateTaskReq{
		PrivateKey:   "<PRIVATE_KEY>",
		RepoUri:      "https://github.com/swanchain/awesome-swanchain/tree/main/hello_world",
		Duration:     2 * time.Hour,
		InstanceType: "C1ae.small",
	})
	taskUUID := task.Task.UUID

	// Get task deployment info
	resp, err := client.TaskInfo(taskUUID)
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("task info: %+v \n", resp)

	//Get application instances URL
	appUrls, err := client.GetRealUrl(taskUUID)
	if err != nil {
		log.Fatalln(err)
	}
	log.Printf("app urls: %v \n", appUrls)
}

Usage

client, err := swan.NewAPIClient("<SWAN_API_KEY>")

Through InstanceResources you can get a list of available instance resources including their region information. You can select one you want to use.

instances, err := swan.InstanceResources(true)

Note: All Instance type list can be found here

Deploy a application, if you have set PrivateKey, this task will be payed automaiclly, and deploy to computing providers on Swan Chain Network:

task, err := client.CreateTask(&CreateTaskReq{
    PrivateKey:   "<YOUR_WALLET_ADDRESS_PRIVATE_KEY>",
    RepoUri:      "<YOUR_PROJECT_GITHUB_URL>",
    Duration:      2 * time.Hour,
    InstanceType: "C1ae.small", 
})

taskUUID := task.Task.UUID
log.Printf("taskUUID: %v", taskUUID)

You can easily get the deployed application instances for an existing task.

// Get application instances URL
appUrls, err := client.GetRealUrl("<TASK_UUID>")
if err != nil {
	log.Fatalln(err)
}
log.Printf("app urls: %v", appUrls)

A sample output:

['https://krfswstf2g.anlu.loveismoney.fun', 'https://l2s5o476wf.cp162.bmysec.xyz', 'https://e2uw19k9uq.cp5.node.study']

It shows that this task has three applications. Visit the URL in the web browser you will view the application's information if it is running correctly.

RenewTask extends the duration of the task before completed

resp, err := client.RenewTask("<TASK_UUID>", <Duration>,"<PRIVATE_KEY>")

You can early terminate an existing task and its application instances. By terminating task, you will stop all the related running application instances and thus you will get refund of the remaining task duration.

resp, err := client.TerminateTask("<TASK_UUID>")

You can get the task details by the taskUUID

resp, err := client.TaskInfo("<TASK_UUID>")

You can get all tasks deployed from one wallet address

total, resp, err := client.Tasks(&TaskQueryReq{
    Wallet: "<WALLET_ADDRESS>",
    Page:   0,
    Size:   10,
})

More Samples

For more pratical samples, consult go-swan-sdk-samples.

More Resources

More resources about swan SDK can be found

License

The go-swan-sdk is released under the MIT license, details of which can be found in the LICENSE file.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages