Skip to content

Commit 6945458

Browse files
committed
Support for CRM properties and CRM schemas endpoints
1 parent bf87cc8 commit 6945458

8 files changed

+512
-6
lines changed

contact.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (s *ContactServiceOp) Update(contactID string, contact interface{}) (*Respo
355355

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

361361
// AssociateAnotherObj associates Contact with another HubSpot objects.

crm.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ const (
99
)
1010

1111
type CRM struct {
12-
Contact ContactService
13-
Deal DealService
14-
Imports CrmImportsService
12+
Contact ContactService
13+
Deal DealService
14+
Imports CrmImportsService
15+
Schemas CrmSchemasService
16+
Properties CrmPropertiesService
1517
}
1618

1719
func newCRM(c *Client) *CRM {
@@ -29,5 +31,13 @@ func newCRM(c *Client) *CRM {
2931
crmImportsPath: fmt.Sprintf("%s/%s", crmPath, crmImportsBasePath),
3032
client: c,
3133
},
34+
Schemas: &CrmSchemasServiceOp{
35+
crmSchemasPath: fmt.Sprintf("%s/%s", crmPath, crmSchemasPath),
36+
client: c,
37+
},
38+
Properties: &CrmPropertiesServiceOp{
39+
crmPropertiesPath: fmt.Sprintf("%s/%s", crmPath, crmPropertiesPath),
40+
client: c,
41+
},
3242
}
3343
}

crm_properties.go

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package hubspot
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
const (
8+
crmPropertiesPath = "properties"
9+
)
10+
11+
type CrmPropertiesList struct {
12+
Results []*CrmProperty
13+
}
14+
15+
type CrmProperty struct {
16+
Calculated *HsBool `json:"calculated,omitempty"`
17+
Description *HsStr `json:"description,omitempty"`
18+
DisplayOrder *HsInt `json:"displayOrder,omitempty"`
19+
ExternalOptions *HsBool `json:"externalOptions,omitempty"`
20+
FieldType *HsStr `json:"fieldType,omitempty"`
21+
FormField *HsBool `json:"formField,omitempty"`
22+
GroupName *HsStr `json:"groupName,omitempty"`
23+
HasUniqueValue *HsBool `json:"hasUniqueValue,omitempty"`
24+
Hidden *HsBool `json:"hidden,omitempty"`
25+
HubspotDefined *HsBool `json:"hubspotDefined,omitempty"`
26+
Label *HsStr `json:"label,omitempty"`
27+
ModificationMeta *CrmPropertyModificationMeta `json:"modificationMeta,omitempty"`
28+
Name *HsStr `json:"name,omitempty"`
29+
Options []*CrmPropertyOptions `json:"options,omitempty"`
30+
Type *HsStr `json:"type,omitempty"`
31+
CreatedUserId *HsStr `json:"createdUserId,omitempty"`
32+
UpdatedUserId *HsStr `json:"updatedUserId,omitempty"`
33+
CreatedAt *HsTime `json:"createdAt,omitempty"`
34+
UpdatedAt *HsTime `json:"updatedAt,omitempty"`
35+
}
36+
37+
type CrmPropertyModificationMeta struct {
38+
Archivable *HsBool `json:"archivable,omitempty"`
39+
ReadOnlyDefition *HsBool `json:"readOnlyDefinition,omitempty"`
40+
ReadOnlyValue *HsBool `json:"readOnlyValue,omitempty"`
41+
}
42+
type CrmPropertyOptions struct {
43+
DisplayOrder *HsInt `json:"displayOrder,omitempty"`
44+
Hidden *HsBool `json:"hidden,omitempty"`
45+
Label *HsStr `json:"label,omitempty"`
46+
Value *HsStr `json:"value,omitempty"`
47+
}
48+
49+
// CrmPropertiesService is an interface of CRM properties endpoints of the HubSpot API.
50+
// Reference: https://developers.hubspot.com/docs/api/crm/properties
51+
type CrmPropertiesService interface {
52+
List(objectType string) (*CrmPropertiesList, error)
53+
Create(objectType string, reqData interface{}) (*CrmProperty, error)
54+
Get(objectType string, propertyName string) (*CrmProperty, error)
55+
Delete(objectType string, propertyName string) error
56+
Update(objectType string, propertyName string, reqData interface{}) (*CrmProperty, error)
57+
}
58+
59+
// CrmPropertiesServiceOp handles communication with the CRM properties endpoint.
60+
type CrmPropertiesServiceOp struct {
61+
client *Client
62+
crmPropertiesPath string
63+
}
64+
65+
var _ CrmPropertiesService = (*CrmPropertiesServiceOp)(nil)
66+
67+
func (s *CrmPropertiesServiceOp) List(objectType string) (*CrmPropertiesList, error) {
68+
var resource CrmPropertiesList
69+
path := fmt.Sprintf("%s/%s", s.crmPropertiesPath, objectType)
70+
if err := s.client.Get(path, &resource, nil); err != nil {
71+
return nil, err
72+
}
73+
return &resource, nil
74+
}
75+
76+
func (s *CrmPropertiesServiceOp) Get(objectType, propertyName string) (*CrmProperty, error) {
77+
var resource CrmProperty
78+
path := fmt.Sprintf("%s/%s/%s", s.crmPropertiesPath, objectType, propertyName)
79+
if err := s.client.Get(path, &resource, nil); err != nil {
80+
return nil, err
81+
}
82+
return &resource, nil
83+
}
84+
85+
func (s *CrmPropertiesServiceOp) Create(objectType string, reqData interface{}) (*CrmProperty, error) {
86+
var resource CrmProperty
87+
path := fmt.Sprintf("%s/%s", s.crmPropertiesPath, objectType)
88+
if err := s.client.Post(path, reqData, &resource); err != nil {
89+
return nil, err
90+
}
91+
return &resource, nil
92+
}
93+
94+
func (s *CrmPropertiesServiceOp) Delete(objectType string, propertyName string) error {
95+
path := fmt.Sprintf("%s/%s/%s", s.crmPropertiesPath, objectType, propertyName)
96+
return s.client.Delete(path, nil)
97+
}
98+
99+
func (s *CrmPropertiesServiceOp) Update(objectType string, propertyName string, reqData interface{}) (*CrmProperty, error) {
100+
var resource CrmProperty
101+
path := fmt.Sprintf("%s/%s/%s", s.crmPropertiesPath, objectType, propertyName)
102+
if err := s.client.Patch(path, reqData, &resource); err != nil {
103+
return nil, err
104+
}
105+
return &resource, nil
106+
}

