-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker_service.go
158 lines (133 loc) · 3.26 KB
/
worker_service.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package adp
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
type ADPWorkerService struct {
Authenticator ADPAuthenticationSystem
}
type ADPWorkerEmployeeResponse struct {
Workers []*Worker `json:"workers"`
}
func NewADPWorkerService(authenticator ADPAuthenticationSystem) ADPWorkerService {
return ADPWorkerService{
Authenticator: authenticator,
}
}
func (a *ADPWorkerService) GetWorker(aoid string) (*ADPWorkerEmployeeResponse, error) {
url := fmt.Sprintf("hr/v2/workers/%s", aoid)
body, err := a.MakeRequest(http.MethodGet, url)
if err != nil {
return nil, err
}
response := ADPWorkerEmployeeResponse{}
if err := json.NewDecoder(body).Decode(&response); err != nil {
return nil, err
}
return &response, nil
}
func (a *ADPWorkerService) ListWorkersAsync(reciever chan<- *Worker, resultLimit int) {
errChan := make(chan error)
closeChan := make(chan struct{})
defer close(reciever)
defer close(errChan)
defer close(closeChan)
go func() {
top := resultLimit
skip := 0
for {
response, err := a.makeWorkerAPIRequest(top, skip)
if err != nil {
errChan <- err
}
if len(response.Workers) == 0 {
closeChan <- struct{}{}
}
for _, worker := range response.Workers {
reciever <- worker
}
skip += resultLimit
}
}()
for {
select {
case err := <-errChan:
log.Fatal(err)
case <-closeChan:
return
}
}
}
func (a *ADPWorkerService) makeWorkerAPIRequest(top, skip int) (*ADPWorkerEmployeeResponse, error) {
response := &ADPWorkerEmployeeResponse{}
body, err := a.MakeRequest(http.MethodGet, fmt.Sprintf("hr/v2/workers?$skip=%d&top=%d", skip, top))
if err != nil {
return nil, err
}
if err := json.NewDecoder(body).Decode(response); err != nil && err != io.EOF {
return nil, err
}
return response, nil
}
func (a *ADPWorkerService) ListWorkers() (*ADPWorkerEmployeeResponse, error) {
top := 100
skip := 0
workers := ADPWorkerEmployeeResponse{
Workers: []*Worker{},
}
for {
response, err := a.makeWorkerAPIRequest(top, skip)
if err != nil {
return nil, err
}
if len(response.Workers) == 0 {
break
}
workers.Workers = append(workers.Workers, response.Workers...)
skip += 100
}
return &workers, nil
}
func (a *ADPWorkerService) GetWorkerImages(aoid, filePath string) error {
body, err := a.MakeRequest(http.MethodGet, fmt.Sprintf("hr/v2/workers/%s/worker-images/photo", aoid))
if err != nil {
return err
}
pictureFile, err := os.Open(filePath)
if err != nil {
return err
}
defer pictureFile.Close()
_, err = io.Copy(pictureFile, body)
if err != nil {
return err
}
return nil
}
func (a *ADPWorkerService) MakeRequest(method string, path string) (io.ReadCloser, error) {
baseUrl := "https://api.adp.com"
request, err := http.NewRequest(method, fmt.Sprintf("%s/%s", baseUrl, path), nil)
if err != nil {
return nil, err
}
a.Authenticator.SetRequestAuthorizationHeader(request)
if method == http.MethodGet {
request.Header.Set("Content-Type", "application/json")
}
client, err := a.Authenticator.NewHttpClient()
if err != nil {
return nil, err
}
resp, err := client.Do(request)
if err != nil {
return nil, err
}
if !isValidResponseStatusCode(resp) {
return nil, fmt.Errorf("invalid request. status code: %d", resp.StatusCode)
}
return resp.Body, nil
}