web

package
v2.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2024 License: MIT Imports: 6 Imported by: 0

README

goutils/v2/web

The web package is a collection of utility functions designed to simplify common web tasks.


Table of contents


Functions

CancelAll(...func())
CancelAll(...func())

CancelAll executes all provided cancel functions. It is typically used for cleaning up or aborting operations that were started earlier and can be cancelled.

Note: The caller is responsible for handling any errors that may occur during the execution of the cancel functions.

Parameters:

cancels: A slice of cancel functions, each of type func(). These are typically functions returned by context.WithCancel, or similar functions that provide a way to cancel an operation.


GetRandomWait(int)
GetRandomWait(int) time.Duration, error

GetRandomWait returns a random duration in seconds between the specified minWait and maxWait durations. The function takes the minimum and maximum wait times as arguments, creates a new random number generator with a seed based on the current Unix timestamp, and calculates the random wait time within the given range.

Parameters:

minWait: The minimum duration to wait. maxWait: The maximum duration to wait.

Returns:

time.Duration: A random duration between minWait and maxWait. error: An error if the generation of the random wait time fails.


IsLogMeOutEnabled(*LoginOptions)
IsLogMeOutEnabled(*LoginOptions) bool

IsLogMeOutEnabled checks if the option to log out the user after login is enabled in the provided login options.

Parameters:

opts: A pointer to a LoginOptions instance.

Returns:

bool: A boolean indicating whether the user is to be logged out after login.


IsTwoFacEnabled(*LoginOptions)
IsTwoFacEnabled(*LoginOptions) bool

IsTwoFacEnabled checks if two-factor authentication is enabled in the provided login options.

Parameters:

opts: A pointer to a LoginOptions instance.

Returns:

bool: A boolean indicating whether two-factor authentication is enabled.


SetLoginOptions(...LoginOption)
SetLoginOptions(...LoginOption) *LoginOptions

SetLoginOptions applies provided login options to a new LoginOptions instance with default values and returns a pointer to this instance. This function is primarily used to configure login behavior in the LoginAccount function.

Parameters:

options: A variadic set of LoginOption functions. Each LoginOption is a function that takes a pointer to a LoginOptions struct and modifies it in place.

Returns:

*LoginOptions: A pointer to a LoginOptions struct that has been configured with the provided options.


Wait(float64)
Wait(float64) time.Duration, error

Wait generates a random period of time anchored to a given input value.

Parameters:

near: A float64 value that serves as the base value for generating the random wait time.

Returns:

time.Duration: The calculated random wait time in milliseconds. error: An error if the generation of the random wait time fails.

The function is useful for simulating more human-like interaction in the context of a web application. It first calculates a 'zoom' value by dividing the input 'near' by 10. Then, a random number is generated in the range of [0, zoom), and added to 95% of 'near'. This sum is then converted to a time duration in milliseconds and returned.


WithLogout(bool)
WithLogout(bool) LoginOption

WithLogout is a function that returns a LoginOption function which sets the logMeOut option.

Parameters:

enabled: Determines if the user should be logged out after login.

Returns:

LoginOption: A function that modifies the logMeOut option of a LoginOptions struct.


WithTwoFac(bool)
WithTwoFac(bool) LoginOption

WithTwoFac is a function that returns a LoginOption function which sets the twoFacEnabled option.

Parameters:

enabled: Determines if two-factor authentication should be enabled during login.

Returns:

LoginOption: A function that modifies the twoFacEnabled option of a LoginOptions struct.


Installation

To use the goutils/v2/web package, you first need to install it. Follow the steps below to install via go get.

go get github.com/l50/goutils/v2/web

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/l50/goutils/v2/web"

Tests

To ensure the package is working correctly, run the following command to execute the tests for goutils/v2/web:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CancelAll

func CancelAll(cancels ...func())

CancelAll executes all provided cancel functions. It is typically used for cleaning up or aborting operations that were started earlier and can be cancelled.

Note: The caller is responsible for handling any errors that may occur during the execution of the cancel functions.

**Parameters:**

cancels: A slice of cancel functions, each of type func(). These are typically functions returned by context.WithCancel, or similar functions that provide a way to cancel an operation.

Example
package main

import (
	"context"

	"github.com/l50/goutils/v2/web"
)

func main() {
	var cancels []func()
	_, cancel := context.WithCancel(context.Background())
	cancels = append(cancels, cancel)

	// Later, when all operations need to be cancelled:
	web.CancelAll(cancels...)
}
Output:

func GetRandomWait

func GetRandomWait(min, max int) (time.Duration, error)

GetRandomWait returns a random duration in seconds between the specified minWait and maxWait durations. The function takes the minimum and maximum wait times as arguments, creates a new random number generator with a seed based on the current Unix timestamp, and calculates the random wait time within the given range.

**Parameters:**

minWait: The minimum duration to wait. maxWait: The maximum duration to wait.

**Returns:**

time.Duration: A random duration between minWait and maxWait. error: An error if the generation of the random wait time fails.

Example
package main

import (
	"github.com/l50/goutils/v2/web"
)

func main() {
	minWait := 2
	maxWait := 6
	randomWaitTime, _ := web.GetRandomWait(minWait, maxWait)
	_ = randomWaitTime
}
Output:

func IsLogMeOutEnabled added in v2.0.3

