Skip to content

Commit

Permalink
return error when creating client
Browse files Browse the repository at this point in the history
  • Loading branch information
rokostik committed Jan 24, 2024
1 parent 32f456b commit 3ff7bdc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
29 changes: 18 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package friendlycaptcha

import (
"fmt"
"net/http"
)

type ClientOption func(*Client)
type ClientOption func(*Client) error

// A client for the Friendly Captcha API, see also the API docs at https://developer.friendlycaptcha.com
type Client struct {
Expand All @@ -31,7 +32,7 @@ const (
euSiteverifyEndpointURL = "https://eu.frcapi.com/api/v2/captcha/siteverify"
)

func NewClient(opts ...ClientOption) *Client {
func NewClient(opts ...ClientOption) (*Client, error) {
const (
defaultSiteverifyEndpoint = globalSiteverifyEndpointURL
)
Expand All @@ -43,36 +44,41 @@ func NewClient(opts ...ClientOption) *Client {

// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
// *Client as the argument
opt(c)
// Call the option giving the instantiated *Client as the argument
err := opt(c)
if err != nil {
return nil, err
}
}

if c.APIKey == "" {
panic("You must set your Friendly Captcha APIKey using `WithAPIKey()` when creating a new client")
return nil, fmt.Errorf("you must set your Friendly Captcha API key using `WithAPIKey()` when creating a new client")
}

return c
return c, nil
}

func WithAPIKey(apiKey string) ClientOption {
return func(c *Client) {
return func(c *Client) error {
c.APIKey = apiKey
return nil
}
}

func WithSitekey(sitekey string) ClientOption {
return func(c *Client) {
return func(c *Client) error {
c.Sitekey = sitekey
return nil
}
}

// In strict mode only strictly verified captcha response are allowed. If your API key is invalid or your server can not reach the API endpoint all requests will be rejected.
//
// This defaults to `false`.
func WithStrictMode(strict bool) ClientOption {
return func(c *Client) {
return func(c *Client) error {
c.Strict = strict
return nil
}
}

Expand All @@ -84,7 +90,8 @@ func WithSiteverifyEndpoint(siteverifyEndpoint string) ClientOption {
siteverifyEndpoint = euSiteverifyEndpointURL
}

return func(c *Client) {
return func(c *Client) error {
c.SiteverifyEndpoint = siteverifyEndpoint
return nil
}
}
8 changes: 6 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func main() {
if siteverifyEndpoint != "" {
opts = append(opts, friendlycaptcha.WithSiteverifyEndpoint(siteverifyEndpoint)) // optional, defaults to "global"
}
frcClient := friendlycaptcha.NewClient(opts...)
frcClient, err := friendlycaptcha.NewClient(opts...)
if err != nil {
log.Fatalf("Failed to create Friendly Captcha client: %s", err)
}

tmpl := template.Must(template.ParseFiles("demo.html"))

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -105,7 +109,7 @@ func main() {
return
}
})
log.Printf("Starting server on localhost port 8844 (http://localhost:8844)")

log.Printf("Starting server on localhost port 8844 (http://localhost:8844)")
http.ListenAndServe(":8844", nil)
}

0 comments on commit 3ff7bdc

Please sign in to comment.