-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontactEnrolmentsCertificate.go
72 lines (57 loc) · 1.69 KB
/
contactEnrolmentsCertificate.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
package axcelerate
import (
"encoding/base64"
"encoding/json"
"fmt"
"path/filepath"
"strings"
)
type Certificate struct {
Certificate string `json:"CERTIFICATE"`
}
type Media struct {
FileName string `db:"file_name" json:"file_name,omitempty"`
FileType string `db:"file_type" json:"file_type,omitempty"`
ContentType string `db:"content_type" json:"content_type,omitempty"`
Data []byte `db:"file_data" json:"file_data,omitempty"`
Size int `db:"file_size" json:"file_size,omitempty"`
}
// ContactEnrolmentsCertificate Returns a Certificate
func (s *ContactService) ContactEnrolmentsCertificate(enrolID int) (Media, *Response, error) {
var obj Certificate
parms := map[string]string{"enrolID": fmt.Sprintf("%d", enrolID)}
url := "/contact/enrolment/certificate"
resp, err := do(s.client, "GET", Params{parms: parms, u: url}, obj)
if err != nil {
return Media{}, resp, err
}
err = json.Unmarshal([]byte(resp.Body), &obj)
if err != nil {
return Media{}, resp, err
}
// Decode the base64 encoded certificate
pdfData, err := base64.StdEncoding.DecodeString(obj.Certificate)
if err != nil {
return Media{}, resp, err
}
fileName := fmt.Sprintf("%d-Certificate.pdf", enrolID)
media := createMedia(pdfData, fileName)
return media, resp, err
}
func createMedia(fileData []byte, fileName string) Media {
return Media{
FileName: fileName,
FileType: filepath.Ext(fileName),
ContentType: getContentType(fileName),
Data: fileData,
Size: len(fileData),
}
}
func getContentType(fileName string) string {
switch strings.ToLower(filepath.Ext(fileName)) {
case ".pdf":
return "application/pdf"
default:
return "application/octet-stream"
}
}