func IsLogMeOutEnabled(opts *LoginOptions) bool

IsLogMeOutEnabled checks if the option to log out the user after login is enabled in the provided login options.

**Parameters:**

opts: A pointer to a LoginOptions instance.

**Returns:**

bool: A boolean indicating whether the user is to be logged out after login.

Example
package main

import (
	"github.com/l50/goutils/v2/web"
)

func main() {
	options := []web.LoginOption{
		web.WithLogout(false),
	}
	loginOpts := web.SetLoginOptions(options...)
	isLogMeOutEnabled := web.IsLogMeOutEnabled(loginOpts)
	_ = isLogMeOutEnabled
}
Output:

func IsTwoFacEnabled added in v2.0.3

func IsTwoFacEnabled(opts *LoginOptions) bool

IsTwoFacEnabled checks if two-factor authentication is enabled in the provided login options.

**Parameters:**

opts: A pointer to a LoginOptions instance.

**Returns:**

bool: A boolean indicating whether two-factor authentication is enabled.

Example
package main

import (
	"github.com/l50/goutils/v2/web"
)

func main() {
	options := []web.LoginOption{
		web.WithTwoFac(false),
	}
	loginOpts := web.SetLoginOptions(options...)
	isTwoFacEnabled := web.IsTwoFacEnabled(loginOpts)
	_ = isTwoFacEnabled
}
Output:

func Wait

func Wait(near float64) (time.Duration, error)

Wait generates a random period of time anchored to a given input value.

**Parameters:**

near: A float64 value that serves as the base value for generating the random wait time.

**Returns:**

time.Duration: The calculated random wait time in milliseconds. error: An error if the generation of the random wait time fails.

The function is useful for simulating more human-like interaction in the context of a web application. It first calculates a 'zoom' value by dividing the input 'near' by 10. Then, a random number is generated in the range of [0, zoom), and added to 95% of 'near'. This sum is then converted to a time duration in milliseconds and returned.

Types

type Browser

type Browser struct {
	Driver  interface{}
	Cancels []func()
}

Browser defines parameters used for driving a web browser.

**Attributes:**

Driver: An interface that could be implemented for various browsers. Cancels: A list of cancel functions.

type Credential

type Credential struct {
	User       string
	Password   string
	TwoFacCode string
}

Credential contains the information that makes up a credential to authenticate to an application.

type Driver

type Driver interface {
	GetContext() context.Context
	SetContext(context.Context)
	GetOptions() []chromedp.ExecAllocatorOption
	SetOptions([]chromedp.ExecAllocatorOption)
}

Driver is an interface that can be implemented for various browsers in go. It should service to capture information such as context and browser options.

**Methods:**

GetContext: Retrieves the context. SetContext: Sets the context. GetOptions: Retrieves the browser options. SetOptions: Sets the browser options.

type FormField

type FormField struct {
	Name     string `json:"-"`
	Selector string `json:"-"`
}

FormField contains a form field name and its associated selector.

type LoginOption

type LoginOption func(*LoginOptions)

LoginOption is a type for functions that modify the login options. These functions take a pointer to a LoginOptions struct and modify it in place.

func WithLogout

func WithLogout(enabled bool) LoginOption

WithLogout is a function that returns a LoginOption function which sets the logMeOut option.

**Parameters:**

enabled: Determines if the user should be logged out after login.

**Returns:**

LoginOption: A function that modifies the logMeOut option of a LoginOptions struct.

func WithTwoFac

func WithTwoFac(enabled bool) LoginOption

WithTwoFac is a function that returns a LoginOption function which sets the twoFacEnabled option.

**Parameters:**

enabled: Determines if two-factor authentication should be enabled during login.

**Returns:**

LoginOption: A function that modifies the twoFacEnabled option of a LoginOptions struct.

type LoginOptions added in v2.0.4

type LoginOptions struct {
	// contains filtered or unexported fields
}

LoginOptions holds the configurations for the login process.

**Attributes:**

twoFacEnabled: Determines if two-factor authentication is enabled during login. logMeOut: Determines if the user is logged out after login.

func SetLoginOptions added in v2.0.3

func SetLoginOptions(options ...LoginOption) *LoginOptions

SetLoginOptions applies provided login options to a new LoginOptions instance with default values and returns a pointer to this instance. This function is primarily used to configure login behavior in the LoginAccount function.

**Parameters:**

options: A variadic set of LoginOption functions. Each LoginOption is a function that takes a pointer to a LoginOptions struct and modifies it in place.

**Returns:**

*LoginOptions: A pointer to a LoginOptions struct that has been configured with the provided options.

Example
package main

import (
	"github.com/l50/goutils/v2/web"
)

func main() {
	options := []web.LoginOption{
		web.WithTwoFac(false),
		web.WithLogout(true),
	}
	loginOpts := web.SetLoginOptions(options...)
	_ = loginOpts // use loginOpts
}
Output:

type Session

type Session struct {
	Credential Credential
	Driver     interface{}
}

Session contains parameters associated with maintaining a session.

type Site

type Site struct {
	LoginURL string
	Session  Session
	Debug    bool
}

Site is used to define parameters for interacting with web applications.

**Attributes:**

LoginURL: The URL for login. Session: The session information. Debug: Debug flag.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL