This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreport.go
127 lines (105 loc) · 3.39 KB
/
report.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
package checkr
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"path"
"time"
)
// Report represents a background check report. Depending on the selected
// package, a report can include the following screenings: SSN trace, sex
// offender search, national criminal search, county criminal searches and
// motor vehicle report.
type Report struct {
ID string `json:"id,omitempty"`
Object string `json:"object,omitempty"`
URI string `json:"uri,omitempty"`
Status string `json:"status,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
CompletedAt *time.Time `json:"completed_at,omitempty"`
TurnaroundTime int `json:"turnaround_time,omitempty"`
DueTime *time.Time `json:"due_time,omitempty"`
Adjudication string `json:"adjudication,omitempty"`
Package string `json:"package,omitempty"`
CandidateID string `json:"candidate_id,omitempty"`
SsnTraceID string `json:"ssn_trace_id,omitempty"`
SexOffenderSearchID string `json:"sex_offender_search_id,omitempty"`
NationalCriminalSearchID string `json:"national_criminal_search_id,omitempty"`
CountyCriminalSearchIDs []string `json:"county_criminal_search_ids,omitempty"`
MotorVehicleReportID string `json:"motor_vehicle_report_id,omitempty"`
StateCriminalSearchIDs []string `json:"state_criminal_search_ids,omitempty"`
}
// Create sends a request to create a new Report.
func (r Report) Create() error {
// marshal the candidate to buffered bytes representing JSON
b, err := json.Marshal(r)
if err != nil {
return err
}
body := bytes.NewBuffer(b)
// create a new request
url := URL.String() + reports
req, err := http.NewRequest(http.MethodPost, url, body)
if err != nil {
return err
}
// set API key for authentication and authorization
req.SetBasicAuth(apiKey, "")
// send the HTTP request with the default Go client
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
// check the HTTP response status code is 201
if resp.StatusCode != http.StatusCreated {
// read the HTTP response body
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// return the HTTP response body as an error
return errors.New(string(b))
}
return nil
}
// Show shows one report.
func (r *Report) Show() error {
if r.ID == "" {
return errors.New("an id is needed to show a report")
}
// create a new request
u, _ := url.Parse(URL.String())
u.Path = path.Join(URL.Path, reports, r.ID)
// create a new request
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return err
}
// set API key for authentication and authorization
req.SetBasicAuth(apiKey, "")
// send the HTTP request with the default Go client
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
// read the HTTP response body
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// unmarshal the candidate
if err = json.Unmarshal(b, &r); err != nil {
return err
}
// check the HTTP response status code is 200
if resp.StatusCode != http.StatusOK {
// return the HTTP response body as an error
return errors.New(string(b))
}
return nil
}