In this tutorial we will cover how to implement asynchronous approach of Image Converter. You only need to send source image to SubmitWebPConversionTask or any other submission API methods like SubmitBmpConversionTask, SubmitDicomConversionTask etc. to receive task identifier as response.

After receiving task identifier you need to check task status by calling GetConversionTaskStatus method. You need to call same method after a few seconds delay as long as task status equals to “Waiting”. Once you receive “Completed” from GetConversionTaskStatus method you can call DownloadResult method to download final image. You can use the Visual Basic implementation below.

 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
Imports System
Imports System.IO
Imports System.Net.Http

Module Program
    Async Function SubmitWebPConversionTask(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("true"), "lossy")
                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://image-converter4.p.rapidapi.com/submitWebPConversionTask", 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://image-converter4.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://image-converter4.p.rapidapi.com/downloadResult?taskId={taskId}")

                response.EnsureSuccessStatusCode()
                Return Await response.Content.ReadAsByteArrayAsync()
            End Using
        End Using
    End Function
    Public Async Function ConvertToWebPAsync() As Task
        Dim rapidApiKey = "<YOUR RAPIDAPI KEY HERE>"
        Dim filePathToConvert = "<SOURCE IMAGE FILE TO CONVERT>"
        Dim taskId = Await SubmitWebPConversionTask(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_image.webp", fileBytes)
                Exit Do
            ElseIf (status = "Waiting") Then
                Continue Do
            ElseIf (status = "Failed") Then
                Throw New Exception("Cannot convert image")
            Else
                Throw New Exception("Invalid status")
            End If
        Loop
    End Function
    Sub Main(args As String())
        ConvertToWebPAsync().Wait()
    End Sub
End Module