Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Thecodingmachine master #33

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 28 additions & 13 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,27 +118,42 @@ func (c *Client) PostContext(ctx context.Context, req Request) (*http.Response,
return resp, nil
}

// Store creates the resulting PDF to given destination.
func (c *Client) Store(req Request, dest string) error {
return c.StoreContext(context.Background(), req, dest)
}

// StoreContext creates the resulting PDF to given destination.
// The created HTTP request can be canceled by the passed context.
func (c *Client) StoreContext(ctx context.Context, req Request, dest string) error {
func (c *Client) makeRequest(ctx context.Context, req Request) (*bytes.Buffer, error) {
if hasWebhook(req) {
return errors.New("cannot use Store method with a webhook")
return nil, errors.New("cannot use Store method with a webhook")
}
resp, err := c.PostContext(ctx, req)
if err != nil {
return err
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return errors.New("failed to generate the result PDF")
if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("failed to generate the result PDF - http status code: %d", resp.StatusCode)
}

buff := bytes.NewBuffer(nil)
_, err = io.Copy(buff, resp.Body)
return buff, err
}

// Store creates the resulting PDF to given destination.
func (c *Client) Store(req Request, dest string) error {
data, err := c.makeRequest(context.Background(), req)
if err != nil {
return err
}
return writeNewFile(dest, data)
}

// StoreToWriter creates the resulting PDF to a io writer
func (c *Client) StoreToWriter(req Request, w io.Writer) error {
data, err := c.makeRequest(context.Background(), req)
if err != nil {
return err
}
return writeNewFile(dest, resp.Body)
_, err = io.Copy(w, data)
return err
}

func hasWebhook(req Request) bool {
Expand Down
2 changes: 1 addition & 1 deletion html.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (req *HTMLRequest) Assets(assets ...Document) {
}

func (req *HTMLRequest) postURL() string {
return "/convert/html"
return "/forms/chromium/convert/html"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This will work with gotenberg API docker iamge v7, but is not backward compatible with v6.

}

func (req *HTMLRequest) formFiles() map[string]Document {
Expand Down