Documentation ¶
Index ¶
- func CancelAll(cancels ...func())
- func GetRandomWait(min, max int) (time.Duration, error)
- func IsLogMeOutEnabled(opts *LoginOptions) bool
- func IsTwoFacEnabled(opts *LoginOptions) bool
- func Wait(near float64) (time.Duration, error)
- type Browser
- type Credential
- type Driver
- type FormField
- type LoginOption
- type LoginOptions
- type Session
- type Site
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 ¶
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 ¶
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 ¶
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 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.