thecodingmachine / gotenberg-go-client

Go client for the Gotenberg API
MIT License
57 stars 33 forks source link

Help supporting v7 #31

Open mnavarrocarter opened 3 years ago

mnavarrocarter commented 3 years ago

Hey, at my company we are in dire need for this SDK to support v7.

I'm willing to contribute to make support happen, but I don't know where to start.

If you could point me to what does need to be done in order to support v7 then I'll be more than happy to submit a PR.

How does that sound? 😃

gulien commented 3 years ago

Hello @mnavarrocarter, thanks for your interest in Gotenberg 😄

The API primarily consists of multipart/form-data endpoints, so this client is mainly a multipart/form-data request builder.

Honestly, I'm not sure that using an SDK is required to interact with Gotenberg; it helps a bit, but you might have more flexibility by directly creating "native" multipart/form-data requests in your application.

Anyway, if an SDK makes sense in your case, you may either:

IMO the key will be to mimic the module's organization from Gotenberg (i.e., endpoints per module).

mnavarrocarter commented 3 years ago

Hi @gulien, thanks for your reply!

I'm mid way of coding our own implementation of the html endpoint. It would be nice If I can help the community porting some of that. But if you think is not necessary, that's fine with me. 😃

gulien commented 3 years ago

Your call 😄 but I think it might help others indeed.

ju-popov commented 2 years ago

It is morally important to support v7 👯‍♀️

export-mike commented 2 years ago

Raw HTML example:

package main

import (
    "bytes"
    "fmt"
    "io"
    "mime/multipart"
    "net/http"
    "os"
    "strings"
)

func main() {
    reader := strings.NewReader("<h1>hi</h1>")
    buf := new(bytes.Buffer)
    writer := multipart.NewWriter(buf)
    part, err := writer.CreateFormFile("file", "index.html")
    if err != nil {
        panic(err)
    }
    _, err = io.Copy(part, reader)
    if err != nil {
        panic(err)
    }
    fmt.Println(buf.String())
    writer.Close()

    req, err := http.NewRequest("POST", "http://localhost:3000/forms/chromium/convert/html", buf)
    if err != nil {
        panic(err)
    }
    req.Header.Set("Content-Type", writer.FormDataContentType())

    res, err := http.DefaultClient.Do(req)
    if err != nil {
        panic(err)
    }
    if res.StatusCode != http.StatusOK {
        panic(fmt.Errorf("bad status: %s", res.Status))
    }
    defer res.Body.Close()
    out, err := os.Create("index.pdf")
    if err != nil {
        panic(err)
    }
    defer out.Close()
    io.Copy(out, res.Body)
    return
}