This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BillomatPdfExporter.groovy
98 lines (81 loc) · 2.34 KB
/
BillomatPdfExporter.groovy
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
@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.5.2')
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
/**
* Exports Billomat invoices as PDF
*/
class BillomatPdfExporter {
/**
* Invoice IDs
*/
private ids = []
/**
* HTTPBuilder
*/
private http
/**
* Constructor
*/
BillomatPdfExporter() {
this.http = new HTTPBuilder('https://medefa.billomat.net')
this.http.setHeaders([
'User-Agent': 'BilloPdfExporter 0.1 by [email protected]',
'X-BillomatApiKey': '4b28556815aeb8f67e3937524b73817d',
'Accept': 'application/json'
])
}
/**
* Save PDF
* @param invoiceId
* @return
*/
public void savePdf(invoiceId) {
this.http.request(GET, JSON) {
uri.path = "/api/invoices/${invoiceId}/pdf"
uri.query = ['format': 'json']
response.success = { resp, json ->
def file = new File(json.document.filename)
file.delete();
file << json.document.base64file.decodeBase64()
}
response.failure = { resp ->
println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
}
}
}
/**
* Get invoice ids
* @param status
* @return
*/
public List getInvoiceIds(status) {
this.http.request(GET, JSON) {
uri.path = '/api/invoices'
uri.query = ['status': status]
response.success = { resp, json ->
ids = []
json.invoices.invoice.each { invoice ->
ids.push(invoice.id)
}
return ids
}
response.failure = { resp ->
println "Unexpected error: ${resp.statusLine.statusCode} : ${resp.statusLine.reasonPhrase}"
}
}
}
/**
*
* @param arguments
*/
static main(arguments) {
println "Starting export..."
def exporter = new BillomatPdfExporter()
def invIds = exporter.getInvoiceIds('open')
invIds.each { invoiceId ->
exporter.savePdf(invoiceId)
}
println "done."
}
}