Skip to content

Commit

Permalink
Merge pull request #11 from belong-inc/feature/deprecate-apikey-and-a…
Browse files Browse the repository at this point in the history
…dd-private-app-description

Add deprecated comments to SetAPIKey and replace usages
  • Loading branch information
kk-no authored Oct 25, 2022
2 parents 1091f06 + 692f0d4 commit f64c04f
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 33 deletions.
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ $ go get github.com/belong-inc/go-hubspot

### API key

**Deprecated**

You should take api key in advance. Follow steps
in [here](https://knowledge.hubspot.com/integrations/how-do-i-get-my-hubspot-api-key).

```go
// Initialize hubspot client with apikey
// Initialize hubspot client with apikey.
client, _ := hubspot.NewClient(hubspot.SetAPIKey("YOUR_API_KEY"))
```

Expand All @@ -43,13 +45,23 @@ client, _ := hubspot.NewClient(hubspot.SetOAuth(&hubspot.OAuthConfig{
}))
```

### Private app

You should take access token in advance. Follow steps
in [here](https://developers.hubspot.com/docs/api/private-apps).

```go
// Initialize hubspot client with private app access token.
client, _ := hubspot.NewClient(hubspot.SetPrivateAppToken("YOUR_ACCESS_TOKEN"))
```

## API call

### Get contact

```go
// Initialize hubspot client with auth method.
client, _ := hubspot.NewClient(hubspot.SetAPIKey("YOUR_API_KEY"))
client, _ := hubspot.NewClient(hubspot.SetPrivateAppToken("YOUR_ACCESS_TOKEN"))

// Get a Contact object whose id is `yourContactID`.
// Contact instance needs to be provided to bind response value.
Expand All @@ -71,7 +83,7 @@ fmt.Println(contact.FirstName, contact.LastName)

```go
// Initialize hubspot client with auth method.
client, _ := hubspot.NewClient(hubspot.SetAPIKey("YOUR_API_KEY"))
client, _ := hubspot.NewClient(hubspot.SetPrivateAppToken("YOUR_ACCESS_TOKEN"))

// Create request payload.
req := &hubspot.Contact{
Expand Down Expand Up @@ -102,7 +114,7 @@ fmt.Println(contact.FirstName, contact.LastName)

```go
// Initialize hubspot client with auth method.
client, _ := hubspot.NewClient(hubspot.SetAPIKey("YOUR_API_KEY"))
client, _ := hubspot.NewClient(hubspot.SetPrivateAppToken("YOUR_ACCESS_TOKEN"))

// Call associate api.
client.CRM.Contact.AssociateAnotherObj("yourContactID", &hubspot.AssociationConfig{
Expand All @@ -127,7 +139,7 @@ type CustomDeal struct {
}

// Initialize hubspot client with auth method.
client, _ := hubspot.NewClient(hubspot.SetAPIKey("YOUR_API_KEY"))
client, _ := hubspot.NewClient(hubspot.SetPrivateAppToken("YOUR_ACCESS_TOKEN"))

// Get a Deal object whose id is `yourDealID`.
// CustomDeal instance needs to be provided as to bind response value contained custom fields.
Expand Down Expand Up @@ -160,7 +172,7 @@ type CustomDeal struct {
}

// Initialize hubspot client with auth method.
client, _ := hubspot.NewClient(hubspot.SetAPIKey("YOUR_API_KEY"))
client, _ := hubspot.NewClient(hubspot.SetPrivateAppToken("YOUR_ACCESS_TOKEN"))

req := &CustomDeal{
Deal: hubspot.Deal{
Expand Down Expand Up @@ -205,9 +217,9 @@ fmt.Println(customDeal.CustomA, customDeal.CustomB)

|Type | Availability |
|-------------|--------------|
|API key | Available |
|OAuth | Available |
|Private apps | Not Implemented |
|API key | Deprecated |
|OAuth | Available |
|Private apps | Available |

# Contributing

Expand Down
1 change: 1 addition & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func SetOAuth(config *OAuthConfig) AuthMethod {
}
}

// Deprecated: Use hubspot.SetPrivateAppToken.
func SetAPIKey(key string) AuthMethod {
return func(c *Client) {
c.authenticator = &APIKey{
Expand Down
58 changes: 47 additions & 11 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type ExampleContact struct {
}

func ExampleContactServiceOp_Create() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

example := &ExampleContact{
email: "[email protected]",
Expand Down Expand Up @@ -54,7 +54,7 @@ func ExampleContactServiceOp_Create() {
}

func ExampleContactServiceOp_Update() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

example := &ExampleContact{
email: "[email protected]",
Expand Down Expand Up @@ -92,7 +92,7 @@ func ExampleContactServiceOp_Update() {
}

func ExampleContactServiceOp_Get() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

res, err := cli.CRM.Contact.Get("contact001", &hubspot.Contact{}, nil)
if err != nil {
Expand All @@ -113,7 +113,7 @@ func ExampleContactServiceOp_Get() {
}

func ExampleContactServiceOp_AssociateAnotherObj() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

res, err := cli.CRM.Contact.AssociateAnotherObj("contact001", &hubspot.AssociationConfig{
ToObject: hubspot.ObjectTypeDeal,
Expand Down Expand Up @@ -221,14 +221,50 @@ func ExampleDealServiceOp_Create_oauth() {
// // Output:
}

func ExampleDealServiceOp_Create_privateapp() {
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

example := &ExampleDeal{
amount: "1500.00",
name: "Custom data integrations",
stage: "presentation scheduled",
ownerID: "910901",
}

deal := &hubspot.Deal{
Amount: hubspot.NewString(example.amount),
DealName: hubspot.NewString(example.name),
DealStage: hubspot.NewString(example.stage),
DealOwnerID: hubspot.NewString(example.ownerID),
PipeLine: hubspot.NewString("default"),
}

res, err := cli.CRM.Deal.Create(deal)
if err != nil {
log.Fatal(err)
}

r, ok := res.Properties.(*hubspot.Deal)
if !ok {
log.Fatal("unable to type assertion")
}

// use properties
_ = r

fmt.Println(res)

// // Output:
}

type CustomDeal struct {
hubspot.Deal
CustomA string `json:"custom_a,omitempty"`
CustomB string `json:"custom_b,omitempty"`
}

func ExampleDealServiceOp_Create_custom() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

example := &ExampleDeal{
amount: "1500.00",
Expand Down Expand Up @@ -267,7 +303,7 @@ func ExampleDealServiceOp_Create_custom() {
}

func ExampleDealServiceOp_Update() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

example := &ExampleDeal{
amount: "1500.00",
Expand Down Expand Up @@ -303,7 +339,7 @@ func ExampleDealServiceOp_Update() {
}

func ExampleDealServiceOp_Get() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

res, err := cli.CRM.Deal.Get("deal001", &hubspot.Deal{}, nil)
if err != nil {
Expand All @@ -324,7 +360,7 @@ func ExampleDealServiceOp_Get() {
}

func ExampleDealServiceOp_Get_custom() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

res, err := cli.CRM.Deal.Get("deal001", &CustomDeal{}, &hubspot.RequestQueryOption{
CustomProperties: []string{
Expand All @@ -350,7 +386,7 @@ func ExampleDealServiceOp_Get_custom() {
}

func ExampleDealServiceOp_AssociateAnotherObj() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

res, err := cli.CRM.Deal.AssociateAnotherObj("deal001", &hubspot.AssociationConfig{
ToObject: hubspot.ObjectTypeContact,
Expand All @@ -375,7 +411,7 @@ func ExampleDealServiceOp_AssociateAnotherObj() {
}

func ExampleMarketingEmailOp_GetStatistics() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

emailID := 0 // Set proper value.
res, err := cli.Marketing.Email.GetStatistics(emailID, &hubspot.Statistics{})
Expand All @@ -397,7 +433,7 @@ func ExampleMarketingEmailOp_GetStatistics() {
}

func ExampleMarketingEmailOp_ListStatistics() {
cli, _ := hubspot.NewClient(hubspot.SetAPIKey(os.Getenv("API_KEY")))
cli, _ := hubspot.NewClient(hubspot.SetPrivateAppToken(os.Getenv("PRIVATE_APP_TOKEN")))

statistics := make([]hubspot.Statistics, 0, 50)
res, err := cli.Marketing.Email.ListStatistics(&hubspot.BulkStatisticsResponse{Objects: statistics}, &hubspot.BulkRequestQueryOption{Limit: 10})
Expand Down
2 changes: 1 addition & 1 deletion gohubspot.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type ResponseResource struct {

// NewClient returns a new HubSpot API client with APIKey or OAuthConfig.
// HubSpot officially recommends authentication with OAuth.
// e.g. hubspot.NewClient(hubspot.SetAPIKey("key"))
// e.g. hubspot.NewClient(hubspot.SetPrivateAppToken("key"))
func NewClient(setAuthMethod AuthMethod, opts ...Option) (*Client, error) {
if setAuthMethod == nil {
return nil, errors.New("the authentication method is not set")
Expand Down
58 changes: 50 additions & 8 deletions gohubspot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,45 +77,58 @@ func TestNewClient(t *testing.T) {
},
wantErr: nil,
},
{
name: "Success new client and set private app token",
args: args{
setAuthMethod: hubspot.SetPrivateAppToken("token"),
},
settings: settings{
client: http.DefaultClient,
baseURL: hubspot.ExportBaseURL,
apiVersion: hubspot.ExportAPIVersion,
authMethod: hubspot.SetPrivateAppToken("token"),
},
wantErr: nil,
},
{
name: "Success new client with custom http client",
args: args{
setAuthMethod: hubspot.SetAPIKey("key"),
setAuthMethod: hubspot.SetPrivateAppToken("token"),
opts: []hubspot.Option{hubspot.WithHTTPClient(&http.Client{Timeout: 100 * time.Second})},
},
settings: settings{
client: &http.Client{Timeout: 100 * time.Second},
baseURL: hubspot.ExportBaseURL,
apiVersion: hubspot.ExportAPIVersion,
authMethod: hubspot.SetAPIKey("key"),
authMethod: hubspot.SetPrivateAppToken("token"),
},
wantErr: nil,
},
{
name: "Success new client with custom base url",
args: args{
setAuthMethod: hubspot.SetAPIKey("key"),
setAuthMethod: hubspot.SetPrivateAppToken("token"),
opts: []hubspot.Option{hubspot.WithBaseURL(&url.URL{Scheme: "http", Host: "example.com"})},
},
settings: settings{
client: http.DefaultClient,
baseURL: &url.URL{Scheme: "http", Host: "example.com"},
apiVersion: hubspot.ExportAPIVersion,
authMethod: hubspot.SetAPIKey("key"),
authMethod: hubspot.SetPrivateAppToken("token"),
},
wantErr: nil,
},
{
name: "Success new client with custom api version",
args: args{
setAuthMethod: hubspot.SetAPIKey("key"),
setAuthMethod: hubspot.SetPrivateAppToken("token"),
opts: []hubspot.Option{hubspot.WithAPIVersion("v0")},
},
settings: settings{
client: http.DefaultClient,
baseURL: hubspot.ExportBaseURL,
apiVersion: "v0",
authMethod: hubspot.SetAPIKey("key"),
authMethod: hubspot.SetPrivateAppToken("token"),
},
wantErr: nil,
},
Expand Down Expand Up @@ -279,6 +292,35 @@ func TestClient_NewRequest(t *testing.T) {
},
wantErr: nil,
},
{
name: "Created http.POST request with PrivateApp token",
settings: settings{
client: hubspot.NewMockHTTPClient(&hubspot.MockConfig{}),
baseURL: hubspot.ExportBaseURL,
apiVersion: hubspot.ExportAPIVersion,
authMethod: hubspot.SetPrivateAppToken("token"),
crm: nil,
},
args: args{
method: http.MethodPost,
path: "objects/test",
body: &body{
ID: "001",
Name: "example",
},
option: nil,
},
want: wantReq{
method: http.MethodPost,
url: "https://api.hubapi.com/objects/test",
body: []byte(`{"id":"001","name":"example"}`),
header: http.Header{
"Content-Type": []string{"application/json"},
"Authorization": []string{"Bearer token"},
},
},
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -355,7 +397,7 @@ func TestClient_CreateAndDo(t *testing.T) {
}),
baseURL: hubspot.ExportBaseURL,
apiVersion: hubspot.ExportAPIVersion,
authMethod: hubspot.SetAPIKey("test"),
authMethod: hubspot.SetPrivateAppToken("test"),
crm: nil,
},
args: args{
Expand Down Expand Up @@ -393,7 +435,7 @@ func TestClient_CreateAndDo(t *testing.T) {
}),
baseURL: hubspot.ExportBaseURL,
apiVersion: hubspot.ExportAPIVersion,
authMethod: hubspot.SetAPIKey("test"),
authMethod: hubspot.SetPrivateAppToken("test"),
crm: nil,
},
args: args{
Expand Down
2 changes: 1 addition & 1 deletion mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewMockClient(conf *MockConfig) *Client {
apiVersion: defaultAPIVersion,
}
cli.CRM = newCRM(cli)
SetAPIKey("apikey")(cli)
SetPrivateAppToken("token")(cli)

return cli
}
Expand Down
Loading

0 comments on commit f64c04f

Please sign in to comment.