Skip to content

Commit

Permalink
support the pdf consumer with a pluggable playwright wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
northdpole committed Oct 24, 2024
1 parent 1d27773 commit 876824a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pkg/playwright/mock/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mock

type MockClient struct {
StopCallBack func() error
GetPDFOfPageCallBack func(string, string) ([]byte, error)
}

// NewClient returns an actual github client
func NewMockClient() (MockClient, error) {
return MockClient{}, nil
}

func (c MockClient) Stop() error {
return c.StopCallBack()
}

func (c MockClient) GetPDFOfPage(page, storePath string) ([]byte, error) {
return c.GetPDFOfPageCallBack(page, storePath)
}
58 changes: 58 additions & 0 deletions pkg/playwright/wrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package wrapper

import (
"github.com/go-errors/errors"
"github.com/playwright-community/playwright-go"
)

// Wrapper is the wrapper interface that allows the playwright client to be pluggable
type Wrapper interface {
Stop() error
GetPDFOfPage(string, string) ([]byte, error)
}

// Client is the wrapper around google's go-github client
type Client struct {
playwright *playwright.Playwright
}

// NewClient returns an actual github client
func NewClient() (Client, error) {
pw, err := playwright.Run()
if err != nil {
return Client{}, err
}
// create new playwright client
return Client{
playwright: pw,
}, nil
}

func (c Client) Stop() error {
return c.playwright.Stop()
}

func (c Client) GetPDFOfPage(page, storePath string) ([]byte, error) {
browser, err := c.playwright.Chromium.Launch()
if err != nil {
return nil, err
}

currentContext, err := browser.NewContext()
if err != nil {
return nil, err
}

newPage, err := currentContext.NewPage()
if err != nil {
return nil, err
}

if _, err = newPage.Goto(page); err != nil {
return nil, errors.Errorf("could not goto page %s in the browser: %w", page, err)
}

return newPage.PDF(playwright.PagePdfOptions{
Path: playwright.String(storePath),
})
}

0 comments on commit 876824a

Please sign in to comment.