-
Notifications
You must be signed in to change notification settings - Fork 4
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 #34 from absmach/manager-sdk
NOISSUE - Add SDK and tasks management on CLI
- Loading branch information
Showing
8 changed files
with
531 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ linters: | |
- interfacebloat | ||
- dupl | ||
- err113 | ||
- noctx | ||
|
||
linters-settings: | ||
gocritic: | ||
|
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
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,122 @@ | ||
package sdk | ||
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
const CTJSON string = "application/json" | ||
|
||
type PageMetadata struct { | ||
Offset uint64 `json:"offset"` | ||
Limit uint64 `json:"limit"` | ||
} | ||
|
||
type SDK interface { | ||
// CreateTask creates a new task. | ||
// | ||
// example: | ||
// task := sdk.Task{ | ||
// Name: "John Doe" | ||
// } | ||
// task, _ := sdk.CreateTask(task) | ||
// fmt.Println(task) | ||
CreateTask(task Task) (Task, error) | ||
|
||
// GetTask gets a task by id. | ||
// | ||
// example: | ||
// task, _ := sdk.GetTask("b1d10738-c5d7-4ff1-8f4d-b9328ce6f040") | ||
// fmt.Println(task) | ||
GetTask(id string) (Task, error) | ||
|
||
// ListTasks lists tasks. | ||
// | ||
// example: | ||
// taskPage, _ := sdk.ListTasks(0, 10) | ||
// fmt.Println(taskPage) | ||
ListTasks(offset uint64, limit uint64) (TaskPage, error) | ||
|
||
// UpdateTask updates a task. | ||
// | ||
// example: | ||
// task := sdk.Task{ | ||
// Name: "John Doe" | ||
// } | ||
// task, _ := sdk.UpdateTask(task) | ||
// fmt.Println(task) | ||
UpdateTask(task Task) (Task, error) | ||
|
||
// DeleteTask deletes a task. | ||
// | ||
// example: | ||
// task, _ := sdk.DeleteTask("b1d10738-c5d7-4ff1-8f4d-b9328ce6f040") | ||
// fmt.Println(task) | ||
DeleteTask(id string) error | ||
|
||
// StartTask starts a task. | ||
// | ||
// example: | ||
// task, _ := sdk.StartTask("b1d10738-c5d7-4ff1-8f4d-b9328ce6f040") | ||
// fmt.Println(task) | ||
StartTask(id string) error | ||
|
||
// StopTask stops a task. | ||
// | ||
// example: | ||
// task, _ := sdk.StopTask("b1d10738-c5d7-4ff1-8f4d-b9328ce6f040") | ||
// fmt.Println(task) | ||
StopTask(id string) error | ||
} | ||
|
||
type propSDK struct { | ||
managerURL string | ||
client *http.Client | ||
} | ||
|
||
type Config struct { | ||
ManagerURL string | ||
TLSVerification bool | ||
} | ||
|
||
func NewSDK(cfg Config) SDK { | ||
return &propSDK{ | ||
managerURL: cfg.ManagerURL, | ||
client: &http.Client{ | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
InsecureSkipVerify: !cfg.TLSVerification, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (sdk *propSDK) processRequest(method, reqURL string, data []byte, expectedRespCode int) ([]byte, error) { | ||
req, err := http.NewRequest(method, reqURL, bytes.NewReader(data)) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
req.Header.Add("Content-Type", CTJSON) | ||
|
||
resp, err := sdk.client.Do(req) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
|
||
if resp.StatusCode != expectedRespCode { | ||
return []byte{}, fmt.Errorf("unexpected response code: %d", resp.StatusCode) | ||
} | ||
|
||
return body, nil | ||
} |
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,142 @@ | ||
package sdk | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const tasksEndpoint = "/tasks" | ||
|
||
type Task struct { | ||
ID string `json:"id,omitempty"` | ||
Name string `json:"name"` | ||
State uint8 `json:"state,omitempty"` | ||
StartTime time.Time `json:"start_time"` | ||
FinishTime time.Time `json:"finish_time"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
} | ||
|
||
type TaskPage struct { | ||
Offset uint64 `json:"offset"` | ||
Limit uint64 `json:"limit"` | ||
Total uint64 `json:"total"` | ||
Tasks []Task `json:"tasks"` | ||
} | ||
|
||
func (sdk *propSDK) CreateTask(task Task) (Task, error) { | ||
data, err := json.Marshal(task) | ||
if err != nil { | ||
return Task{}, err | ||
} | ||
|
||
url := sdk.managerURL + tasksEndpoint | ||
|
||
body, err := sdk.processRequest(http.MethodPost, url, data, http.StatusCreated) | ||
if err != nil { | ||
return Task{}, err | ||
} | ||
|
||
var t Task | ||
if err := json.Unmarshal(body, &t); err != nil { | ||
return Task{}, err | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
func (sdk *propSDK) GetTask(id string) (Task, error) { | ||
url := sdk.managerURL + tasksEndpoint + "/" + id | ||
|
||
body, err := sdk.processRequest(http.MethodGet, url, nil, http.StatusOK) | ||
if err != nil { | ||
return Task{}, err | ||
} | ||
|
||
var t Task | ||
if err := json.Unmarshal(body, &t); err != nil { | ||
return Task{}, err | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
func (sdk *propSDK) ListTasks(offset, limit uint64) (TaskPage, error) { | ||
queries := make([]string, 0) | ||
if offset > 0 { | ||
queries = append(queries, fmt.Sprintf("offset=%d", offset)) | ||
} | ||
if limit > 0 { | ||
queries = append(queries, fmt.Sprintf("limit=%d", limit)) | ||
} | ||
query := "" | ||
if len(queries) > 0 { | ||
query = "?" + strings.Join(queries, "&") | ||
} | ||
url := sdk.managerURL + tasksEndpoint + query | ||
|
||
body, err := sdk.processRequest(http.MethodGet, url, nil, http.StatusOK) | ||
if err != nil { | ||
return TaskPage{}, err | ||
} | ||
|
||
var t TaskPage | ||
if err := json.Unmarshal(body, &t); err != nil { | ||
return TaskPage{}, err | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
func (sdk *propSDK) UpdateTask(task Task) (Task, error) { | ||
data, err := json.Marshal(task) | ||
if err != nil { | ||
return Task{}, err | ||
} | ||
url := sdk.managerURL + tasksEndpoint + "/" + task.ID | ||
|
||
body, err := sdk.processRequest(http.MethodPut, url, data, http.StatusOK) | ||
if err != nil { | ||
return Task{}, err | ||
} | ||
|
||
var t Task | ||
if err := json.Unmarshal(body, &t); err != nil { | ||
return Task{}, err | ||
} | ||
|
||
return t, nil | ||
} | ||
|
||
func (sdk *propSDK) DeleteTask(id string) error { | ||
url := sdk.managerURL + tasksEndpoint + "/" + id | ||
|
||
if _, err := sdk.processRequest(http.MethodDelete, url, nil, http.StatusNoContent); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (sdk *propSDK) StartTask(id string) error { | ||
url := fmt.Sprintf("%s/tasks/%s/start", sdk.managerURL, id) | ||
|
||
if _, err := sdk.processRequest(http.MethodPost, url, nil, http.StatusOK); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (sdk *propSDK) StopTask(id string) error { | ||
url := fmt.Sprintf("%s/tasks/%s/stop", sdk.managerURL, id) | ||
|
||
if _, err := sdk.processRequest(http.MethodPost, url, nil, http.StatusOK); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.