Skip to content

Commit b4e1828

Browse files
committed
Chore: Use filter apply pattern accross the sdk
The filter will be responsible for injecting the query string parameters in the HTTP request.
1 parent d723599 commit b4e1828

26 files changed

Lines changed: 800 additions & 698 deletions

projects/activity.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,30 @@ type ActivityListRequestFilters struct {
197197
PageSize int64
198198
}
199199

200+
func (a ActivityListRequestFilters) apply(req *http.Request) {
201+
query := req.URL.Query()
202+
if !a.StartDate.IsZero() {
203+
query.Set("startDate", a.StartDate.Format(time.RFC3339))
204+
}
205+
if !a.EndDate.IsZero() {
206+
query.Set("endDate", a.EndDate.Format(time.RFC3339))
207+
}
208+
if len(a.LogItemTypes) > 0 {
209+
logItemTypes := make([]string, len(a.LogItemTypes))
210+
for i, logType := range a.LogItemTypes {
211+
logItemTypes[i] = string(logType)
212+
}
213+
query.Set("activityTypes", strings.Join(logItemTypes, ","))
214+
}
215+
if a.Page > 0 {
216+
query.Set("page", strconv.FormatInt(a.Page, 10))
217+
}
218+
if a.PageSize > 0 {
219+
query.Set("pageSize", strconv.FormatInt(a.PageSize, 10))
220+
}
221+
req.URL.RawQuery = query.Encode()
222+
}
223+
200224
// ActivityListRequest represents the request body for loading multiple activities.
201225
//
202226
// https://apidocs.teamwork.com/docs/teamwork/v3/activity/get-projects-api-v3-latestactivity-json
@@ -233,28 +257,7 @@ func (a ActivityListRequest) HTTPRequest(ctx context.Context, server string) (*h
233257
if err != nil {
234258
return nil, err
235259
}
236-
237-
query := req.URL.Query()
238-
if !a.Filters.StartDate.IsZero() {
239-
query.Set("startDate", a.Filters.StartDate.Format(time.RFC3339))
240-
}
241-
if !a.Filters.EndDate.IsZero() {
242-
query.Set("endDate", a.Filters.EndDate.Format(time.RFC3339))
243-
}
244-
if len(a.Filters.LogItemTypes) > 0 {
245-
logItemTypes := make([]string, len(a.Filters.LogItemTypes))
246-
for i, logType := range a.Filters.LogItemTypes {
247-
logItemTypes[i] = string(logType)
248-
}
249-
query.Set("activityTypes", strings.Join(logItemTypes, ","))
250-
}
251-
if a.Filters.Page > 0 {
252-
query.Set("page", strconv.FormatInt(a.Filters.Page, 10))
253-
}
254-
if a.Filters.PageSize > 0 {
255-
query.Set("pageSize", strconv.FormatInt(a.Filters.PageSize, 10))
256-
}
257-
req.URL.RawQuery = query.Encode()
260+
a.Filters.apply(req)
258261

259262
return req, nil
260263
}

projects/calendar.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ type CalendarListRequestFilters struct {
204204
PageSize int64
205205
}
206206

207+
func (c CalendarListRequestFilters) apply(req *http.Request) {
208+
query := req.URL.Query()
209+
if c.Page > 0 {
210+
query.Set("page", strconv.FormatInt(c.Page, 10))
211+
}
212+
if c.PageSize > 0 {
213+
query.Set("pageSize", strconv.FormatInt(c.PageSize, 10))
214+
}
215+
req.URL.RawQuery = query.Encode()
216+
}
217+
207218
// CalendarListRequest represents the request for loading calendars.
208219
//
209220
// https://apidocs.teamwork.com/docs/teamwork/v3/calendars/get-projects-api-v3-calendars-json
@@ -230,15 +241,7 @@ func (c CalendarListRequest) HTTPRequest(ctx context.Context, server string) (*h
230241
if err != nil {
231242
return nil, err
232243
}
233-
234-
query := req.URL.Query()
235-
if c.Filters.Page > 0 {
236-
query.Set("page", strconv.FormatInt(c.Filters.Page, 10))
237-
}
238-
if c.Filters.PageSize > 0 {
239-
query.Set("pageSize", strconv.FormatInt(c.Filters.PageSize, 10))
240-
}
241-
req.URL.RawQuery = query.Encode()
244+
c.Filters.apply(req)
242245

243246
return req, nil
244247
}

projects/calendar_event.go

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,37 @@ type CalendarEventListRequestFilters struct {
278278
Limit int64
279279
}
280280

