Skip to content

Commit ea171ea

Browse files
added cancel job
1 parent cd74736 commit ea171ea

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

jobs.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type GetJobOutputRequest GetJobRequest
7272

7373
// these have the same structure
7474
type DeleteJobRequest GetJobRequest
75+
type CancelJobRequest GetJobRequest
7576

7677
type DeleteJobResponseWithStatus struct {
7778
Response DeleteJobResponse
@@ -141,6 +142,16 @@ type DeleteJobResponse struct {
141142
Status string `json:"status"`
142143
}
143144

145+
type CancelJobResponse struct {
146+
ID string `json:"id"`
147+
Status string `json:"status"`
148+
}
149+
150+
type CancelJobResponseWithStatus struct {
151+
Response CancelJobResponse
152+
Status int
153+
}
154+
144155
type DeleteManyJobsResponseWithStatus struct {
145156
Response DeleteManyJobsResponse
146157
Status int
@@ -370,3 +381,35 @@ func (c *Client) DeleteJob(ctx context.Context, deleteJobRequest *DeleteJobReque
370381
Status: res.StatusCode,
371382
}, nil
372383
}
384+
385+
func (c *Client) CancelJob(ctx context.Context, cancelJobRequest *CancelJobRequest) (*CancelJobResponseWithStatus, error) {
386+
url := c.makeURL(fmt.Sprintf("%s/%s/status/cancel", jobsPath, cancelJobRequest.ID))
387+
388+
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, http.NoBody)
389+
if err != nil {
390+
return nil, err
391+
}
392+
393+
c.setHeaders(req)
394+
395+
res, err := c.client.Do(req)
396+
if err != nil {
397+
return nil, err
398+
}
399+
defer res.Body.Close()
400+
401+
body, err := io.ReadAll(res.Body)
402+
if err != nil {
403+
return nil, err
404+
}
405+
406+
var cancelJobResponse CancelJobResponse
407+
if err := json.Unmarshal(body, &cancelJobResponse); err != nil {
408+
return nil, err
409+
}
410+
411+
return &CancelJobResponseWithStatus{
412+
Response: cancelJobResponse,
413+
Status: res.StatusCode,
414+
}, nil
415+
}

jobs_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,42 @@ func TestGetJobOutputSuccess(t *testing.T) {
529529
t.Fatalf("unexpected diff: %s", diff)
530530
}
531531
}
532+
533+
func TestCancelJobSuccess(t *testing.T) {
534+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
535+
defer cancel()
536+
537+
defer gock.Off()
538+
539+
jobResponseMock := CancelJobResponse{
540+
ID: "some-id",
541+
Status: "canceled",
542+
}
543+
544+
mockJson, err := json.Marshal(&jobResponseMock)
545+
if err != nil {
546+
t.Fatal(err)
547+
}
548+
t.Logf("will mock response as: %s", mockJson)
549+
550+
newGock().
551+
Put(fmt.Sprintf("%s/some-id/status/cancel", jobsPath)).
552+
Reply(200).
553+
JSON(&jobResponseMock)
554+
555+
client := NewClient(myFakeEndpoint, myFakeAPIKey)
556+
jobResponseWithStatus, err := client.CancelJob(ctx, &CancelJobRequest{
557+
ID: "some-id",
558+
})
559+
if err != nil {
560+
t.Fatal(err)
561+
}
562+
563+
if jobResponseWithStatus.Status != http.StatusOK {
564+
t.Fatalf("unexpected status: %d", jobResponseWithStatus.Status)
565+
}
566+
567+
if diff := deep.Equal(jobResponseMock, jobResponseWithStatus.Response); len(diff) > 0 {
568+
t.Fatalf("unexpected diff: %s", diff)
569+
}
570+
}

0 commit comments

Comments
 (0)