Skip to content

Commit 5751a46

Browse files
author
Genert Org
committed
feat: use context for everything
1 parent d73dd65 commit 5751a46

38 files changed

+594
-430
lines changed

pipedrive/activities.go

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package pipedrive
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
)
78

9+
// ActivitiesService handles activities related
10+
// methods of the Pipedrive API.
11+
//
12+
// Pipedrive API dcos: https://developers.pipedrive.com/docs/api/v1/#!/Activities
813
type ActivitiesService service
914

15+
// Participants represents a Pipedrive participant.
1016
type Participants struct {
1117
PersonID int `json:"person_id"`
1218
PrimaryFlag bool `json:"primary_flag"`
1319
}
1420

21+
// Activity represents a Pipedrive activity.
1522
type Activity struct {
1623
ID int `json:"id"`
1724
CompanyID int `json:"company_id"`
@@ -46,29 +53,36 @@ type Activity struct {
4653
AssignedToUserID int `json:"assigned_to_user_id"`
4754
}
4855

49-
type SingleActivity struct {
56+
func (a Activity) String() string {
57+
return Stringify(a)
58+
}
59+
60+
// ActivityResponse represents single activity response.
61+
type ActivityResponse struct {
5062
Success bool `json:"success"`
5163
Data Activity `json:"data"`
5264
}
5365

54-
type Activities struct {
66+
// ActivitiesReponse represents multiple activities response.
67+
type ActivitiesReponse struct {
5568
Success bool `json:"success"`
5669
Data []Activity `json:"data"`
5770
AdditionalData AdditionalData `json:"additional_data,omitempty"`
5871
}
5972

60-
// Returns all activities assigned to a particular user
73+
// List returns all activities assigned to a particular user
74+
//
6175
// https://developers.pipedrive.com/docs/api/v1/#!/Activities/get_activities
62-
func (s *ActivitiesService) List(id int) (*Activities, *Response, error) {
76+
func (s *ActivitiesService) List(ctx context.Context) (*ActivitiesReponse, *Response, error) {
6377
req, err := s.client.NewRequest(http.MethodGet, "/activities", nil, nil)
6478

6579
if err != nil {
6680
return nil, nil, err
6781
}
6882

69-
var record *Activities
83+
var record *ActivitiesReponse
7084

71-
resp, err := s.client.Do(req, &record)
85+
resp, err := s.client.Do(ctx, req, &record)
7286

7387
if err != nil {
7488
return nil, resp, err
@@ -77,19 +91,20 @@ func (s *ActivitiesService) List(id int) (*Activities, *Response, error) {
7791
return record, resp, nil
7892
}
7993

80-
// Returns details of a specific activity.
94+
// GetByID returns details of a specific activity.
95+
//
8196
// https://developers.pipedrive.com/docs/api/v1/#!/Activities/get_activities
82-
func (s *ActivitiesService) GetById(id int) (*Activities, *Response, error) {
97+
func (s *ActivitiesService) GetByID(ctx context.Context, id int) (*ActivitiesReponse, *Response, error) {
8398
uri := fmt.Sprintf("/activities/%v", id)
8499
req, err := s.client.NewRequest(http.MethodGet, uri, nil, nil)
85100

86101
if err != nil {
87102
return nil, nil, err
88103
}
89104

90-
var record *Activities
105+
var record *ActivitiesReponse
91106

92-
resp, err := s.client.Do(req, &record)
107+
resp, err := s.client.Do(ctx, req, &record)
93108

94109
if err != nil {
95110
return nil, resp, err
@@ -98,32 +113,19 @@ func (s *ActivitiesService) GetById(id int) (*Activities, *Response, error) {
98113
return record, resp, nil
99114
}
100115

101-
type ActivitiesCreateOptions struct {
102-
Subject string `url:"subject"`
103-
Done uint8 `url:"done"`
104-
Type string `url:"type"`
105-
DueDate string `url:"due_date"`
106-
DueTime string `url:"due_time"`
107-
Duration string `url:"duration"`
108-
UserId uint `url:"user_id"`
109-
DealId uint `url:"user_id"`
110-
PersonId uint `url:"person_id"`
111-
Participants interface{} `url:"participants"`
112-
OrgId uint `url:"org_id"`
113-
}
114-
115116
// Create an activity.
117+
//
116118
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Activities/post_activities
117-
func (s *ActivitiesService) Create(opt *ActivitiesCreateOptions) (*SingleActivity, *Response, error) {
119+
func (s *ActivitiesService) Create(ctx context.Context, opt *ActivitiesCreateOptions) (*ActivityResponse, *Response, error) {
118120
req, err := s.client.NewRequest(http.MethodPost, "/activities", nil, opt)
119121

120122
if err != nil {
121123
return nil, nil, err
122124
}
123125

124-
var record *SingleActivity
126+
var record *ActivityResponse
125127

126-
resp, err := s.client.Do(req, &record)
128+
resp, err := s.client.Do(ctx, req, &record)
127129

128130
if err != nil {
129131
return nil, resp, err
@@ -132,19 +134,36 @@ func (s *ActivitiesService) Create(opt *ActivitiesCreateOptions) (*SingleActivit
132134
return record, resp, nil
133135
}
134136

135-
// Edit an activity
137+
// ActivitiesCreateOptions specifices the optional parameters to the
138+
// ActivitiesService.Update method.
139+
type ActivitiesCreateOptions struct {
140+
Subject string `url:"subject,omitempty"`
141+
Done uint8 `url:"done,omitempty"`
142+
Type string `url:"type,omitempty"`
143+
DueDate string `url:"due_date,omitempty"`
144+
DueTime string `url:"due_time,omitempty"`
145+
Duration string `url:"duration,omitempty"`
146+
UserID uint `url:"user_id,omitempty"`
147+
DealID uint `url:"user_id,omitempty"`
148+
PersonID uint `url:"person_id,omitempty"`
149+
Participants interface{} `url:"participants,omitempty"`
150+
OrgID uint `url:"org_id,omitempty"`
151+
}
152+
153+
// Update an activity
154+
//
136155
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Activities/put_activities_id
137-
func (s *ActivitiesService) Update(id int, opt *ActivitiesCreateOptions) (*SingleActivity, *Response, error) {
156+
func (s *ActivitiesService) Update(ctx context.Context, id int, opt *ActivitiesCreateOptions) (*ActivityResponse, *Response, error) {
138157
uri := fmt.Sprintf("/activities/%v", id)
139158
req, err := s.client.NewRequest(http.MethodPut, uri, opt, nil)
140159

141160
if err != nil {
142161
return nil, nil, err
143162
}
144163

145-
var record *SingleActivity
164+
var record *ActivityResponse
146165

147-
resp, err := s.client.Do(req, &record)
166+
resp, err := s.client.Do(ctx, req, &record)
148167

149168
if err != nil {
150169
return nil, resp, err
@@ -153,9 +172,10 @@ func (s *ActivitiesService) Update(id int, opt *ActivitiesCreateOptions) (*Singl
153172
return record, resp, nil
154173
}
155174

156-
// Delete multiple activities in bulk.
175+
// DeleteMultiple activities in bulk.
176+
//
157177
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Activities/delete_activities
158-
func (s *ActivitiesService) DeleteMultiple(ids []int) (*Response, error) {
178+
func (s *ActivitiesService) DeleteMultiple(ctx context.Context, ids []int) (*Response, error) {
159179
req, err := s.client.NewRequest(http.MethodDelete, "/activities", &DeleteMultipleOptions{
160180
Ids: arrayToString(ids, ","),
161181
}, nil)
@@ -164,18 +184,18 @@ func (s *ActivitiesService) DeleteMultiple(ids []int) (*Response, error) {
164184
return nil, err
165185
}
166186

167-
return s.client.Do(req, nil)
187+
return s.client.Do(ctx, req, nil)
168188
}
169189

170-
// Deletes an activity.
190+
// Delete an activity.
171191
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/Activities/delete_activities_id
172-
func (s *ActivitiesService) Delete(id int) (*Response, error) {
192+
func (s *ActivitiesService) Delete(ctx context.Context, id int) (*Response, error) {
173193
uri := fmt.Sprintf("/activities/%v", id)
174194
req, err := s.client.NewRequest(http.MethodDelete, uri, nil, nil)
175195

176196
if err != nil {
177197
return nil, err
178198
}
179199

180-
return s.client.Do(req, nil)
200+
return s.client.Do(ctx, req, nil)
181201
}

pipedrive/activity_fields.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package pipedrive
22

3-
import "net/http"
3+
import (
4+
"context"
5+
"net/http"
6+
)
47

8+
// ActivityFieldsService handles activity fields related
9+
// methods of the Pipedrive API.
10+
//
11+
// Pipedrive API dcos: https://developers.pipedrive.com/docs/api/v1/#!/ActivityFields
512
type ActivityFieldsService service
613

14+
// ActivityField represents a Pipedrive activity field.
715
type ActivityField struct {
816
ID int `json:"id"`
917
Key string `json:"key"`
@@ -27,24 +35,26 @@ type ActivityField struct {
2735
} `json:"options,omitempty"`
2836
}
2937

30-
type ActivityFields struct {
38+
// ActivityFieldsResponse represents multiple activity fields response.
39+
type ActivityFieldsResponse struct {
3140
Success bool `json:"success"`
3241
Data []ActivityField `json:"data"`
3342
AdditionalData AdditionalData `json:"additional_data"`
3443
}
3544

36-
// Return list of all fields for activity.
45+
// List returns all fields for activity.
46+
//
3747
// Pipedrive API docs: https://developers.pipedrive.com/docs/api/v1/#!/ActivityFields/get_activityFields
38-
func (s *ActivityFieldsService) List() (*ActivityFields, *Response, error) {
48+
func (s *ActivityFieldsService) List(ctx context.Context) (*ActivityFieldsResponse, *Response, error) {
3949
req, err := s.client.NewRequest(http.MethodGet, "/activityFields", nil, nil)
4050

4151
if err != nil {
4252
return nil, nil, err
4353
}
4454

45-
var record *ActivityFields
55+
var record *ActivityFieldsResponse
4656

47-
resp, err := s.client.Do(req, &record)
57+
resp, err := s.client.Do(ctx, req, &record)
4858

4959
if err != nil {
5060
return nil, resp, err

0 commit comments

Comments
 (0)