281+
func (c CalendarEventListRequestFilters) apply(req *http.Request) {
282+
query := req.URL.Query()
283+
if !c.StartedAfterDate.IsZero() {
284+
query.Set("startedAfterDate", c.StartedAfterDate.String())
285+
}
286+
if !c.EndedBeforeDate.IsZero() {
287+
query.Set("endedBeforeDate", c.EndedBeforeDate.String())
288+
}
289+
if len(c.Include) > 0 {
290+
for _, include := range c.Include {
291+
query.Add("include", string(include))
292+
}
293+
}
294+
if c.Cursor != "" {
295+
query.Set("cursor", c.Cursor)
296+
}
297+
if c.Limit > 0 {
298+
query.Set("limit", strconv.FormatInt(c.Limit, 10))
299+
}
300+
// hardcoded filters to simplify API usage
301+
query.Set("includeMasterInstances", "false")
302+
query.Set("showDeletedInstances", "false")
303+
query.Set("includeInstances", "true")
304+
query.Set("includeOngoingEvents", "true")
305+
query.Set("includeDeletedInstances", "false")
306+
query.Set("includeModifiedInstances", "true")
307+
query.Set("includeTimelogs", "false")
308+
309+
req.URL.RawQuery = query.Encode()
310+
}
311+
281312
// CalendarEventListRequest represents the request for loading calendar events.
282313
//
283314
// https://apidocs.teamwork.com/docs/teamwork/v3/calendar-events/get-projects-api-v3-calendars-calendar-id-events-json
@@ -310,35 +341,8 @@ func (c CalendarEventListRequest) HTTPRequest(ctx context.Context, server string
310341
if err != nil {
311342
return nil, err
312343
}
344+
c.Filters.apply(req)
313345

314-
query := req.URL.Query()
315-
if !c.Filters.StartedAfterDate.IsZero() {
316-
query.Set("startedAfterDate", c.Filters.StartedAfterDate.String())
317-
}
318-
if !c.Filters.EndedBeforeDate.IsZero() {
319-
query.Set("endedBeforeDate", c.Filters.EndedBeforeDate.String())
320-
}
321-
if len(c.Filters.Include) > 0 {
322-
for _, include := range c.Filters.Include {
323-
query.Add("include", string(include))
324-
}
325-
}
326-
if c.Filters.Cursor != "" {
327-
query.Set("cursor", c.Filters.Cursor)
328-
}
329-
if c.Filters.Limit > 0 {
330-
query.Set("limit", strconv.FormatInt(c.Filters.Limit, 10))
331-
}
332-
// hardcoded filters to simplify API usage
333-
query.Set("includeMasterInstances", "false")
334-
query.Set("showDeletedInstances", "false")
335-
query.Set("includeInstances", "true")
336-
query.Set("includeOngoingEvents", "true")
337-
query.Set("includeDeletedInstances", "false")
338-
query.Set("includeModifiedInstances", "true")
339-
query.Set("includeTimelogs", "false")
340-
341-
req.URL.RawQuery = query.Encode()
342346
return req, nil
343347
}
344348

projects/comment.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,30 @@ type CommentListRequestFilters struct {
594594
PageSize int64
595595
}
596596

597+
func (t CommentListRequestFilters) apply(req *http.Request) {
598+
query := req.URL.Query()
599+
if t.SearchTerm != "" {
600+
query.Set("searchTerm", t.SearchTerm)
601+
}
602+
if len(t.UserIDs) > 0 {
603+
tagIDs := make([]string, len(t.UserIDs))
604+
for i, id := range t.UserIDs {
605+
tagIDs[i] = strconv.FormatInt(id, 10)
606+
}
607+
query.Set("userIds", strings.Join(tagIDs, ","))
608+
}
609+
if !t.UpdatedAfter.IsZero() {
610+
query.Set("updatedAfter", t.UpdatedAfter.Format(time.RFC3339))
611+
}
612+
if t.Page > 0 {
613+
query.Set("page", strconv.FormatInt(t.Page, 10))
614+
}
615+
if t.PageSize > 0 {
616+
query.Set("pageSize", strconv.FormatInt(t.PageSize, 10))
617+
}
618+
req.URL.RawQuery = query.Encode()
619+
}
620+
597621
// CommentListRequest represents the request body for loading multiple comments.
598622
//
599623
// https://apidocs.teamwork.com/docs/teamwork/v3/comments/get-projects-api-v3-comments-json
@@ -644,28 +668,7 @@ func (t CommentListRequest) HTTPRequest(ctx context.Context, server string) (*ht
644668
if err != nil {
645669
return nil, err
646670
}
647-
648-
query := req.URL.Query()
649-
if t.Filters.SearchTerm != "" {
650-
query.Set("searchTerm", t.Filters.SearchTerm)
651-
}
652-
if len(t.Filters.UserIDs) > 0 {
653-
tagIDs := make([]string, len(t.Filters.UserIDs))
654-
for i, id := range t.Filters.UserIDs {
655-
tagIDs[i] = strconv.FormatInt(id, 10)
656-
}
657-
query.Set("userIds", strings.Join(tagIDs, ","))
658-
}
659-
if !t.Filters.UpdatedAfter.IsZero() {
660-
query.Set("updatedAfter", t.Filters.UpdatedAfter.Format(time.RFC3339))
661-
}
662-
if t.Filters.Page > 0 {
663-
query.Set("page", strconv.FormatInt(t.Filters.Page, 10))
664-
}
665-
if t.Filters.PageSize > 0 {
666-
query.Set("pageSize", strconv.FormatInt(t.Filters.PageSize, 10))
667-
}
668-
req.URL.RawQuery = query.Encode()
671+
t.Filters.apply(req)
669672

670673
return req, nil
671674
}

