-
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.
- Loading branch information
1 parent
e0c0f38
commit 62d8373
Showing
3 changed files
with
290 additions
and
0 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,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 | ||
} |
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,221 @@ | ||
package hubspot | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestGetSchemas(t *testing.T) { | ||
t.SkipNow() | ||
|
||
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN"))) | ||
res, err := cli.CRM.Schemas.List() | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
_, ok := res["results"] | ||
if !ok { | ||
t.Errorf("expected List schemas to respond with results key on success") | ||
} | ||
} | ||
|
||
func TestCreateSchema(t *testing.T) { | ||
t.SkipNow() | ||
// this example is from Hubspot.. | ||
var exampleJson string = ` | ||
{ | ||
"name": "cars", | ||
"labels": { | ||
"singular": "Car", | ||
"plural": "Cars" | ||
}, | ||
"primaryDisplayProperty": "model", | ||
"secondaryDisplayProperties": [ | ||
"make" | ||
], | ||
"searchableProperties": [ | ||
"year", | ||
"make", | ||
"vin", | ||
"model" | ||
], | ||
"requiredProperties": [ | ||
"year", | ||
"make", | ||
"vin", | ||
"model" | ||
], | ||
"properties": [ | ||
{ | ||
"name": "condition", | ||
"label": "Condition", | ||
"type": "enumeration", | ||
"fieldType": "select", | ||
"options": [ | ||
{ | ||
"label": "New", | ||
"value": "new" | ||
}, | ||
{ | ||
"label": "Used", | ||
"value": "used" | ||
} | ||
] | ||
}, | ||
{ | ||
"name": "date_received", | ||
"label": "Date received", | ||
"type": "date", | ||
"fieldType": "date" | ||
}, | ||
{ | ||
"name": "year", | ||
"label": "Year", | ||
"type": "number", | ||
"fieldType": "number" | ||
}, | ||
{ | ||
"name": "make", | ||
"label": "Make", | ||
"type": "string", | ||
"fieldType": "text" | ||
}, | ||
{ | ||
"name": "model", | ||
"label": "Model", | ||
"type": "string", | ||
"fieldType": "text" | ||
}, | ||
{ | ||
"name": "vin", | ||
"label": "VIN", | ||
"type": "string", | ||
"hasUniqueValue": true, | ||
"fieldType": "text" | ||
}, | ||
{ | ||
"name": "price", | ||
"label": "Price", | ||
"type": "number", | ||
"fieldType": "number" | ||
}, | ||
{ | ||
"name": "notes", | ||
"label": "Notes", | ||
"type": "string", | ||
"fieldType": "text" | ||
} | ||
], | ||
"associatedObjects": [ | ||
"CONTACT" | ||
] | ||
} | ||
` | ||
|
||
req := make(map[string]interface{}) | ||
err := json.Unmarshal([]byte(exampleJson), &req) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN"))) | ||
res, err := cli.CRM.Schemas.Create(req) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
_, ok := res["id"] | ||
if !ok { | ||
t.Errorf("expected post schema result to have an id field") | ||
} | ||
} | ||
|
||
func TestGetSchema(t *testing.T) { | ||
t.SkipNow() | ||
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN"))) | ||
res, err := cli.CRM.Schemas.Get("cars") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
fmt.Printf("%+v\n", res) | ||
|
||
_, ok := res["id"] | ||
if !ok { | ||
t.Errorf("expected post schema result to have an id field") | ||
} | ||
} | ||
|
||
func TestDeleteSchema(t *testing.T) { | ||
t.SkipNow() | ||
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN"))) | ||
|
||
res, err := cli.CRM.Schemas.Get("cars") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
qName, ok := res["fullyQualifiedName"] | ||
if !ok { | ||
t.Error(err) | ||
} | ||
err = cli.CRM.Schemas.Delete(qName.(string), false) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
err = cli.CRM.Schemas.Delete(qName.(string), true) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
} | ||
|
||
func TestUpdateSchema(t *testing.T) { | ||
t.SkipNow() | ||
// Note: You need to wait some time after calling create before calling Update as it'll return an error message. | ||
req := make(map[string]interface{}) | ||
req["primaryDisplayProperty"] = "year" | ||
|
||
cli, _ := NewClient(SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN"))) | ||
|
||
res, err := cli.CRM.Schemas.Get("cars") | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
primaryDisplayProperty, ok := res["primaryDisplayProperty"] | ||
if !ok { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
if primaryDisplayProperty != "model" { | ||
t.Error("expected primaryDisplayProperty to be model before update") | ||
return | ||
} | ||
|
||
qName, ok := res["fullyQualifiedName"] | ||
if !ok { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
res, err = cli.CRM.Schemas.Update(qName.(string), req) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
primaryDisplayProperty, ok = res["primaryDisplayProperty"] | ||
if !ok { | ||
t.Error(err) | ||
} | ||
|
||
if primaryDisplayProperty != "year" { | ||
t.Errorf("expected primaryDisplayProperty to be year after update, got %s", primaryDisplayProperty.(string)) | ||
} | ||
|
||
} |