-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for properties and schemas endpoints
- Loading branch information
1 parent
bf87cc8
commit 02ed782
Showing
8 changed files
with
512 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.