Skip to content

Commit

Permalink
Visitor Identification module added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sehbaz committed Dec 19, 2023
1 parent 4f48fed commit 1a83d6b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions gohubspot.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Client struct {

CRM *CRM
Marketing *Marketing

VisitorIdentification VisitorIdentificationService
}

// RequestPayload is common request structure for HubSpot APIs.
Expand Down Expand Up @@ -68,6 +70,11 @@ func NewClient(setAuthMethod AuthMethod, opts ...Option) (*Client, error) {
apiVersion: defaultAPIVersion,
}

// Initialize the VisitorIdentification field
c.VisitorIdentification = &VisitorIdentificationServiceOp{
client: c,
}

// Set the authentication method specified by the argument.
// Authentication method is either APIKey or OAuth.
setAuthMethod(c)
Expand Down
31 changes: 31 additions & 0 deletions visitor_identification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hubspot

const (
visitorIdentificationBasePath = "/conversations/v3/visitor-identification"
)

// VisitorIdentificationService is an interface of visitor identification endpoints of the HubSpot API.
type VisitorIdentificationService interface {
GenerateIdentificationToken(request interface{}) (*IdentificationTokenResponse, error)
}

// VisitorIdentificationServiceOp handles communication with the Visitor Identification related methods of the HubSpot API.
type VisitorIdentificationServiceOp struct {
client *Client
}

type IdentificationTokenResponse struct {
Token string `json:"token"`
}

// Create creates a Identification Token.
// In order to bind the created content, a structure must be specified as an argument.
// When using custom fields, please embed hubspot.request in your own structure.
func (s *VisitorIdentificationServiceOp) GenerateIdentificationToken(request interface{}) (*IdentificationTokenResponse, error) {
response := &IdentificationTokenResponse{}
path := visitorIdentificationBasePath + "/tokens/create"
if err := s.client.Post(path, request, response); err != nil {
return nil, err
}
return response, nil
}
31 changes: 31 additions & 0 deletions visitor_identification_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package hubspot

import (
"os"
"testing"
)

type IdentificationTokenRequest struct {
Email string `json:"email"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}

func TestGenerateIdentificationToken(t *testing.T) {
//t.SkipNow() // Remove this line when you're ready to run the test

cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
request := &IdentificationTokenRequest{
Email: "test@example.com",
FirstName: "Test",
}

response, err := cli.VisitorIdentification.GenerateIdentificationToken(request)
if err != nil {
t.Error(err)
}

if response.Token == "" {
t.Errorf("expected response.Token to be not empty")
}
}

0 comments on commit 1a83d6b

Please sign in to comment.