Skip to content

Commit

Permalink
Add support for crm/v3/schemas and crm/v3/properties
Browse files Browse the repository at this point in the history
  • Loading branch information
sblackstone committed Mar 13, 2023
1 parent e0c0f38 commit b008e61
Show file tree
Hide file tree
Showing 5 changed files with 446 additions and 3 deletions.
16 changes: 13 additions & 3 deletions crm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ const (
)

type CRM struct {
Contact ContactService
Deal DealService
Imports CrmImportsService
Contact ContactService
Deal DealService
Imports CrmImportsService
Schemas CrmSchemasService
Properties CrmPropertiesService
}

func newCRM(c *Client) *CRM {
Expand All @@ -29,5 +31,13 @@ func newCRM(c *Client) *CRM {
crmImportsPath: fmt.Sprintf("%s/%s", crmPath, crmImportsBasePath),
client: c,
},
Schemas: &CrmSchemasServiceOp{
crmSchemasPath: fmt.Sprintf("%s/%s", crmPath, crmSchemasPath),
client: c,
},
Properties: &CrmPropertiesServiceOp{
crmPropertiesPath: fmt.Sprintf("%s/%s", crmPath, crmPropertiesPath),
client: c,
},
}
}
66 changes: 66 additions & 0 deletions crm_properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package hubspot

import "fmt"

const (
crmPropertiesPath = "properties"
)

// CrmPropertiesService is an interface of CRM crm/v3/properties interface..
// Reference: https://developers.hubspot.com/docs/api/crm/properties
type CrmPropertiesService interface {
List(string) (map[string]interface{}, error)
Create(string, map[string]interface{}) (map[string]interface{}, error)
Get(string, string) (map[string]interface{}, error)
Delete(string, string) error
Update(string, string, map[string]interface{}) (map[string]interface{}, error)
}

// CrmPropertiesServiceOp handles communication with the CRM properties endpoint.
type CrmPropertiesServiceOp struct {
client *Client
crmPropertiesPath string
}

var _ CrmPropertiesService = (*CrmPropertiesServiceOp)(nil)

func (s *CrmPropertiesServiceOp) List(objectType string) (map[string]interface{}, error) {
path := fmt.Sprintf("%s/%s", s.crmPropertiesPath, objectType)
resource := make(map[string]interface{})
if err := s.client.Get(path, &resource, nil); err != nil {
return nil, err
}
return resource, nil
}

func (s *CrmPropertiesServiceOp) Get(objectType, propertyName string) (map[string]interface{}, error) {
path := fmt.Sprintf("%s/%s/%s", s.crmPropertiesPath, objectType, propertyName)
resource := make(map[string]interface{})
if err := s.client.Get(path, &resource, nil); err != nil {
return nil, err
}
return resource, nil
}

func (s *CrmPropertiesServiceOp) Create(objectType string, reqData map[string]interface{}) (map[string]interface{}, error) {
resource := make(map[string]interface{})
path := fmt.Sprintf("%s/%s", s.crmPropertiesPath, objectType)
if err := s.client.Post(path, reqData, &resource); err != nil {
return nil, err
}
return resource, nil
}

func (s *CrmPropertiesServiceOp) Delete(objectType string, propertyName string) error {
path := fmt.Sprintf("%s/%s/%s", s.crmPropertiesPath, objectType, propertyName)
return s.client.Delete(path)
}

func (s *CrmPropertiesServiceOp) Update(objectType string, propertyName string, reqData map[string]interface{}) (map[string]interface{}, error) {
resource := make(map[string]interface{})
path := fmt.Sprintf("%s/%s/%s", s.crmPropertiesPath, objectType, propertyName)
if err := s.client.Patch(path, reqData, &resource); err != nil {
return nil, err
}
return resource, nil
}
82 changes: 82 additions & 0 deletions crm_properties_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package hubspot

import (
"os"
"testing"
)

