an updated and hopefully faster version of the ST Toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

162 lines
4.0 KiB

package api
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strconv"
"time"
)
// SearchJobsParams defines parameters for searching jobs
type SearchJobsParams struct {
Query string
Status string
StartDate time.Time
EndDate time.Time
Page int
Limit int
IncludeArchived bool
}
// SearchJobs searches for jobs based on the provided parameters
func (s *Session) SearchJobs(params SearchJobsParams) ([]map[string]interface{}, error) {
queryValues := url.Values{}
if params.Query != "" {
queryValues.Add("q", params.Query)
}
if params.Status != "" {
queryValues.Add("status", params.Status)
}
if !params.StartDate.IsZero() {
queryValues.Add("start", params.StartDate.Format("2006-01-02"))
}
if !params.EndDate.IsZero() {
queryValues.Add("end", params.EndDate.Format("2006-01-02"))
}
if params.Page > 0 {
queryValues.Add("page", strconv.Itoa(params.Page))
} else {
queryValues.Add("page", "1")
}
if params.Limit > 0 {
queryValues.Add("limit", strconv.Itoa(params.Limit))
} else {
queryValues.Add("limit", "50")
}
if params.IncludeArchived {
queryValues.Add("include", "archived")
}
endpoint := fmt.Sprintf("/job?%s", queryValues.Encode())
resp, err := s.DoRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("API returned error: %s - %s", resp.Status, string(body))
}
var result struct {
Data struct {
Jobs []map[string]interface{} `json:"records"`
} `json:"data"`
}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %v", err)
}
log.Printf("Found %d jobs", len(result.Data.Jobs))
log.Printf("Parsed Data: %+v", result.Data)
return result.Data.Jobs, nil
}
// GetJobDetails retrieves detailed information about a specific job
func (s *Session) GetJobDetails(jobID string) (map[string]interface{}, error) {
endpoint := fmt.Sprintf("/job/%s", jobID)
resp, err := s.DoRequest(http.MethodGet, endpoint, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("API returned error: %s - %s", resp.Status, string(body))
}
var result struct {
Data map[string]interface{} `json:"data"`
}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %v", err)
}
return result.Data, nil
}
// GetAttachmentsForJob retrieves attachments for a specific job
func (s *Session) GetAttachmentsForJob(jobID string) (map[string]interface{}, error) {
resp, err := s.DoRequest("GET", fmt.Sprintf("/job/%s/paperwork", jobID), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get attachments: %s, response: %s", resp.Status, string(body))
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %v", err)
}
return result, nil
}
// GetDeficiencyInfoForJob retrieves deficiency information for a specific job
func (s *Session) GetDeficiencyInfoForJob(jobID string) ([]map[string]interface{}, error) {
resp, err := s.DoRequest("GET", fmt.Sprintf("/deficiency/%s", jobID), nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get deficiency info: %s, response: %s", resp.Status, string(body))
}
var result struct {
Data []map[string]interface{} `json:"data"`
}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("error unmarshalling response: %v", err)
}
return result.Data, nil
}