crm_properties_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package hubspot
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
"time"
8+
)
9+
10+
func TestListCrmProperties(t *testing.T) {
11+
t.SkipNow()
12+
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
13+
// Use crm_schemas:TestCreate() to generate this...
14+
res, err := cli.CRM.Properties.List("cars")
15+
if err != nil {
16+
t.Error(err)
17+
}
18+
19+
if len(res.Results) < 1 {
20+
t.Error("expected len(res.Results) to be > 1")
21+
}
22+
23+
}
24+
25+
func TestGetCrmProperty(t *testing.T) {
26+
t.SkipNow()
27+
28+
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
29+
// Use crm_schemas:TestCreate() to generate this...
30+
res, err := cli.CRM.Properties.Get("cars", "model")
31+
if err != nil {
32+
t.Error(err)
33+
}
34+
35+
if *res.Name != "model" {
36+
t.Errorf("expected res.Name to be model, got %s", res.Name)
37+
}
38+
39+
}
40+
41+
func TestCreateProperty(t *testing.T) {
42+
t.SkipNow()
43+
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
44+
newProp := &CrmProperty{
45+
Name: NewString("mileage"),
46+
Label: NewString("Mileage Label"),
47+
Type: NewString("number"),
48+
FieldType: NewString("number"),
49+
GroupName: NewString("cars_information"),
50+
}
51+
52+
_, err := cli.CRM.Properties.Create("cars", newProp)
53+
if err != nil {
54+
t.Error(err)
55+
return
56+
}
57+
}
58+
59+
func TestUpdateProperty(t *testing.T) {
60+
t.SkipNow()
61+
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
62+
63+
updateProp := make(map[string]interface{})
64+
updateProp["label"] = fmt.Sprintf("Updated Label %d", time.Now().UnixMicro())
65+
66+
res, err := cli.CRM.Properties.Update("cars", "mileage", &updateProp)
67+
if err != nil {
68+
t.Error(err)
69+
return
70+
}
71+
72+
if res.Label != updateProp["label"] {
73+
t.Errorf("expected res.Label to be %s, got %s", updateProp["label"], res.Label)
74+
}
75+
}
76+
77+
func TestDeleteProperty(t *testing.T) {
78+
t.SkipNow()
79+
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))
80+
err := cli.CRM.Properties.Delete("cars", "mileage")
81+
if err != nil {
82+
t.Error(err)
83+
}
84+
}

