-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from celestiaorg/mojtaba/go-sdk
feat: add a simple sdk for golang
- Loading branch information
Showing
5 changed files
with
665 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# BitTwister SDK for Go | ||
|
||
The BitTwister SDK for Go provides a convenient interface to interact with the BitTwister tool, which applies network restrictions on a network interface, including bandwidth limitation, packet loss, latency, and jitter. | ||
|
||
## Installation | ||
|
||
To use this SDK, import it into your Go project: | ||
|
||
```bash | ||
go get -u github.com/celestiaorg/bittwister/sdk | ||
``` | ||
|
||
## Usage | ||
|
||
### Initialization | ||
|
||
First, import the SDK into your Go project: | ||
|
||
```go | ||
import "github.com/celestiaorg/bittwister/sdk" | ||
``` | ||
|
||
Next, create a client by specifying the base URL of where BitTwister is running e.g. _a container running on the same machine_: | ||
|
||
```go | ||
func main() { | ||
baseURL := "http://localhost:9007/api/v1" | ||
client := sdk.NewClient(baseURL) | ||
// Use the client for API requests | ||
} | ||
``` | ||
|
||
### Examples | ||
|
||
Bandwidth Service | ||
|
||
Start the Bandwidth service with a specified network interface and bandwidth limit: | ||
|
||
```go | ||
req := sdk.BandwidthStartRequest{ | ||
NetworkInterfaceName: "eth0", | ||
Limit: 100, | ||
} | ||
|
||
err := client.BandwidthStart(req) | ||
if err != nil { | ||
// Handle error | ||
} | ||
``` | ||
|
||
Stop the Bandwidth service: | ||
|
||
```go | ||
err := client.BandwidthStop() | ||
if err != nil { | ||
// Handle error | ||
} | ||
``` | ||
|
||
Retrieve the status of the Bandwidth service: | ||
|
||
```go | ||
status, err := client.BandwidthStatus() | ||
if err != nil { | ||
// Handle error | ||
} | ||
// Use status for further processing | ||
``` | ||
|
||
Similarly, you can use PacketlossStart, PacketlossStop, PacketlossStatus, LatencyStart, LatencyStop, LatencyStatus, and other functions provided by the SDK following similar usage patterns. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package sdk | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/celestiaorg/bittwister/api/v1" | ||
) | ||
|
||
func (c *Client) PacketlossStart(req api.PacketLossStartRequest) error { | ||
_, err := c.postResource("/packetloss/start", req) | ||
return err | ||
} | ||
|
||
func (c *Client) PacketlossStop() error { | ||
_, err := c.postResource("/packetloss/stop", nil) | ||
return err | ||
} | ||
|
||
func (c *Client) PacketlossStatus() (*api.MetaMessage, error) { | ||
return c.getServiceStatus("/packetloss/status") | ||
} | ||
|
||
func (c *Client) BandwidthStart(req api.BandwidthStartRequest) error { | ||
_, err := c.postResource("/bandwidth/start", req) | ||
return err | ||
} | ||
|
||
func (c *Client) BandwidthStop() error { | ||
_, err := c.postResource("/bandwidth/stop", nil) | ||
return err | ||
} | ||
|
||
func (c *Client) BandwidthStatus() (*api.MetaMessage, error) { | ||
return c.getServiceStatus("/bandwidth/status") | ||
} | ||
|
||
func (c *Client) LatencyStart(req api.LatencyStartRequest) error { | ||
_, err := c.postResource("/latency/start", req) | ||
return err | ||
} | ||
|
||
func (c *Client) LatencyStop() error { | ||
_, err := c.postResource("/latency/stop", nil) | ||
return err | ||
} | ||
|
||
func (c *Client) LatencyStatus() (*api.MetaMessage, error) { | ||
return c.getServiceStatus("/latency/status") | ||
} | ||
|
||
func (c *Client) AllServicesStatus() ([]api.ServiceStatus, error) { | ||
resp, err := c.getResource("/services/status") | ||
msgs := []api.ServiceStatus{} | ||
|
||
if err := json.Unmarshal(resp, &msgs); err != nil { | ||
return nil, err | ||
} | ||
return msgs, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package sdk | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/celestiaorg/bittwister/api/v1" | ||
) | ||
|
||
type Client struct { | ||
baseURL string | ||
httpClient *http.Client | ||
} | ||
|
||
func NewClient(baseURL string) *Client { | ||
return &Client{ | ||
baseURL: baseURL, | ||
httpClient: http.DefaultClient, | ||
} | ||
} | ||
|
||
func (c *Client) getResource(resPath string) ([]byte, error) { | ||
resp, err := c.httpClient.Get(c.baseURL + resPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unexpected status: %d", resp.StatusCode) | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return body, nil | ||
} | ||
|
||
func (c *Client) postResource(resPath string, requestBody interface{}) ([]byte, error) { | ||
requestBodyJSON, err := json.Marshal(requestBody) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
req, err := http.NewRequest("POST", c.baseURL+resPath, bytes.NewBuffer(requestBodyJSON)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { | ||
return nil, fmt.Errorf("unexpected status: %d", resp.StatusCode) | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return body, nil | ||
} | ||
|
||
func (c *Client) getServiceStatus(resPath string) (*api.MetaMessage, error) { | ||
resp, err := c.getResource(resPath) | ||
msg := &api.MetaMessage{} | ||
|
||
if err := json.Unmarshal(resp, msg); err != nil { | ||
return nil, err | ||
} | ||
return msg, err | ||
} |
Oops, something went wrong.