Repository navigation
Expand file tree
/
Copy pathuspmp-api.js
More file actions
135 lines (115 loc) · 4.52 KB
/
Copy pathuspmp-api.js
File metadata and controls
135 lines (115 loc) · 4.52 KB
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
const axios = require('axios').default
const https = require('node:https')
const cheerio = require('cheerio')
module.exports = class UspMp {
http
constructor({ httpsConfig, demoServer = false } = {}) {
const baseURL = `https://${demoServer ? 'demo-' : ''}autoabholung.meinpostkorb.brz.gv.at`
const httpsAgent = new https.Agent(httpsConfig)
this.http = axios.create({
baseURL,
timeout: 60 * 1000,
httpsAgent
})
}
async #soapCall(soapRequest) {
const res = await this.http.post('/soap', soapRequest, { headers: { 'Content-Type': 'application/soap+xml; charset=utf-8' } })
return res.data
}
async queryDeliveries({ newDeliveriesOnly = true } = {}) {
const queryAllDeliveriesRequest = `<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<aa:QueryDeliveriesRequest xmlns:aa="http://reference.e-government.gv.at/namespace/zustellung/autoabholung/phase2/20181206#">
<aa:AllDeliveries>
<aa:Paging>
<aa:Start>0</aa:Start>
<aa:Limit>100</aa:Limit>
</aa:Paging>
</aa:AllDeliveries>
</aa:QueryDeliveriesRequest>
</env:Body>
</env:Envelope>
`
const queryNewDeliveriesRequest = `<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<aa:QueryDeliveriesRequest xmlns:aa="http://reference.e-government.gv.at/namespace/zustellung/autoabholung/phase2/20181206#">
<aa:NewDeliveriesOnly>
<aa:Limit>100</aa:Limit>
</aa:NewDeliveriesOnly>
</aa:QueryDeliveriesRequest>
</env:Body>
</env:Envelope>
`
const queryDeliveriesResponse = await this.#soapCall(
newDeliveriesOnly ? queryNewDeliveriesRequest : queryAllDeliveriesRequest
)
const $ = cheerio.load(queryDeliveriesResponse, { xmlMode: true })
const deliveryIds = $('aa\\:DeliveryID')
.map((_, el) => $(el).text())
.get()
return deliveryIds
}
async getDelivery(id, { downloadAttachments = true } = {}) {
const getDeliveryRequest = `<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<aa:GetDeliveryRequest xmlns:aa="http://reference.e-government.gv.at/namespace/zustellung/autoabholung/phase2/20181206#">
<aa:DeliveryID>${id}</aa:DeliveryID>
</aa:GetDeliveryRequest>
</env:Body>
</env:Envelope>
`
const getDeliveryResponse = await this.#soapCall(getDeliveryRequest)
const $ = cheerio.load(getDeliveryResponse, { xmlMode: true })
const sender = $('aa\\:Sender p\\:CorporateBody p\\:FullName').text()
const subject = $('msg\\:Subject').text()
const timestamp = $('msg\\:DeliveryTimestamp').text()
const quality = $('msg\\:DeliveryQuality').text()
const attachments = $('aa\\:Attachment').map((_, el) => ({
id: $(el).find('aa\\:AttachmentID').text(),
filename: $(el).find('aa\\:FileName').text(),
mimeType: $(el).find('msg\\:MimeType').text(),
documentClass: $(el).find('msg\\:DocumentClass').text() || undefined,
})).get()
const delivery = {
id,
sender,
timestamp,
subject,
quality,
attachments
}
if (downloadAttachments) {
for (const attachment of attachments) {
const res = await this.http.get('/attachment', {
params: {
'delivery_id': id,
'attachment_id': attachment.id,
},
responseType: attachment.mimeType.startsWith('text/') ? 'text' : 'arraybuffer',
})
attachment.data = res.data
}
}
return delivery
}
async closeDelivery(id) {
const closeDeliveryRequest = `<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<aa:CloseDeliveryRequest xmlns:aa="http://reference.e-government.gv.at/namespace/zustellung/autoabholung/phase2/20181206#">
<aa:DeliveryID>${id}</aa:DeliveryID>
</aa:CloseDeliveryRequest>
</env:Body>
</env:Envelope>`
const closeDeliveryResponse = await this.#soapCall(closeDeliveryRequest)
const $ = cheerio.load(closeDeliveryResponse, { xmlMode: true })
return $('aa\\:CloseDeliveryResponse aa\\:Success').text() === 'true'
}
}