projects/company.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,30 @@ type CompanyListRequestFilters struct {
513513
PageSize int64
514514
}
515515

516+
func (c CompanyListRequestFilters) apply(req *http.Request) {
517+
query := req.URL.Query()
518+
if c.SearchTerm != "" {
519+
query.Set("searchTerm", c.SearchTerm)
520+
}
521+
if len(c.TagIDs) > 0 {
522+
tagIDs := make([]string, len(c.TagIDs))
523+
for i, id := range c.TagIDs {
524+
tagIDs[i] = strconv.FormatInt(id, 10)
525+
}
526+
query.Set("projectTagIds", strings.Join(tagIDs, ","))
527+
}
528+
if c.MatchAllTags != nil {
529+
query.Set("matchAllProjectTags", strconv.FormatBool(*c.MatchAllTags))
530+
}
531+
if c.Page > 0 {
532+
query.Set("page", strconv.FormatInt(c.Page, 10))
533+
}
534+
if c.PageSize > 0 {
535+
query.Set("pageSize", strconv.FormatInt(c.PageSize, 10))
536+
}
537+
req.URL.RawQuery = query.Encode()
538+
}
539+
516540
// CompanyListRequest represents the request body for loading multiple
517541
// clients/companies.
518542
//
@@ -540,28 +564,7 @@ func (c CompanyListRequest) HTTPRequest(ctx context.Context, server string) (*ht
540564
if err != nil {
541565
return nil, err
542566
}
543-
544-
query := req.URL.Query()
545-
if c.Filters.SearchTerm != "" {
546-
query.Set("searchTerm", c.Filters.SearchTerm)
547-
}
548-
if len(c.Filters.TagIDs) > 0 {
549-
tagIDs := make([]string, len(c.Filters.TagIDs))
550-
for i, id := range c.Filters.TagIDs {
551-
tagIDs[i] = strconv.FormatInt(id, 10)
552-
}
553-
query.Set("projectTagIds", strings.Join(tagIDs, ","))
554-
}
555-
if c.Filters.MatchAllTags != nil {
556-
query.Set("matchAllProjectTags", strconv.FormatBool(*c.Filters.MatchAllTags))
557-
}
558-
if c.Filters.Page > 0 {
559-
query.Set("page", strconv.FormatInt(c.Filters.Page, 10))
560-
}
561-
if c.Filters.PageSize > 0 {
562-
query.Set("pageSize", strconv.FormatInt(c.Filters.PageSize, 10))
563-
}
564-
req.URL.RawQuery = query.Encode()
567+
c.Filters.apply(req)
565568

566569
return req, nil
567570
}

projects/jobrole.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,27 @@ type JobRoleListRequestFilters struct {
390390
Include []JobRoleListRequestSideload
391391
}
392392

393+
func (s JobRoleListRequestFilters) apply(req *http.Request) {
394+
query := req.URL.Query()
395+
if s.SearchTerm != "" {
396+
query.Set("searchTerm", s.SearchTerm)
397+
}
398+
if s.Page > 0 {
399+
query.Set("page", strconv.FormatInt(s.Page, 10))
400+
}
401+
if s.PageSize > 0 {
402+
query.Set("pageSize", strconv.FormatInt(s.PageSize, 10))
403+
}
404+
if len(s.Include) > 0 {
405+
var include []string
406+
for _, sideload := range s.Include {
407+
include = append(include, string(sideload))
408+
}
409+
query.Set("include", strings.Join(include, ","))
410+
}
411+
req.URL.RawQuery = query.Encode()
412+
}
413+
393414
// JobRoleListRequest represents the request body for loading multiple job
394415
// roles.
395416
//
@@ -420,25 +441,7 @@ func (s JobRoleListRequest) HTTPRequest(ctx context.Context, server string) (*ht
420441
if err != nil {
421442
return nil, err
422443
}
423-
424-
query := req.URL.Query()
425-
if s.Filters.SearchTerm != "" {
426-
query.Set("searchTerm", s.Filters.SearchTerm)
427-
}
428-
if s.Filters.Page > 0 {
429-
query.Set("page", strconv.FormatInt(s.Filters.Page, 10))
430-
}
431-
if s.Filters.PageSize > 0 {
432-
query.Set("pageSize", strconv.FormatInt(s.Filters.PageSize, 10))
433-
}
434-
if len(s.Filters.Include) > 0 {
435-
var include []string
436-
for _, sideload := range s.Filters.Include {
437-
include = append(include, string(sideload))
438-
}
439-
query.Set("include", strings.Join(include, ","))
440-
}
441-
req.URL.RawQuery = query.Encode()
444+
s.Filters.apply(req)
442445

443446
return req, nil
444447
}

0 commit comments

Comments
 (0)