From 79d951bca457b6821b0d0c8e92fd0500d84aa1c0 Mon Sep 17 00:00:00 2001 From: sehbazm Date: Wed, 3 Jan 2024 17:44:56 +0000 Subject: [PATCH] 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") - } -}