From 1a83d6bcd5cae512e06bd4534c86c97c8738fd06 Mon Sep 17 00:00:00 2001 From: sehbazm Date: Tue, 19 Dec 2023 18:19:53 +0000 Subject: [PATCH 1/6] Visitor Identification module added. --- gohubspot.go | 7 +++++++ visitor_identification.go | 31 +++++++++++++++++++++++++++++++ visitor_identification_test.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 visitor_identification.go create mode 100644 visitor_identification_test.go diff --git a/gohubspot.go b/gohubspot.go index c0d30a7..219166c 100644 --- a/gohubspot.go +++ b/gohubspot.go @@ -36,6 +36,8 @@ type Client struct { CRM *CRM Marketing *Marketing + + VisitorIdentification VisitorIdentificationService } // RequestPayload is common request structure for HubSpot APIs. @@ -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) diff --git a/visitor_identification.go b/visitor_identification.go new file mode 100644 index 0000000..d803f3b --- /dev/null +++ b/visitor_identification.go @@ -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 +} diff --git a/visitor_identification_test.go b/visitor_identification_test.go new file mode 100644 index 0000000..a79f70c --- /dev/null +++ b/visitor_identification_test.go @@ -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") + } +} From 9b6ff67ff313675cfa68768b1c33f326151c8f30 Mon Sep 17 00:00:00 2001 From: sehbazm Date: Thu, 21 Dec 2023 16:13:57 +0000 Subject: [PATCH 2/6] visitor identification test file updated. --- visitor_identification_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/visitor_identification_test.go b/visitor_identification_test.go index a79f70c..3c34c73 100644 --- a/visitor_identification_test.go +++ b/visitor_identification_test.go @@ -12,7 +12,7 @@ type IdentificationTokenRequest struct { } func TestGenerateIdentificationToken(t *testing.T) { - //t.SkipNow() // Remove this line when you're ready to run the test + t.SkipNow() cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN"))) request := &IdentificationTokenRequest{ From 1920661ced8d838a6a6b31afaf3523eaa08d2bda Mon Sep 17 00:00:00 2001 From: sehbazm Date: Tue, 2 Jan 2024 19:58:48 +0000 Subject: [PATCH 3/6] Code refactored based on feedback. --- gohubspot.go | 4 +--- visitor_identification.go | 18 ++++++++++++------ visitor_identification_test.go | 11 +++-------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/gohubspot.go b/gohubspot.go index 219166c..d8bada4 100644 --- a/gohubspot.go +++ b/gohubspot.go @@ -71,9 +71,7 @@ func NewClient(setAuthMethod AuthMethod, opts ...Option) (*Client, error) { } // Initialize the VisitorIdentification field - c.VisitorIdentification = &VisitorIdentificationServiceOp{ - client: c, - } + c.VisitorIdentification = &VisitorIdentificationServiceOp{client: c} // Set the authentication method specified by the argument. // Authentication method is either APIKey or OAuth. diff --git a/visitor_identification.go b/visitor_identification.go index d803f3b..51696dc 100644 --- a/visitor_identification.go +++ b/visitor_identification.go @@ -4,11 +4,6 @@ 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 @@ -18,10 +13,21 @@ type IdentificationTokenResponse struct { Token string `json:"token"` } +type IdentificationTokenRequest struct { + FirstName string + LastName string + Email string +} + +// VisitorIdentificationService is an interface of visitor identification endpoints of the HubSpot API. +type VisitorIdentificationService interface { + GenerateIdentificationToken(request IdentificationTokenRequest) (*IdentificationTokenResponse, error) +} + // 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) { +func (s *VisitorIdentificationServiceOp) GenerateIdentificationToken(request IdentificationTokenRequest) (*IdentificationTokenResponse, error) { response := &IdentificationTokenResponse{} path := visitorIdentificationBasePath + "/tokens/create" if err := s.client.Post(path, request, response); err != nil { diff --git a/visitor_identification_test.go b/visitor_identification_test.go index 3c34c73..6f4e6c2 100644 --- a/visitor_identification_test.go +++ b/visitor_identification_test.go @@ -5,19 +5,14 @@ import ( "testing" ) -type IdentificationTokenRequest struct { - Email string `json:"email"` - FirstName string `json:"firstName"` - LastName string `json:"lastName"` -} - func TestGenerateIdentificationToken(t *testing.T) { t.SkipNow() cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN"))) - request := &IdentificationTokenRequest{ - Email: "test@example.com", + request := IdentificationTokenRequest{ FirstName: "Test", + LastName: "User", + Email: "test@example.com", } response, err := cli.VisitorIdentification.GenerateIdentificationToken(request) From 79d951bca457b6821b0d0c8e92fd0500d84aa1c0 Mon Sep 17 00:00:00 2001 From: sehbazm Date: Wed, 3 Jan 2024 17:44:56 +0000 Subject: [PATCH 4/6] Conversation visitor identification code refactored & all test cases failure issue resolved. --- conversation.go | 14 ++++++++++++ conversation_visitor_identity.go | 34 +++++++++++++++++++++++++++++ export_test.go | 5 +++-- gohubspot.go | 11 ++++------ gohubspot_test.go | 1 + visitor_identification.go | 37 -------------------------------- visitor_identification_test.go | 26 ---------------------- 7 files changed, 56 insertions(+), 72 deletions(-) create mode 100644 conversation.go create mode 100644 conversation_visitor_identity.go delete mode 100644 visitor_identification.go delete mode 100644 visitor_identification_test.go diff --git a/conversation.go b/conversation.go new file mode 100644 index 0000000..80aed31 --- /dev/null +++ b/conversation.go @@ -0,0 +1,14 @@ +package hubspot + +type Conversation struct { + VisitorIdentification VisitorIdentificationService + IdentificationTokenRequest IdentificationTokenRequest +} + +func newConversation(c *Client) *Conversation { + return &Conversation{ + VisitorIdentification: &VisitorIdentificationServiceOp{ + client: c, + }, + } +} diff --git a/conversation_visitor_identity.go b/conversation_visitor_identity.go new file mode 100644 index 0000000..b81d797 --- /dev/null +++ b/conversation_visitor_identity.go @@ -0,0 +1,34 @@ +package hubspot + +const ( + visitorIdentificationBasePath = "/conversations/v3/visitor-identification" +) + +type IdentificationTokenResponse struct { + Token string `json:"token"` +} + +type IdentificationTokenRequest struct { + FirstName string `json:"firstname"` + LastName string `json:"lastname"` + Email string `json:"email"` +} + +type VisitorIdentificationService interface { + GenerateIdentificationToken(option IdentificationTokenRequest) (*IdentificationTokenResponse, error) +} + +type VisitorIdentificationServiceOp struct { + client *Client +} + +var _ VisitorIdentificationService = (*VisitorIdentificationServiceOp)(nil) + +func (s *VisitorIdentificationServiceOp) GenerateIdentificationToken(option IdentificationTokenRequest) (*IdentificationTokenResponse, error) { + response := &IdentificationTokenResponse{} + path := visitorIdentificationBasePath + "/tokens/create" + if err := s.client.Post(path, option, response); err != nil { + return nil, err + } + return response, nil +} diff --git a/export_test.go b/export_test.go index aca5eee..83d8f2a 100644 --- a/export_test.go +++ b/export_test.go @@ -14,8 +14,9 @@ var ( ) var ( - ExportNewCRM = newCRM - ExportNewMarketing = newMarketing + ExportNewCRM = newCRM + ExportNewMarketing = newMarketing + ExportNewConversation = newConversation ExportSetupProperties = (*RequestQueryOption).setupProperties diff --git a/gohubspot.go b/gohubspot.go index d8bada4..e026bc6 100644 --- a/gohubspot.go +++ b/gohubspot.go @@ -34,10 +34,9 @@ type Client struct { authenticator Authenticator - CRM *CRM - Marketing *Marketing - - VisitorIdentification VisitorIdentificationService + CRM *CRM + Marketing *Marketing + Conversation *Conversation } // RequestPayload is common request structure for HubSpot APIs. @@ -70,9 +69,6 @@ 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) @@ -84,6 +80,7 @@ func NewClient(setAuthMethod AuthMethod, opts ...Option) (*Client, error) { // Since the baseURL and apiVersion may change, initialize the service after applying the options. c.CRM = newCRM(c) c.Marketing = newMarketing(c) + c.Conversation = newConversation(c) return c, nil } diff --git a/gohubspot_test.go b/gohubspot_test.go index 5d104b4..87e6553 100644 --- a/gohubspot_test.go +++ b/gohubspot_test.go @@ -152,6 +152,7 @@ func TestNewClient(t *testing.T) { want.ExportSetBaseURL(tt.settings.baseURL) want.CRM = hubspot.ExportNewCRM(want) want.Marketing = hubspot.ExportNewMarketing(want) + want.Conversation = hubspot.ExportNewConversation(want) tt.settings.authMethod(want) } diff --git a/visitor_identification.go b/visitor_identification.go deleted file mode 100644 index 51696dc..0000000 --- a/visitor_identification.go +++ /dev/null @@ -1,37 +0,0 @@ -package hubspot - -const ( - visitorIdentificationBasePath = "/conversations/v3/visitor-identification" -) - -// 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"` -} - -type IdentificationTokenRequest struct { - FirstName string - LastName string - Email string -} - -// VisitorIdentificationService is an interface of visitor identification endpoints of the HubSpot API. -type VisitorIdentificationService interface { - GenerateIdentificationToken(request IdentificationTokenRequest) (*IdentificationTokenResponse, error) -} - -// 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 IdentificationTokenRequest) (*IdentificationTokenResponse, error) { - response := &IdentificationTokenResponse{} - path := visitorIdentificationBasePath + "/tokens/create" - if err := s.client.Post(path, request, response); err != nil { - return nil, err - } - return response, nil -} diff --git a/visitor_identification_test.go b/visitor_identification_test.go deleted file mode 100644 index 6f4e6c2..0000000 --- a/visitor_identification_test.go +++ /dev/null @@ -1,26 +0,0 @@ -package hubspot - -import ( - "os" - "testing" -) - -func TestGenerateIdentificationToken(t *testing.T) { - t.SkipNow() - - cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN"))) - request := IdentificationTokenRequest{ - FirstName: "Test", - LastName: "User", - Email: "test@example.com", - } - - response, err := cli.VisitorIdentification.GenerateIdentificationToken(request) - if err != nil { - t.Error(err) - } - - if response.Token == "" { - t.Errorf("expected response.Token to be not empty") - } -} From e8afb480d69531b869809d88ae2a2595fe31a89e Mon Sep 17 00:00:00 2001 From: sehbazm Date: Sun, 7 Jan 2024 17:57:31 +0000 Subject: [PATCH 5/6] Few more changes added based on code review feedback. --- conversation.go | 10 +++++++--- conversation_visitor_identity.go | 9 +++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/conversation.go b/conversation.go index 80aed31..e66bd61 100644 --- a/conversation.go +++ b/conversation.go @@ -1,14 +1,18 @@ package hubspot +const ( + visitorIdentificationBasePath = "/conversations/v3/visitor-identification" +) + type Conversation struct { - VisitorIdentification VisitorIdentificationService - IdentificationTokenRequest IdentificationTokenRequest + VisitorIdentification VisitorIdentificationService } func newConversation(c *Client) *Conversation { return &Conversation{ VisitorIdentification: &VisitorIdentificationServiceOp{ - client: c, + client: c, + basePath: visitorIdentificationBasePath, }, } } diff --git a/conversation_visitor_identity.go b/conversation_visitor_identity.go index b81d797..b51d837 100644 --- a/conversation_visitor_identity.go +++ b/conversation_visitor_identity.go @@ -1,9 +1,5 @@ package hubspot -const ( - visitorIdentificationBasePath = "/conversations/v3/visitor-identification" -) - type IdentificationTokenResponse struct { Token string `json:"token"` } @@ -19,14 +15,15 @@ type VisitorIdentificationService interface { } type VisitorIdentificationServiceOp struct { - client *Client + client *Client + basePath string } var _ VisitorIdentificationService = (*VisitorIdentificationServiceOp)(nil) func (s *VisitorIdentificationServiceOp) GenerateIdentificationToken(option IdentificationTokenRequest) (*IdentificationTokenResponse, error) { response := &IdentificationTokenResponse{} - path := visitorIdentificationBasePath + "/tokens/create" + path := s.basePath + "/tokens/create" if err := s.client.Post(path, option, response); err != nil { return nil, err } From a96869a4a73655074b4b35dcc7c0d16a06710125 Mon Sep 17 00:00:00 2001 From: sehbazm Date: Sun, 7 Jan 2024 17:57:39 +0000 Subject: [PATCH 6/6] Few more changes added based on code review feedback. --- conversation_visitor_identity.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversation_visitor_identity.go b/conversation_visitor_identity.go index b51d837..f8866d0 100644 --- a/conversation_visitor_identity.go +++ b/conversation_visitor_identity.go @@ -5,8 +5,8 @@ type IdentificationTokenResponse struct { } type IdentificationTokenRequest struct { - FirstName string `json:"firstname"` - LastName string `json:"lastname"` + FirstName string `json:"firstName"` + LastName string `json:"lastName"` Email string `json:"email"` }