forked from intercom/intercom-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
job_api.go
47 lines (41 loc) · 1.07 KB
/
job_api.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
package intercom
import (
"encoding/json"
"fmt"
"gopkg.in/intercom/intercom-go.v2/interfaces"
)
// JobRepository defines the interface for working with Jobs.
type JobRepository interface {
save(job *JobRequest) (JobResponse, error)
find(id string) (JobResponse, error)
}
// JobAPI implements TagRepository
type JobAPI struct {
httpClient interfaces.HTTPClient
}
func (api JobAPI) save(job *JobRequest) (JobResponse, error) {
for i := range job.Items {
obj := job.Items[i].Data
switch obj.(type) {
case *User:
user := obj.(*User)
job.Items[i].Data = RequestUserMapper{}.ConvertUser(user)
}
}
savedJob := JobResponse{}
data, err := api.httpClient.Post(fmt.Sprintf("/bulk/%s", job.bulkType), job)
if err != nil {
return savedJob, err
}
err = json.Unmarshal(data, &savedJob)
return savedJob, err
}
func (api JobAPI) find(id string) (JobResponse, error) {
fetchedJob := JobResponse{}
data, err := api.httpClient.Get(fmt.Sprintf("/jobs/%s", id), nil)
if err != nil {
return fetchedJob, err
}
err = json.Unmarshal(data, &fetchedJob)
return fetchedJob, err
}