Documentation
¶
Index ¶
- Constants
- Variables
- type AppData
- type Funpay
- type FunpayAuthHandler
- type FunpayClient
- func (fp *FunpayClient) Balance() int64
- func (fp *FunpayClient) BaseURL() string
- func (fp *FunpayClient) CSRFToken() string
- func (fp *FunpayClient) Cookies() []*http.Cookie
- func (fp *FunpayClient) GoldenKey() string
- func (fp *FunpayClient) Locale() Locale
- func (fp *FunpayClient) Request(ctx context.Context, requestURL string, opts ...RequestOpt) (*http.Response, error)
- func (fp *FunpayClient) RequestHTML(ctx context.Context, requestURL string, opts ...RequestOpt) (*goquery.Document, error)
- func (fp *FunpayClient) SetBaseURL(baseURL string)
- func (fp *FunpayClient) SetProxy(proxy *url.URL)
- func (fp *FunpayClient) Update(ctx context.Context) error
- func (fp *FunpayClient) UpdateLocale(ctx context.Context, locale Locale) error
- func (fp *FunpayClient) UserAgent() string
- func (fp *FunpayClient) UserID() int64
- func (fp *FunpayClient) Username() string
- type FunpayRequester
- type FunpayUpdater
- type FunpayUser
- type Locale
- type RequestOpt
- type RequestOpts
Constants ¶
const ( // Domain represents the Funpay website domain. Domain = "funpay.com" // BaseURL is the base URL for the Funpay website. BaseURL = "https://" + Domain )
const ( // CookieGoldenKey is the cookie name for golden key. CookieGoldenKey = "golden_key" // HeaderUserAgent is the header name for user agent. HeaderUserAgent = "User-Agent" FormCSRFToken = "csrf_token" )
Variables ¶
var ( // ErrTooManyRequests indicates rate limiting (HTTP 429 Too Many Requests). // Returned when exceeding API request limits. ErrTooManyRequests = errors.New("too many requests") // ErrBadStatusCode indicates unexpected HTTP response status. // Returned for any non-2xx status code not covered by other errors. ErrBadStatusCode = errors.New("bad status code") )
var ( // Returned when golden key or session cookies are invalid/expired. ErrAccountUnauthorized = errors.New("account unauthorized") )
var ( // RequestPostHeaders contains content-type, accept and x-requested-with headers. Copy these values to your headers if needed. RequestPostHeaders = map[string]string{ "content-type": "application/x-www-form-urlencoded; charset=UTF-8", "accept": "*/*", "x-requested-with": "XMLHttpRequest", } )
Functions ¶
This section is empty.
Types ¶
type AppData ¶
type AppData struct {
CSRFToken string `json:"csrf-token"`
UserID int64 `json:"userId"`
Locale Locale `json:"locale"`
}
AppData represents the object from data-app-data attribute inside the body element.
type Funpay ¶
type Funpay interface {
FunpayUser
FunpayAuthHandler
FunpayUpdater
FunpayRequester
}
type FunpayAuthHandler ¶ added in v0.2.2
type FunpayAuthHandler interface {
// CSRFToken returns CSRF token extracted from [AppData]. CSRF token updates every call [FunpayRequester.RequestHTML].
CSRFToken() string
// UserAgent returns the account's user agent provided into funpay (see [New]).
GoldenKey() string
// UserAgent returns the account's user agent provided into funpay (see [New]).
UserAgent() string
}
type FunpayClient ¶ added in v0.2.2
type FunpayClient struct {
// contains filtered or unexported fields
}
func (*FunpayClient) Balance ¶ added in v0.2.2
func (fp *FunpayClient) Balance() int64
func (*FunpayClient) BaseURL ¶ added in v0.2.2
func (fp *FunpayClient) BaseURL() string
func (*FunpayClient) CSRFToken ¶ added in v0.2.2
func (fp *FunpayClient) CSRFToken() string
func (*FunpayClient) Cookies ¶ added in v0.2.2
func (fp *FunpayClient) Cookies() []*http.Cookie
func (*FunpayClient) GoldenKey ¶ added in v0.2.2
func (fp *FunpayClient) GoldenKey() string
func (*FunpayClient) Locale ¶ added in v0.2.2
func (fp *FunpayClient) Locale() Locale
func (*FunpayClient) Request ¶ added in v0.2.2
func (fp *FunpayClient) Request(ctx context.Context, requestURL string, opts ...RequestOpt) (*http.Response, error)
func (*FunpayClient) RequestHTML ¶ added in v0.2.2
func (fp *FunpayClient) RequestHTML(ctx context.Context, requestURL string, opts ...RequestOpt) (*goquery.Document, error)
func (*FunpayClient) SetBaseURL ¶ added in v0.2.2
func (fp *FunpayClient) SetBaseURL(baseURL string)
func (*FunpayClient) SetProxy ¶ added in v0.2.2
func (fp *FunpayClient) SetProxy(proxy *url.URL)
func (*FunpayClient) Update ¶ added in v0.2.2
func (fp *FunpayClient) Update(ctx context.Context) error
func (*FunpayClient) UpdateLocale ¶ added in v0.2.2
func (fp *FunpayClient) UpdateLocale(ctx context.Context, locale Locale) error
func (*FunpayClient) UserAgent ¶ added in v0.2.2
func (fp *FunpayClient) UserAgent() string
func (*FunpayClient) UserID ¶ added in v0.2.2
func (fp *FunpayClient) UserID() int64
func (*FunpayClient) Username ¶ added in v0.2.2
func (fp *FunpayClient) Username() string
type FunpayRequester ¶ added in v0.2.2
type FunpayRequester interface {
// Cookies returns a safe copy of all session cookies.
Cookies() []*http.Cookie
// SetProxy sets or updates the HTTP proxy for the requests.
// To remove proxy and make direct connections, pass nil.
SetProxy(proxy *url.URL)
// Request executes an HTTP request using the account's session.
//
// It handles:
// - Proxy configuration (if set),
// - Locale settings (path or query param),
// - Cookie management (session and golden key),
// - User-Agent header,
// - Response status code validation,
//
// Specific returns:
// - [*http.Response] and [ErrAccountUnauthorized] if status code equals 403,
// - [*http.Response] and [ErrTooManyRequests] if status code equals 429,
// - [*http.Response] [ErrBadStatusCode] otherwise.
Request(ctx context.Context, requestURL string, opts ...RequestOpt) (*http.Response, error)
// RequestHTML calls [FunpayRequester.Request] and converting response as [*goquery.Document].
// Updates [AppData] and account info (see [FunpayUser]).
//
// Returns nil and [ErrAccountUnauthorized] if [Funpay.UserID] is zero.
RequestHTML(ctx context.Context, requestURL string, opts ...RequestOpt) (*goquery.Document, error)
}
type FunpayUpdater ¶ added in v0.2.2
type FunpayUpdater interface {
// BaseURL returns clients baseURL. Needed for tests to substitute the [BaseURL] with test server.
BaseURL() string
// SetBaseURL updates clients baseURL. Needed for tests to substitute the [BaseURL] with test server.
SetBaseURL(baseURL string)
// Update calls [FunpayRequester.RequestHTML]. You should call it every 40-60 minutes to update PHPSESSIONID cookie.
// [FunpayRequester.Request] saves all cookies from response if they are not empty.
Update(ctx context.Context) error
// UpdateLocale calls [FunpayRequester.RequestHTML] with setlocale query param.
UpdateLocale(ctx context.Context, locale Locale) error
}
type FunpayUser ¶ added in v0.4.0
type FunpayUser interface {
// UserID returns the unique identifier of the Funpay account.
// Returns 0 if the account hasn't been updated yet.
UserID() int64
// Locale returns the account's locale (see [Locale]). Must be loaded after update.
Locale() Locale
// Username returns the account's username. Must be loaded after update.
Username() string
// Balance returns the account's balance. Must be loaded after update.
Balance() int64
}
type RequestOpt ¶ added in v0.2.2
type RequestOpt func(options *RequestOpts)
RequestOpt defines a function type for modifying request options.
func RequestWithBody ¶
func RequestWithBody(body io.Reader) RequestOpt
RequestWithBody sets the request body.
func RequestWithCookies ¶
func RequestWithCookies(cookies []*http.Cookie) RequestOpt
RequestWithCookies adds additional cookies to the request. Note: Session cookies are added automatically.
func RequestWithHeaders ¶
func RequestWithHeaders(headers map[string]string) RequestOpt
RequestWithHeaders adds custom headers to the request. Headers are added in addition to default User-Agent.
func RequestWithMethod ¶
func RequestWithMethod(method string) RequestOpt
RequestWithMethod sets the HTTP method for the request. Default: GET
func RequestWithProxy ¶
func RequestWithProxy(proxy *url.URL) RequestOpt
RequestWithProxy overrides the account-level proxy for this request. To disable proxy for single request: RequestWithProxy(nil)
type RequestOpts ¶ added in v0.2.2
type RequestOpts struct {
// contains filtered or unexported fields
}
RequestOpts contains configurable parameters for HTTP requests. Used internally by [Funpay.Request] to customize request behavior.
func NewRequestOpts ¶ added in v0.2.2
func NewRequestOpts() *RequestOpts
NewRequestOpts creates request options with defaults:
- Method: GET