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
| Imports System
Imports System.IO
Imports System.Net.Http
Module Program
Async Function SubmitPdfConversionTask(filePathToConvert As String, rapidApiKey As String) As Task(Of String)
Using httpClient As New HttpClient()
Using form As New MultipartFormDataContent()
form.Headers.Add("X-RapidAPI-Key", rapidApiKey)
form.Add(New StringContent("Sebastian"), "authorName")
form.Add(New StringContent("Final report"), "title")
form.Add(New StringContent("Reports, Final"), "keywords")
form.Add(New StringContent("Quarterly Final Report"), "name")
Dim fileBytes = Await File.ReadAllBytesAsync(filePathToConvert)
Dim fileName = Path.GetFileName(filePathToConvert)
form.Add(New ByteArrayContent(fileBytes, 0, fileBytes.Length), "file", fileName)
Using response = Await httpClient.PostAsync("https://petadata-document-conversion-suite.p.rapidapi.com/SubmitPDFConversionTask", form)
response.EnsureSuccessStatusCode()
Return Await response.Content.ReadAsStringAsync()
End Using
End Using
End Using
End Function
Async Function GetConversionTaskStatus(taskId As String, rapidApiKey As String) As Task(Of String)
Using httpClient As New HttpClient()
httpClient.DefaultRequestHeaders.Add("X-RapidAPI-Key", rapidApiKey)
Using response = Await httpClient.GetAsync($"https://petadata-document-conversion-suite.p.rapidapi.com/GetConversionTaskStatus?taskId={taskId}")
response.EnsureSuccessStatusCode()
Return Await response.Content.ReadAsStringAsync()
End Using
End Using
End Function
Async Function DownloadResult(taskId As String, rapidApiKey As String) As Task(Of Byte())
Using httpClient As New HttpClient()
httpClient.DefaultRequestHeaders.Add("X-RapidAPI-Key", rapidApiKey)
Using response = Await httpClient.GetAsync($"https://petadata-document-conversion-suite.p.rapidapi.com/DownloadResult?taskId={taskId}")
response.EnsureSuccessStatusCode()
Return Await response.Content.ReadAsByteArrayAsync()
End Using
End Using
End Function
Public Async Function ConvertDocumentAsync() As Task
Dim rapidApiKey = "<YOUR RAPIDAPI KEY HERE>"
Dim filePathToConvert = "<FILE PATH TO CONVERT>"
Dim taskId = Await SubmitPdfConversionTask(filePathToConvert, rapidApiKey)
Dim retryCount = 0
Do While (retryCount < 100)
retryCount += 1
Await Task.Delay(5000)
Dim status = Await GetConversionTaskStatus(taskId, rapidApiKey)
If (status = "Completed") Then
Dim fileBytes = Await DownloadResult(taskId, rapidApiKey)
Await File.WriteAllBytesAsync("final_report.pdf", fileBytes)
Exit Do
ElseIf (status = "Waiting") Then
Continue Do
ElseIf (status = "Failed") Then
Throw New Exception("Cannot convert file")
Else
Throw New Exception("Invalid status")
End If
Loop
End Function
Sub Main(args As String())
ConvertDocumentAsync().Wait()
End Sub
End Module
|