-
Notifications
You must be signed in to change notification settings - Fork 0
/
kyc_documents.go
151 lines (124 loc) · 3.78 KB
/
kyc_documents.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
140
141
142
143
144
145
146
147
148
149
150
151
package rize
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/google/go-querystring/query"
)
// Handles all KYC Document operations
type kycDocumentService service
// KYCDocument data type
type KYCDocument struct {
UID string `json:"uid,omitempty"`
Type string `json:"type,omitempty"`
Filename string `json:"filename,omitempty"`
Note string `json:"note,omitempty"`
Extension string `json:"extension,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
// KYCDocumentListParams builds the query parameters used in querying KYCDocuments
type KYCDocumentListParams struct {
EvaluationUID string `url:"evaluation_uid,omitempty" json:"evaluation_uid,omitempty"`
}
// KYCDocumentUploadParams are the body params used when uploading a new KYC Document
type KYCDocumentUploadParams struct {
EvaluationUID string `json:"evaluation_uid"`
Filename string `json:"filename"`
FileContent string `json:"file_content"`
Note string `json:"note"`
Type string `json:"type"`
}
// KYCDocumentListResponse is an API response containing a list of KYC Documents
type KYCDocumentListResponse struct {
ListResponse
Data []*KYCDocument `json:"data"`
}
// List retrieves a list of KYC Documents for a given evaluation
func (k *kycDocumentService) List(ctx context.Context, params *KYCDocumentListParams) (*KYCDocumentListResponse, error) {
if params.EvaluationUID == "" {
return nil, fmt.Errorf("EvaluationUID is required")
}
v, err := query.Values(params)
if err != nil {
return nil, err
}
res, err := k.client.doRequest(ctx, http.MethodGet, "kyc_documents", 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 := &KYCDocumentListResponse{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Upload a KYC Document for review
func (k *kycDocumentService) Upload(ctx context.Context, params *KYCDocumentUploadParams) (*KYCDocument, error) {
if params.EvaluationUID == "" ||
params.Filename == "" ||
params.FileContent == "" ||
params.Note == "" ||
params.Type == "" {
return nil, fmt.Errorf("all KYCDocumentUploadParams are required")
}
bytesMessage, err := json.Marshal(params)
if err != nil {
return nil, err
}
res, err := k.client.doRequest(ctx, http.MethodPost, "kyc_documents", 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 := &KYCDocument{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// Get is used to retrieve metadata for a KYC Document previously uploaded
func (k *kycDocumentService) Get(ctx context.Context, uid string) (*KYCDocument, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
res, err := k.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("kyc_documents/%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 := &KYCDocument{}
if err = json.Unmarshal(body, response); err != nil {
return nil, err
}
return response, nil
}
// View is used to retrieve a KYC Document (image, PDF, etc) previously uploaded
func (k *kycDocumentService) View(ctx context.Context, uid string) (*http.Response, error) {
if uid == "" {
return nil, fmt.Errorf("UID is required")
}
// TODO: Does this require a different Accept header type (image/png)?
res, err := k.client.doRequest(ctx, http.MethodGet, fmt.Sprintf("kyc_documents/%s/view", uid), nil, nil)
if err != nil {
return nil, err
}
defer res.Body.Close()
return res, nil
}