diff --git a/pkg/playwright/mock/wrapper.go b/pkg/playwright/mock/wrapper.go new file mode 100644 index 000000000..8a644e16b --- /dev/null +++ b/pkg/playwright/mock/wrapper.go @@ -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) +} diff --git a/pkg/playwright/wrapper.go b/pkg/playwright/wrapper.go new file mode 100644 index 000000000..c57911251 --- /dev/null +++ b/pkg/playwright/wrapper.go @@ -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), + }) +}