Skip to content

Commit

Permalink
feat(login) Add option to download chrome binary
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre PÉRONNET <[email protected]>
  • Loading branch information
holyhope committed Feb 8, 2024
1 parent 02e7e7c commit 1cf45a2
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# digiposte-go-sdk

This repository contains the Go SDK for the [Digiposte](https://digiposte.fr) API.

![Continuous Integration](https://github.com/holyhope/digiposte-go-sdk/actions/workflows/test.yml/badge.svg)
[![Go Reference](https://pkg.go.dev/badge/github.com/holyhope/digiposte-go-sdk.svg)](https://pkg.go.dev/github.com/holyhope/digiposte-go-sdk)
[![Go References](https://pkg.go.dev/badge/github.com/holyhope/digiposte-go-sdk.svg)](https://pkg.go.dev/github.com/holyhope/digiposte-go-sdk)

This repository contains the Go SDK for the [Digiposte](https://digiposte.fr) API.

It is a work in progress, and all the API endpoints are not implemented yet.

Expand Down
44 changes: 44 additions & 0 deletions login/chrome/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package chrome

import (
"context"
"errors"
"fmt"
"log"
Expand All @@ -9,6 +10,7 @@ import (
"time"

cu "github.com/Davincible/chromedp-undetected"
"github.com/go-rod/rod/lib/launcher"

login "github.com/holyhope/digiposte-go-sdk/login"
)
Expand Down Expand Up @@ -220,6 +222,48 @@ func (o *withLoggers) Apply(instance interface{}) error {
return &InvalidTypeOptionError{instance: instance}
}

func WithChromeVersion(ctx context.Context, revision int, client *http.Client) login.Option { //nolint:ireturn
browser := launcher.NewBrowser()
if ctx != nil {
browser.Context = ctx
}

if revision > 0 {
browser.Revision = revision
}

if client != nil {
browser.HTTPClient = client
}

return &withChromeVersion{Browser: browser}
}

type withChromeVersion struct {
Browser *launcher.Browser
}

func (o *withChromeVersion) Apply(instance interface{}) error {
if chrome, ok := instance.(*chromeLogin); ok {
o.Browser.Logger = chrome.infoLogger

path, err := o.Browser.Get()
if err != nil {
return fmt.Errorf("get browser: %w", err)
}

if err := o.Browser.Validate(); err != nil {
return fmt.Errorf("validate browser: %w", err)
}

chrome.binaryPath = path

return nil
}

return &InvalidTypeOptionError{instance: instance}
}

func WithBinary(path string) login.Option { //nolint:ireturn
return &withBinary{Path: path}
}
Expand Down
6 changes: 3 additions & 3 deletions v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Config struct {
Credentials *login.Credentials
}

func (c *Config) SetupDefault() error {
func (c *Config) SetupDefault(ctx context.Context) error {
if c.APIURL == "" {
c.APIURL = settings.DefaultAPIURL
}
Expand All @@ -48,7 +48,7 @@ func (c *Config) SetupDefault() error {
}

if c.LoginMethod == nil {
method, err := chrome.New()
method, err := chrome.New(chrome.WithChromeVersion(ctx, 0, nil))
if err != nil {
return fmt.Errorf("new chrome login method: %w", err)
}
Expand All @@ -65,7 +65,7 @@ func NewAuthenticatedClient(ctx context.Context, client *http.Client, config *Co
config = new(Config)
}

if err := config.SetupDefault(); err != nil {
if err := config.SetupDefault(ctx); err != nil {
return nil, fmt.Errorf("setup default config: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ var _ = ginkgo.Describe("Client", func() {

var _ = ginkgo.Describe("Config", func() {
ginkgo.Describe("SetupDefault", func() {
ginkgo.It("Should work", func() {
ginkgo.It("Should work", func(ctx ginkgo.SpecContext) {
var config digiposte.Config

gomega.Expect(config.SetupDefault()).To(gomega.Succeed())
gomega.Expect(config.SetupDefault(ctx)).To(gomega.Succeed())
gomega.Expect(config.APIURL).To(gomega.Equal(settings.DefaultAPIURL))
gomega.Expect(config.DocumentURL).To(gomega.Equal(settings.DefaultDocumentURL))
gomega.Expect(config.LoginMethod).ToNot(gomega.BeNil())
Expand Down

0 comments on commit 1cf45a2

Please sign in to comment.