forked from ctreminiom/go-atlassian
/
task_impl.go
91 lines (73 loc) · 2.32 KB
/
task_impl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package internal
import (
"context"
"fmt"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service"
"github.com/ctreminiom/go-atlassian/service/jira"
"net/http"
)
func NewTaskService(client service.Client, version string) (*TaskService, error) {
if version == "" {
return nil, model.ErrNoVersionProvided
}
return &TaskService{
internalClient: &internalTaskServiceImpl{c: client, version: version},
}, nil
}
type TaskService struct {
internalClient jira.TaskConnector
}
// Get returns the status of a long-running asynchronous task.
//
// When a task has finished, this operation returns the JSON blob applicable to the task.
//
// See the documentation of the operation that created the task for details.
//
// Task details are not permanently retained.
//
// GET /rest/api/{2-3}/task/{taskId}
//
// https://docs.go-atlassian.io/jira-software-cloud/tasks#get-task
func (t *TaskService) Get(ctx context.Context, taskId string) (*model.TaskScheme, *model.ResponseScheme, error) {
return t.internalClient.Get(ctx, taskId)
}
// Cancel cancels a task.
//
// POST /rest/api/{2-3}/task/{taskId}/cancel
//
// https://docs.go-atlassian.io/jira-software-cloud/tasks#cancel-task
func (t *TaskService) Cancel(ctx context.Context, taskId string) (*model.ResponseScheme, error) {
return t.internalClient.Cancel(ctx, taskId)
}
type internalTaskServiceImpl struct {
c service.Client
version string
}
func (i *internalTaskServiceImpl) Get(ctx context.Context, taskId string) (*model.TaskScheme, *model.ResponseScheme, error) {
if taskId == "" {
return nil, nil, model.ErrNoTaskIDError
}
endpoint := fmt.Sprintf("rest/api/%v/task/%v", i.version, taskId)
request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return nil, nil, err
}
task := new(model.TaskScheme)
response, err := i.c.Call(request, task)
if err != nil {
return nil, response, err
}
return task, response, nil
}
func (i *internalTaskServiceImpl) Cancel(ctx context.Context, taskId string) (*model.ResponseScheme, error) {
if taskId == "" {
return nil, model.ErrNoTaskIDError
}
endpoint := fmt.Sprintf("rest/api/%v/task/%v/cancel", i.version, taskId)
request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, nil)
if err != nil {
return nil, err
}
return i.c.Call(request, nil)
}