Merge 'dev' into 'main'
This commit is contained in:
@@ -1,54 +1,45 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type GetRequest struct {
|
||||
AuditId string `url:"auditId"`
|
||||
}
|
||||
|
||||
func (trq GetRequest) Validate() error {
|
||||
if trq.AuditId == "" {
|
||||
return errors.New("validation-error: field AuditId can not be empty")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Tasks) Get(ctx context.Context, req GetRequest, options ...opts.DecortOpts) (*AsyncTask, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := "/tasks/get"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
taskRaw, err := t.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
task := &AsyncTask{}
|
||||
err = json.Unmarshal(taskRaw, task)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return task, nil
|
||||
|
||||
}
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type GetRequest struct {
|
||||
AuditID string `url:"auditId"`
|
||||
}
|
||||
|
||||
func (trq GetRequest) Validate() error {
|
||||
if trq.AuditID == "" {
|
||||
return errors.New("validation-error: field AuditID can not be empty")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t Tasks) Get(ctx context.Context, req GetRequest) (*AsyncTask, error) {
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
url := "/tasks/get"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
url = prefix + url
|
||||
taskRaw, err := t.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
task := &AsyncTask{}
|
||||
err = json.Unmarshal(taskRaw, task)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return task, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -1,41 +1,32 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/rudecs/decort-sdk/opts"
|
||||
"github.com/rudecs/decort-sdk/typed"
|
||||
)
|
||||
|
||||
type ListRequest struct {
|
||||
Page uint64 `url:"page"`
|
||||
Size uint64 `url:"size"`
|
||||
}
|
||||
|
||||
func (t Tasks) List(ctx context.Context, req ListRequest, options ...opts.DecortOpts) (TasksList, error) {
|
||||
url := "/tasks/list"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
option := opts.New(options)
|
||||
|
||||
if option != nil {
|
||||
if option.IsAdmin {
|
||||
prefix = "/" + option.AdminValue
|
||||
}
|
||||
}
|
||||
url = prefix + url
|
||||
taskListRaw, err := t.client.DecortApiCall(ctx, typed.POST, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
taskList := TasksList{}
|
||||
err = json.Unmarshal(taskListRaw, &taskList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return taskList, nil
|
||||
|
||||
}
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ListRequest struct {
|
||||
Page uint64 `url:"page"`
|
||||
Size uint64 `url:"size"`
|
||||
}
|
||||
|
||||
func (t Tasks) List(ctx context.Context, req ListRequest) (TasksList, error) {
|
||||
url := "/tasks/list"
|
||||
prefix := "/cloudapi"
|
||||
|
||||
url = prefix + url
|
||||
taskListRaw, err := t.client.DecortApiCall(ctx, http.MethodPost, url, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
taskList := TasksList{}
|
||||
err = json.Unmarshal(taskListRaw, &taskList)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return taskList, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -1,51 +1,51 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type TaskResult int
|
||||
|
||||
func (r *TaskResult) UnmarshalJSON(b []byte) error {
|
||||
if b[0] == '"' {
|
||||
b := b[1 : len(b)-1]
|
||||
if len(b) == 0 {
|
||||
*r = 0
|
||||
return nil
|
||||
}
|
||||
n, err := strconv.Atoi(string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*r = TaskResult(n)
|
||||
} else if b[0] == '[' {
|
||||
res := []interface{}{}
|
||||
if err := json.Unmarshal(b, &res); err != nil {
|
||||
return err
|
||||
}
|
||||
if n, ok := res[0].(float64); ok {
|
||||
*r = TaskResult(n)
|
||||
} else {
|
||||
return fmt.Errorf("could not unmarshal %v into int", res[0])
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//AsyncTask represents a long task completion status
|
||||
type AsyncTask struct {
|
||||
AuditID string `json:"auditId"`
|
||||
Completed bool `json:"completed"`
|
||||
Error string `json:"error"`
|
||||
Log []string `json:"log"`
|
||||
Result TaskResult `json:"result"`
|
||||
Stage string `json:"stage"`
|
||||
Status string `json:"status"`
|
||||
UpdateTime uint64 `json:"updateTime"`
|
||||
UpdatedTime uint64 `json:"updatedTime"`
|
||||
}
|
||||
|
||||
type TasksList []AsyncTask
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type TaskResult int
|
||||
|
||||
func (r *TaskResult) UnmarshalJSON(b []byte) error {
|
||||
if b[0] == '"' {
|
||||
b := b[1 : len(b)-1]
|
||||
if len(b) == 0 {
|
||||
*r = 0
|
||||
return nil
|
||||
}
|
||||
n, err := strconv.Atoi(string(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*r = TaskResult(n)
|
||||
} else if b[0] == '[' {
|
||||
res := []interface{}{}
|
||||
if err := json.Unmarshal(b, &res); err != nil {
|
||||
return err
|
||||
}
|
||||
if n, ok := res[0].(float64); ok {
|
||||
*r = TaskResult(n)
|
||||
} else {
|
||||
return fmt.Errorf("could not unmarshal %v into int", res[0])
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//AsyncTask represents a long task completion status
|
||||
type AsyncTask struct {
|
||||
AuditID string `json:"auditId"`
|
||||
Completed bool `json:"completed"`
|
||||
Error string `json:"error"`
|
||||
Log []string `json:"log"`
|
||||
Result TaskResult `json:"result"`
|
||||
Stage string `json:"stage"`
|
||||
Status string `json:"status"`
|
||||
UpdateTime uint64 `json:"updateTime"`
|
||||
UpdatedTime uint64 `json:"updatedTime"`
|
||||
}
|
||||
|
||||
type TasksList []AsyncTask
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"github.com/rudecs/decort-sdk/interfaces"
|
||||
)
|
||||
|
||||
type Tasks struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
func New(client interfaces.Caller) *Tasks {
|
||||
return &Tasks{
|
||||
client,
|
||||
}
|
||||
}
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"github.com/rudecs/decort-sdk/interfaces"
|
||||
)
|
||||
|
||||
type Tasks struct {
|
||||
client interfaces.Caller
|
||||
}
|
||||
|
||||
func New(client interfaces.Caller) *Tasks {
|
||||
return &Tasks{
|
||||
client,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user