-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinwheel_jobs.go
139 lines (116 loc) · 4.23 KB
/
pinwheel_jobs.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
package rize
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/google/go-querystring/query"
)
// Handles all PinwheelJob operations
type pinwheelJobService service
// PinwheelJob data type
type PinwheelJob struct {
UID string `json:"uid,omitempty"`
SyntheticAccountUID string `json:"synthetic_account_uid,omitempty"`
Status string `json:"status,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
StatusUpdatedAt time.Time `json:"status_updated_at,omitempty"`
CustomerUID string `json:"customer_uid,omitempty"`
LinkToken string `json:"link_token,omitempty"`
ExpiresAt time.Time `json:"expires_at,omitempty"`
JobNames []string `json:"job_names,omitempty"`
Amount int `json:"amount,omitempty"`
DisablePartialSwitch bool `json:"disable_partial_switch,omitempty"`
OrganizationName string `json:"organization_name,omitempty"`
SkipWelcomeScreen bool `json:"skip_welcome_screen,omitempty"`
}
// PinwheelJobListParams builds the query parameters used in querying Pinwheel Jobs
type PinwheelJobListParams struct {
CustomerUID string `url:"customer_uid,omitempty" json:"customer_uid,omitempty"`
SyntheticAccountUID string `url:"synthetic_account_uid,omitempty" json:"synthetic_account_uid,omitempty"`
Limit int `url:"limit,omitempty" json:"limit,omitempty"`
Offset int `url:"offset,omitempty" json:"offset,omitempty"`
}
// PinwheelJobCreateParams are the body params used when creating a new Pinwheel Job
type PinwheelJobCreateParams struct {
JobNames []string `json:"job_names"`
SyntheticAccountUID string `json:"synthetic_account_uid"`
Amount int `json:"amount,omitempty"`
DisablePartialSwitch bool `json:"disable_partial_switch,omitempty"`
OrganizationName string `json:"organization_name,omitempty"`
SkipWelcomeScreen bool `json:"skip_welcome_screen,omitempty"`
}
// PinwheelJobListResponse is an API response containing a list of Pinwheel Jobs
type PinwheelJobListResponse struct {
ListResponse
Data []*PinwheelJob `json:"data"`
}
// List retrieves a list of Pinwheel Jobs filtered by the given parameters
func (p *pinwheelJobService) List(ctx context.Context, params *PinwheelJobListParams) (*PinwheelJobListResponse, error) {
// Build PinwheelJobListParams into query string params
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := p.client.doRequest(ctx, http.MethodGet, "pinwheel_jobs", v, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &PinwheelJobListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Create is used to initialize a new Pinwheel Job and return a pinwheel_link_token to be used with the Pinwheel Link SDK
func (p *pinwheelJobService) Create(ctx context.Context, params *PinwheelJobCreateParams) (*PinwheelJob, error) {
if len(params.JobNames) == 0 || params.SyntheticAccountUID == "" {
return nil, fmt.Errorf("JobNames and SyntheticAccountUID are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := p.client.doRequest(ctx, http.MethodPost, "pinwheel_jobs", nil, bytes.NewBuffer(bytesMessage))
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &PinwheelJob{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Get returns a single PinwheelJob
func (p *pinwheelJobService) Get(ctx context.Context, uid string) (*PinwheelJob, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := p.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("pinwheel_jobs/%s", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
response := &PinwheelJob{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}