-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter.go
162 lines (138 loc) · 5.12 KB
/
letter.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
152
153
154
155
156
157
158
159
160
161
162
package epostbusiness
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
type Letter struct {
FileName string `json:"fileName" validate:"min=5,max=200"`
Data string `json:"data"`
IsColor bool `json:"isColor"`
IsDuplex bool `json:"isDuplex"`
BatchID int `json:"batchID"`
TestFlag bool `json:"testFlag"`
TestShowRestrictedArea bool `json:"testShowRestrictedArea"`
TestEMail string `json:"testEMail"`
AddressLine1 string `json:"addressLine1"`
AddressLine2 string `json:"addressLine2"`
AddressLine3 string `json:"addressLine3"`
ZipCode string `json:"zipCode"`
City string `json:"city"`
Country string `json:"country"`
SenderAdressLine1 string `json:"senderAdressLine1"`
SenderStreet string `json:"senderStreet"`
SenderZipCode string `json:"senderZipCode"`
SenderCity string `json:"senderCity"`
Custom1 string `json:"custom1"`
Custom2 string `json:"custom2"`
}
type LetterIdentifier struct {
FileName string `json:"fileName"`
LetterID int `json:"letterID"`
}
func (a API) CreateLetters(ctx context.Context, letters []Letter) ([]LetterIdentifier, error) {
var letterIdentifiers []LetterIdentifier
body, err := json.Marshal(letters)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url+"/api/Letter", bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+a.jwt)
res, err := a.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
switch res.StatusCode {
case http.StatusOK:
err = json.NewDecoder(res.Body).Decode(&letterIdentifiers)
if err != nil {
return nil, err
}
return letterIdentifiers, nil
case http.StatusBadRequest:
fallthrough
case http.StatusUnauthorized:
fallthrough
case http.StatusTooManyRequests:
var loginErr loginError
err = json.NewDecoder(res.Body).Decode(&loginErr)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%s: %s", loginErr.Code, loginErr.Description)
default:
return nil, errors.New(res.Status)
}
}
type LetterStatus struct {
LetterID int `json:"letterID"`
FileName string `json:"fileName"`
StatusID int `json:"statusID"`
StatusDetails *string `json:"statusDetails"`
CreatedDate Time `json:"createdDate"`
ProcessedDate *Time `json:"processedDate"`
PrintUploadDate *Time `json:"printUploadDate"`
PrintFeedbackDate *Time `json:"printFeedbackDate"`
TestFlag *bool `json:"testFlag"`
TestEMail *string `json:"testEMail"`
TestShowRestrictedArea bool `json:"testShowRestrictedArea"`
RegisteredLetter *string `json:"registeredLetter"`
RegisteredLetterID *string `json:"registeredLetterID"`
BatchID int `json:"batchID"`
CoverLetter bool `json:"coverLetter"`
NoOfPages int `json:"noOfPages"`
SubVendorID *string `json:"subVendorID"`
Custom1 *string `json:"custom1"`
Custom2 *string `json:"custom2"`
Custom3 *string `json:"custom3"`
Custom4 *string `json:"custom4"`
Custom5 *string `json:"custom5"`
ZipCode *string `json:"zipCode"`
City *string `json:"city"`
Country *string `json:"country"`
IsColor bool `json:"isColor"`
IsDuplex bool `json:"isDuplex"`
RegisteredLetterStatus *string `json:"registeredLetterStatus"`
RegisteredLetterStatusDate *Time `json:"registeredLetterStatusDate"`
ErrorList []LetterError `json:"errorList"`
}
type LetterError struct {
Description string `json:"description"`
Level string `json:"level"`
Code string `json:"code"`
Date *Time `json:"date"`
}
func (a API) GetLettersStatusList(ctx context.Context, letterIDs []int) ([]LetterStatus, error) {
var statusList []LetterStatus
body, err := json.Marshal(letterIDs)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url+"/api/Letter/StatusQuery", bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+a.jwt)
res, err := a.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%d: %s", res.StatusCode, res.Status)
}
err = json.NewDecoder(res.Body).Decode(&statusList)
if err != nil {
return nil, err
}
return statusList, nil
}