func TestListCrmProperties(t *testing.T) {
t.SkipNow()
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
// Use crm_schemas:TestCreate() to generate this...
res, err := cli.CRM.Properties.List("cars")
if err != nil {
t.Error(err)
}

if _, ok := res["results"]; !ok {
t.Error("expected results key in reslt")
}

}

func TestGetCrmProperty(t *testing.T) {
t.SkipNow()

cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
// Use crm_schemas:TestCreate() to generate this...
res, err := cli.CRM.Properties.Get("cars", "model")
if err != nil {
t.Error(err)
}

if name, ok := res["name"]; !ok {
t.Error("expected result to have a key named name")
} else if name != "model" {
t.Errorf("expedcted name to be model, got %s", name)
}

}

func TestCreateProperty(t *testing.T) {
t.SkipNow()
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
newProp := make(map[string]interface{})
newProp["name"] = "mileage"
newProp["label"] = "Mileage"
newProp["type"] = "number"
newProp["fieldType"] = "number"
newProp["groupName"] = "cars_information"

_, err := cli.CRM.Properties.Create("cars", newProp)
if err != nil {
t.Error(err)
return
}
}

func TestUpdateProperty(t *testing.T) {
t.SkipNow()
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
updateProp := make(map[string]interface{})
updateProp["label"] = "Mileage - updated"

res, err := cli.CRM.Properties.Update("cars", "mileage", updateProp)
if err != nil {
t.Error(err)
return
}

if newLabel, ok := res["label"]; !ok || newLabel != "Mileage - updated" {
t.Errorf("expected updated label")
}
}

func TestDeleteProperty(t *testing.T) {
t.SkipNow()
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
err := cli.CRM.Properties.Delete("cars", "mileage")
if err != nil {
t.Error(err)
}
}
64 changes: 64 additions & 0 deletions crm_schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package hubspot

import "fmt"

const (
crmSchemasPath = "schemas"
)

// CrmSchemas is an interface of CRM crm/v3/schemas interface..
// Reference: https://developers.hubspot.com/docs/api/crm/crm-custom-objects
type CrmSchemasService interface {
List() (map[string]interface{}, error)
Create(map[string]interface{}) (map[string]interface{}, error)
Get(string) (map[string]interface{}, error)
Delete(string, bool) error
Update(string, map[string]interface{}) (map[string]interface{}, error)
}

// CrmSchemasServiceOp handles communication with the CRM schemas endpoint.
type CrmSchemasServiceOp struct {
client *Client
crmSchemasPath string
}

var _ CrmSchemasService = (*CrmSchemasServiceOp)(nil)

func (s *CrmSchemasServiceOp) List() (map[string]interface{}, error) {
resource := make(map[string]interface{})
if err := s.client.Get(s.crmSchemasPath, &resource, nil); err != nil {
return nil, err
}
return resource, nil
}

func (s *CrmSchemasServiceOp) Create(reqData map[string]interface{}) (map[string]interface{}, error) {
resource := make(map[string]interface{})
if err := s.client.Post(s.crmSchemasPath, reqData, &resource); err != nil {
return nil, err
}
return resource, nil
}

func (s *CrmSchemasServiceOp) Get(schemaIdentifier string) (map[string]interface{}, error) {
resource := make(map[string]interface{})
path := fmt.Sprintf("%s/%s", s.crmSchemasPath, schemaIdentifier)
if err := s.client.Get(path, &resource, nil); err != nil {
return nil, err
}
return resource, nil
}

func (s *CrmSchemasServiceOp) Delete(schemaIdentifier string, archived bool) error {
path := fmt.Sprintf("%s/%s?archived=%t", s.crmSchemasPath, schemaIdentifier, archived)
return s.client.Delete(path)
}

func (s *CrmSchemasServiceOp) Update(schemaIdentifier string, reqData map[string]interface{}) (map[string]interface{}, error) {
resource := make(map[string]interface{})
path := fmt.Sprintf("%s/%s", s.crmSchemasPath, schemaIdentifier)
if err := s.client.Patch(path, reqData, &resource); err != nil {
return nil, err
}
return resource, nil
}
Loading

0 comments on commit b008e61

Please sign in to comment.