2023-03-31 11:49:03 +02:00
|
|
|
package request
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RequestService struct {
|
|
|
|
api string
|
|
|
|
apiKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRequestService(api, apiKey string) RequestService {
|
|
|
|
return RequestService{
|
|
|
|
api: api,
|
|
|
|
apiKey: apiKey,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPayload(employeeIDList []string) (string, error) {
|
|
|
|
employees := strings.Builder{}
|
|
|
|
_, err := employees.Write([]byte(`<EmployeeWorkInformationQuery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><EmployeeNumbers>`))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error while write headers: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, employeeID := range employeeIDList {
|
|
|
|
employees.Write([]byte("<string>" + employeeID + "</string>"))
|
|
|
|
}
|
|
|
|
_, err = employees.Write([]byte("</EmployeeNumbers></EmployeeWorkInformationQuery>"))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error while write footer: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return employees.String(), nil
|
|
|
|
}
|
|
|
|
|
2023-04-10 11:43:13 +02:00
|
|
|
func (r RequestService) EmployeeWorkInformation(data *[]byte, employeeIDList []string, from, to string) error {
|
2023-03-31 11:49:03 +02:00
|
|
|
url := r.api + "/EmployeeWorkInformation/Search/" + from + "/" + to + "/"
|
|
|
|
|
|
|
|
stringPayload, err := getPayload(employeeIDList)
|
|
|
|
if err != nil {
|
2023-04-10 11:43:13 +02:00
|
|
|
return fmt.Errorf("getPayload: %s", err)
|
2023-03-31 11:49:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
payload := strings.NewReader(stringPayload)
|
|
|
|
client := &http.Client{}
|
2023-04-05 12:18:07 +02:00
|
|
|
req, err := http.NewRequest("POST", url, payload)
|
2023-03-31 11:49:03 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2023-04-10 11:43:13 +02:00
|
|
|
return fmt.Errorf("http.NewRequest: %s", err)
|
2023-03-31 11:49:03 +02:00
|
|
|
}
|
|
|
|
req.Header.Add("Cache-Control", "no-cache")
|
|
|
|
req.Header.Add("Authorization", r.apiKey)
|
|
|
|
req.Header.Add("Content-Type", "application/xml")
|
|
|
|
|
|
|
|
res, err := client.Do(req)
|
|
|
|
if err != nil {
|
2023-04-10 11:43:13 +02:00
|
|
|
return fmt.Errorf("client.Do: %s", err)
|
2023-03-31 11:49:03 +02:00
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2023-04-10 11:43:13 +02:00
|
|
|
for {
|
|
|
|
if len(*data) == cap(*data) {
|
|
|
|
// Add more capacity (let append pick how much).
|
|
|
|
*data = append(*data, 0)[:len(*data)]
|
|
|
|
}
|
|
|
|
n, err := res.Body.Read((*data)[len(*data):cap(*data)])
|
|
|
|
*data = (*data)[:len(*data)+n]
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2023-03-31 11:49:03 +02:00
|
|
|
}
|
|
|
|
|
2023-04-10 11:43:13 +02:00
|
|
|
// data, err = io.ReadAll(res.Body)
|
|
|
|
// if err != nil {
|
|
|
|
// return fmt.Errorf("ioutil.ReadAll: %s", err)
|
|
|
|
// }
|
2023-03-31 11:49:03 +02:00
|
|
|
|
2023-04-10 11:43:13 +02:00
|
|
|
//log.Println("readed", string(body))
|
|
|
|
return nil
|
2023-03-31 11:49:03 +02:00
|
|
|
}
|