-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support the pdf consumer with a pluggable playwright wrapper
- Loading branch information
1 parent
1d27773
commit 876824a
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
} |