Skip to content

Commit

Permalink
Add support for properties and schemas endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
sblackstone committed Mar 31, 2023
1 parent bf87cc8 commit 02ed782
Show file tree
Hide file tree
Showing 8 changed files with 512 additions and 6 deletions.
2 changes: 1 addition & 1 deletion contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (s *ContactServiceOp) Update(contactID string, contact interface{}) (*Respo

// Delete deletes a contact.
func (s *ContactServiceOp) Delete(contactID string) error {
return s.client.Delete(s.contactPath + "/" + contactID)
return s.client.Delete(s.contactPath+"/"+contactID, nil)
}

// AssociateAnotherObj associates Contact with another HubSpot objects.
Expand Down
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,
},
}
}
106 changes: 106 additions & 0 deletions crm_properties.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package hubspot

import (
"fmt"
)

const (
crmPropertiesPath = "properties"
)

type CrmPropertiesList struct {
Results []CrmProperty
}

type CrmProperty struct {
Calculated *HsBool `json:"calculated"`
Description *HsStr `json:"description"`
DisplayOrder *HsInt `json:"displayOrder"`
ExternalOptions *HsBool `json:"externalOptions"`
FieldType *HsStr `json:"fieldType"`
FormField *HsBool `json:"formField"`
GroupName *HsStr `json:"groupName"`
HasUniqueValue *HsBool `json:"hasUniqueValue"`
Hidden *HsBool `json:"hidden"`
HubspotDefined *HsBool `json:"hubspotDefined"`
Label *HsStr `json:"label"`
ModificationMeta *CrmPropertyModificationMeta
Name *HsStr `json:"name"`
Options []*CrmPropertyOptions
Type *HsStr `json:"type"`
CreatedUserId *HsStr
UpdatedUserId *HsStr
CreatedAt *HsTime
UpdatedAt *HsTime
}

type CrmPropertyModificationMeta struct {
Archivable *HsBool `json:"archivable"`
ReadOnlyDefition *HsBool `json:"readOnlyDefinition"`
ReadOnlyValue *HsBool `json:"readOnlyValue"`
}
type CrmPropertyOptions struct {
DisplayOrder *HsInt `json:"displayOrder"`
Hidden *HsBool `json:"hidden"`
Label *HsStr `json:"label"`
Value *HsStr `json:"value"`
}

// CrmPropertiesService is an interface of CRM properties endpoints of the HubSpot API.
// Reference: https://developers.hubspot.com/docs/api/crm/properties
type CrmPropertiesService interface {
List(string) (*CrmPropertiesList, error)
Create(string, interface{}) (*CrmProperty, error)
Get(string, string) (*CrmProperty, error)
Delete(string, string) error
Update(string, string, interface{}) (*CrmProperty, 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) (*CrmPropertiesList, error) {
var resource CrmPropertiesList
path := fmt.Sprintf("%s/%s", s.crmPropertiesPath, objectType)
if err := s.client.Get(path, &resource, nil); err != nil {
return nil, err
}
return &resource, nil
}

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

func (s *CrmPropertiesServiceOp) Create(objectType string, reqData interface{}) (*CrmProperty, error) {
var resource CrmProperty
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, nil)
}

func (s *CrmPropertiesServiceOp) Update(objectType string, propertyName string, reqData interface{}) (*CrmProperty, error) {
var resource CrmProperty
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
}
84 changes: 84 additions & 0 deletions crm_properties_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package hubspot

import (
"fmt"
"os"
"testing"
"time"
)

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 len(res.Results) < 1 {
t.Error("expected len(res.Results) to be > 1")
}

}

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 *res.Name != "model" {
t.Errorf("expected res.Name to be model, got %s", res.Name)
}

}

func TestCreateProperty(t *testing.T) {
t.SkipNow()
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
newProp := &CrmProperty{
Name: NewString("mileage"),
Label: NewString("Mileage Label"),
Type: NewString("number"),
FieldType: NewString("number"),
GroupName: NewString("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"] = fmt.Sprintf("Updated Label %d", time.Now().UnixMicro())

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

if res.Label != updateProp["label"] {
t.Errorf("expected res.Label to be %s, got %s", updateProp["label"], res.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)
}
}
105 changes: 105 additions & 0 deletions crm_schemas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package hubspot

import (
"fmt"
)

const (
crmSchemasPath = "schemas"
)

type CrmSchemaAssociation struct {
Cardinality *HsStr `json:"cardinality"`
CreatedAt *HsTime `json:"createdAt"`
FromObjectTypeId *HsStr `json:"fromObjectTypeId"`
ID *HsStr `json:"id"`
InverseCardinality *HsStr `json:"inverseCardinality"`
Name *HsStr `json:"name"`
ToObjectTypeId *HsStr `json:"toObjectTypeId"`
UpdatedAt *HsTime `json:"updatedAt"`
}

type CrmSchemaLabels struct {
Plural *HsStr `json:"plural"`
Singular *HsStr `json:"singular"`
}

type CrmSchemasList struct {
Results []*CrmSchema
}

type CrmSchema struct {
Archived *HsBool `json:"archived"`
Associations []*CrmSchemaAssociation
CreatedAt *HsTime `json:"createdAt"`
FullyQualifiedName *HsStr `json:"fullyQualifiedName"`
ID *HsStr `json:"id"`
Labels CrmSchemaLabels
MetaType *HsStr `json:"metaType"`
Name *HsStr `json:"name"`
ObjectTypeId *HsStr `json:"objectTypeId"`
PrimaryDisplayProperty *HsStr `json:"primaryDisplayProperty"`
Properties []*CrmProperty `json:"properties"`
RequiredProperties []*HsStr `json:"requiredProperties"`
Restorable *HsBool `json:"restorable"`
SearchableProperties []*HsStr `json:"searchableProperties"`
SecondaryDisplayProperties []*HsStr `json:"secondaryDisplayProperties"`
UpdatedAt *HsTime `json:"updatedAt"`
}

// CrmSchemasService is an interface of CRM schemas endpoints of the HubSpot API.
// Reference: https://developers.hubspot.com/docs/api/crm/crm-custom-objects
type CrmSchemasService interface {
List() (*CrmSchemasList, error)
Create(interface{}) (*CrmSchema, error)
Get(string) (*CrmSchema, error)
Delete(string, *RequestQueryOption) error
Update(string, interface{}) (*CrmSchema, error)
}

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

var _ CrmSchemasService = (*CrmSchemasServiceOp)(nil)

func (s *CrmSchemasServiceOp) List() (*CrmSchemasList, error) {
var resource CrmSchemasList
if err := s.client.Get(s.crmSchemasPath, &resource, nil); err != nil {
return nil, err
}
return &resource, nil
}

func (s *CrmSchemasServiceOp) Create(reqData interface{}) (*CrmSchema, error) {
var resource CrmSchema
if err := s.client.Post(s.crmSchemasPath, reqData, &resource); err != nil {
return nil, err
}
return &resource, nil
}

func (s *CrmSchemasServiceOp) Get(schemaIdentifier string) (*CrmSchema, error) {
var resource CrmSchema
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, option *RequestQueryOption) error {
path := fmt.Sprintf("%s/%s", s.crmSchemasPath, schemaIdentifier)
return s.client.Delete(path, option)
}

func (s *CrmSchemasServiceOp) Update(schemaIdentifier string, reqData interface{}) (*CrmSchema, error) {
var resource CrmSchema
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 02ed782

Please sign in to comment.