-
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 crm/v3/schemas and crm/v3/properties
- Loading branch information
1 parent
e0c0f38
commit b008e61
Showing
5 changed files
with
446 additions
and
3 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
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 | ||
} |
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,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) | ||
} | ||
} |
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,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 | ||
} |
Oops, something went wrong.