-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostbox.go
More file actions
98 lines (80 loc) · 2.15 KB
/
Copy pathpostbox.go
File metadata and controls
98 lines (80 loc) · 2.15 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
package gomdirectapi
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"path/filepath"
"strconv"
"strings"
"github.com/alex21289/gomdirectapi/postbox"
"github.com/alex21289/merkur"
)
func (c *Client) GetPostbox() (*postbox.PostboxApiResponse, error) {
response, err := c.http.Get(PostboxURL)
if err = handleErr(*response, err); err != nil {
return nil, err
}
var pb postbox.PostboxApiResponse
if err = response.UnmarshalJson(&pb); err != nil {
return nil, err
}
// Get All
matches := pb.Paging.Matches
params := merkur.NewParams()
params.Add("paging-count", strconv.Itoa(matches))
response, err = c.http.GetQuery(PostboxURL, params)
if err = handleErr(*response, err); err != nil {
return nil, err
}
if err = response.UnmarshalJson(&pb); err != nil {
return nil, err
}
return &pb, nil
}
func (c *Client) GetDocumentBytes(documentID string) ([]byte, error) {
url := fmt.Sprintf(PostboxDocumentURL, documentID)
headers := make(http.Header)
headers.Set("Accept", "application/pdf")
response, err := c.http.Get(url, headers)
if err = handleErr(*response, err); err != nil {
return nil, err
}
return response.Bytes(), nil
}
func (c *Client) DownloadPostboxDocument(documentID string, downloadPath string) error {
doc, err := c.GetPostboxDocument(documentID)
if err != nil {
return err
}
if doc.Mimetype != "application/pdf" {
return errors.New("cant download none pdf")
}
log.Println("Download: " + doc.Name)
fileName := strings.ReplaceAll(doc.Name, "/", "-") + ".pdf"
url := fmt.Sprintf(PostboxDocumentURL, documentID)
headers := make(http.Header)
headers.Set("Accept", "application/pdf")
response, err := c.http.Get(url, headers)
if err = handleErr(*response, err); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(downloadPath, fileName), response.Bytes(), 0644); err != nil {
return err
}
return nil
}
func (c *Client) GetPostboxDocument(documentID string) (*postbox.Document, error) {
pb, err := c.GetPostbox()
if err != nil {
return nil, err
}
var document postbox.Document
for _, d := range pb.Values {
if d.DocumentID == documentID {
document = d
}
}
return &document, nil
}