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
| package main
import (
"bytes"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"time"
)
func createReqBody(filePathToConvert string, authorName string, title string, keywords string, name string) (string, io.Reader, error) {
var err error
buf := new(bytes.Buffer)
bw := multipart.NewWriter(buf)
f, err := os.Open(filePathToConvert)
if err != nil {
return "", nil, err
}
defer f.Close()
authorNameField, _ := bw.CreateFormField("authorName")
authorNameField.Write([]byte(authorName))
titleField, _ := bw.CreateFormField("title")
titleField.Write([]byte(title))
keywordsField, _ := bw.CreateFormField("keywords")
keywordsField.Write([]byte(keywords))
nameField, _ := bw.CreateFormField("name")
nameField.Write([]byte(name))
_, fileName := filepath.Split(filePathToConvert)
fw1, _ := bw.CreateFormFile("file", fileName)
io.Copy(fw1, f)
bw.Close()
return bw.FormDataContentType(), buf, nil
}
func submitPdfConversionTask(filePath string, rapidApiKey string) (string, error) {
contType, reader, err := createReqBody(filePath, "Sebastian", "Final report",
"Reports, Final", "Quarterly Final Report")
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", "https://petadata-document-conversion-suite.p.rapidapi.com/SubmitPDFConversionTask", reader)
req.Header.Add("Content-Type", contType)
req.Header.Add("X-RapidAPI-Key", rapidApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Cannot upload file to convert error:", err)
return "", err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}
func getConversionTaskStatus(taskId string, rapidApiKey string) (string, error) {
url := fmt.Sprintf("https://petadata-document-conversion-suite.p.rapidapi.com/GetConversionTaskStatus?taskId=%s", taskId)
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("X-RapidAPI-Key", rapidApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Cannot check conversion status of task with error:", err)
return "", err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}
func downloadResult(taskId string, rapidApiKey string) ([]byte, error) {
url := fmt.Sprintf("https://petadata-document-conversion-suite.p.rapidapi.com/DownloadResult?taskId=%s", taskId)
req, err := http.NewRequest("GET", url, nil)
req.Header.Add("X-RapidAPI-Key", rapidApiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil || resp.StatusCode != 200 {
fmt.Println("Cannot download converted file with error:", err)
return nil, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return b, nil
}
func main() {
filePathToConvert := "<FILE PATH TO CONVERT>"
rapidApiKey := "<YOUR RAPIDAPI KEY HERE>"
taskId, err := submitPdfConversionTask(filePathToConvert, rapidApiKey)
if err != nil {
panic(err)
}
retryCount := 0
for retryCount < 100 {
retryCount++
time.Sleep(5 * time.Second)
status, err := getConversionTaskStatus(taskId, rapidApiKey)
if err != nil {
panic(err)
}
if status == "Completed" {
fileBytes, err := downloadResult(taskId, rapidApiKey)
if err != nil {
panic(err)
}
err = os.WriteFile("final_report.pdf", fileBytes, 0644)
if err != nil {
panic(err)
}
break
} else if status == "Waiting" {
continue
} else if status == "Failed" {
panic(errors.New("Cannot convert file"))
} else {
panic(errors.New("Cannot convert file"))
}
}
}
|