crm_schemas.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package hubspot
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
const (
8+
crmSchemasPath = "schemas"
9+
)
10+
11+
type CrmSchemaAssociation struct {
12+
Cardinality *HsStr `json:"cardinality"`
13+
CreatedAt *HsTime `json:"createdAt"`
14+
FromObjectTypeId *HsStr `json:"fromObjectTypeId"`
15+
ID *HsStr `json:"id"`
16+
InverseCardinality *HsStr `json:"inverseCardinality"`
17+
Name *HsStr `json:"name"`
18+
ToObjectTypeId *HsStr `json:"toObjectTypeId"`
19+
UpdatedAt *HsTime `json:"updatedAt"`
20+
}
21+
22+
type CrmSchemaLabels struct {
23+
Plural *HsStr `json:"plural"`
24+
Singular *HsStr `json:"singular"`
25+
}
26+
27+
type CrmSchemasList struct {
28+
Results []*CrmSchema
29+
}
30+
31+
type CrmSchema struct {
32+
Archived *HsBool `json:"archived,omitempty"`
33+
Associations []*CrmSchemaAssociation `json:"associations,omitempty"`
34+
FullyQualifiedName *HsStr `json:"fullyQualifiedName,omitempty"`
35+
ID *HsStr `json:"id,omitempty"`
36+
Labels *CrmSchemaLabels `json:"labels,omitempty"`
37+
MetaType *HsStr `json:"metaType,omitempty"`
38+
Name *HsStr `json:"name,omitempty"`
39+
ObjectTypeId *HsStr `json:"objectTypeId,omitempty"`
40+
PrimaryDisplayProperty *HsStr `json:"primaryDisplayProperty,omitempty"`
41+
Properties []*CrmProperty `json:"properties,omitempty"`
42+
RequiredProperties []*HsStr `json:"requiredProperties,omitempty"`
43+
Restorable *HsBool `json:"restorable,omitempty"`
44+
SearchableProperties []*HsStr `json:"searchableProperties,omitempty"`
45+
SecondaryDisplayProperties []*HsStr `json:"secondaryDisplayProperties,omitempty"`
46+
CreatedAt *HsTime `json:"createdAt,omitempty"`
47+
UpdatedAt *HsTime `json:"updatedAt,omitempty"`
48+
}
49+
50+
// CrmSchemasService is an interface of CRM schemas endpoints of the HubSpot API.
51+
// Reference: https://developers.hubspot.com/docs/api/crm/crm-custom-objects
52+
type CrmSchemasService interface {
53+
List() (*CrmSchemasList, error)
54+
Create(reqData interface{}) (*CrmSchema, error)
55+
Get(schemaIdentifier string) (*CrmSchema, error)
56+
Delete(schemaIdentifier string, option *RequestQueryOption) error
57+
Update(schemaIdentifier string, reqData interface{}) (*CrmSchema, error)
58+
}
59+
60+
// CrmSchemasServiceOp handles communication with the CRM schemas endpoint.
61+
type CrmSchemasServiceOp struct {
62+
client *Client
63+
crmSchemasPath string
64+
}
65+
66+
var _ CrmSchemasService = (*CrmSchemasServiceOp)(nil)
67+
68+
func (s *CrmSchemasServiceOp) List() (*CrmSchemasList, error) {
69+
var resource CrmSchemasList
70+
if err := s.client.Get(s.crmSchemasPath, &resource, nil); err != nil {
71+
return nil, err
72+
}
73+
return &resource, nil
74+
}
75+
76+
func (s *CrmSchemasServiceOp) Create(reqData interface{}) (*CrmSchema, error) {
77+
var resource CrmSchema
78+
if err := s.client.Post(s.crmSchemasPath, reqData, &resource); err != nil {
79+
return nil, err
80+
}
81+
return &resource, nil
82+
}
83+
84+
func (s *CrmSchemasServiceOp) Get(schemaIdentifier string) (*CrmSchema, error) {
85+
var resource CrmSchema
86+
path := fmt.Sprintf("%s/%s", s.crmSchemasPath, schemaIdentifier)
87+
if err := s.client.Get(path, &resource, nil); err != nil {
88+
return nil, err
89+
}
90+
return &resource, nil
91+
}
92+
93+
func (s *CrmSchemasServiceOp) Delete(schemaIdentifier string, option *RequestQueryOption) error {
94+
path := fmt.Sprintf("%s/%s", s.crmSchemasPath, schemaIdentifier)
95+
return s.client.Delete(path, option)
96+
}
97+
98+
func (s *CrmSchemasServiceOp) Update(schemaIdentifier string, reqData interface{}) (*CrmSchema, error) {
99+
var resource CrmSchema
100+
path := fmt.Sprintf("%s/%s", s.crmSchemasPath, schemaIdentifier)
101+
if err := s.client.Patch(path, reqData, &resource); err != nil {
102+
return nil, err
103+
}
104+
return &resource, nil
105+
}

0 commit comments

Comments
 (0)