package main
import (
"fmt"
"os"
"time"
"github.com/d3/d3-go-client"
)
func main() {
client, err := d3.NewClient(d3.Config{
APIKey: os.Getenv("D3_API_KEY"),
BaseURL: "https://api.dragdropdo.com",
})
if err != nil {
panic(err)
}
// 1. Upload
uploadResult, err := client.UploadFile(d3.UploadFileOptions{
File: "./document.pdf",
FileName: "document.pdf",
})
if err != nil {
panic(err)
}
// 2. Check support
supported, err := client.CheckSupportedOperation(d3.SupportedOperationOptions{
Ext: "pdf",
Action: "convert",
Parameters: map[string]interface{}{
"convert_to": "png",
},
})
if err != nil {
panic(err)
}
if !supported.Supported {
panic("Convert to PNG is not supported")
}
// 3. Create operation
operation, err := client.Convert(
[]string{uploadResult.FileKey},
"png",
map[string]string{"userId": "user-123", "source": "api"},
)
if err != nil {
panic(err)
}
// 4. Poll
status, err := client.PollStatus(d3.PollStatusOptions{
StatusOptions: d3.StatusOptions{
MainTaskID: operation.MainTaskID,
},
Interval: 2 * time.Second,
})
if err != nil {
panic(err)
}
if status.OperationStatus == "completed" {
for _, file := range status.FilesData {
fmt.Printf("Download: %s\n", file.DownloadLink)
}
}
}