/
job_openings.go
92 lines (81 loc) · 3.31 KB
/
job_openings.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
/*
Copyright 2021-2022
Carnegie Robotics, LLC
4501 Hatfield Street, Pittsburgh, PA 15201
https://www.carnegierobotics.com
All rights reserved.
This file is part of greenhouse-client-go.
greenhouse-client-go is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
greenhouse-client-go is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with greenhouse-client-go. If not, see <https://www.gnu.org/licenses/>.
*/
package greenhouse
import (
"context"
"encoding/json"
"fmt"
)
// GetAllJobOpenings retrieves a list of all openings for a job.
// Greenhouse API docs: https://developers.greenhouse.io/harvest.html#get-list-job-openings
func GetAllJobOpenings(c *Client, ctx context.Context, id int, status string) (*[]JobOpening, error) {
var obj []JobOpening
endpoint := fmt.Sprintf("v1/jobs/%d/openings", id)
querystring := fmt.Sprintf("status=%s", status)
err := MultiGet(c, ctx, endpoint, querystring, &obj)
if err != nil {
return nil, err
}
return &obj, nil
}
// GetJobOpening retrieves an opening by ID.
// Greenhouse API docs: https://developers.greenhouse.io/harvest.html#get-single-opening-for-job
func GetJobOpening(c *Client, ctx context.Context, jobId int, openingId int) (*JobOpening, error) {
var obj JobOpening
endpoint := fmt.Sprintf("v1/jobs/%d/openings/%d", jobId, openingId)
err := SingleGet(c, ctx, endpoint, &obj)
if err != nil {
return nil, err
}
return &obj, nil
}
// DeleteJobOpenings deletes openings from a job.
// Greenhouse API docs: https://developers.greenhouse.io/harvest.html#delete-destroy-openings
func DeleteJobOpenings(c *Client, ctx context.Context, jobId int, openingIds []int) error {
jsonBody, err := json.Marshal(map[string][]int{"ids": openingIds})
if err != nil {
return err
}
return Delete(c, ctx, fmt.Sprintf("v1/jobs/%d/openings", jobId), jsonBody)
}
// UpdateJobOpenings updates the openings for a job.
// Greenhouse API docs: https://developers.greenhouse.io/harvest.html#patch-edit-openings
func UpdateJobOpenings(c *Client, ctx context.Context, jobId int, openingId int, obj *JobOpeningUpdateInfo) error {
endpoint := fmt.Sprintf("v1/jobs/%d/openings/%d", jobId, openingId)
return Update(c, ctx, endpoint, obj)
}
// CreateJobOpenings adds openings to a job.
// Greenhouse API docs: https://developers.greenhouse.io/harvest.html#post-create-new-openings
func CreateJobOpenings(c *Client, ctx context.Context, jobId int, obj JobOpeningCreateInfo) (*[]int, error) {
idList := make([]int, len(obj.Openings), len(obj.Openings))
type RespObj struct {
Openings []Opening `json:"openings"`
}
var respObj RespObj
endpoint := fmt.Sprintf("v1/jobs/%d/openings", jobId)
jsonBody, err := json.Marshal(obj)
if err != nil {
return nil, err
}
resp, err := Post(c, ctx, endpoint, jsonBody)
if err != nil {
return nil, err
}
err = json.Unmarshal(resp.Body(), &respObj)
if err != nil {
return nil, err
}
for i, item := range respObj.Openings {
idList[i] = *item.Id
}
return &idList, nil
}