-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathactivities.go
76 lines (68 loc) · 2.35 KB
/
activities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package openproject
import (
"context"
"fmt"
"strings"
"time"
)
// ActivitiesService handles activities for the OpenProject instance / API.
type ActivitiesService struct {
client *Client
}
type Activity struct {
Type string `json:"_type"`
Id int `json:"id"`
Comment OPGenericDescription `json:"comment"`
Details []OPGenericDescription `json:"details"`
Version int `json:"version"`
CreatedAt time.Time `json:"createdAt"`
Links struct {
Self OPGenericLink `json:"self"`
WorkPackage OPGenericLink `json:"workPackage"`
User OPGenericLink `json:"user"`
Update OPGenericLink `json:"update"`
} `json:"_links"`
}
type Activities struct {
Type string `json:"_type"`
Total int `json:"total"`
Count int `json:"count"`
Embedded struct {
Elements []Activity `json:"elements"`
} `json:"_embedded"`
Links struct {
Self OPGenericLink `json:"self"`
} `json:"_links"`
}
// GetWithContext gets activity from OpenProject using its ID
func (s *ActivitiesService) GetWithContext(ctx context.Context, activitiesID string) (*Activity, *Response, error) {
apiEndPoint := fmt.Sprintf("api/v3/activities/%s", activitiesID)
obj, resp, err := GetWithContext(ctx, s, apiEndPoint)
if err != nil {
return nil, resp, err
}
return obj.(*Activity), resp, err
}
// Get wraps GetWithContext using the background context.
func (s *ActivitiesService) Get(activitiesID string) (*Activity, *Response, error) {
return s.GetWithContext(context.Background(), activitiesID)
}
// GetFromWPHrefWithContext gets activities from OpenProject using work package href string, like '/api/v3/work_packages/36353/activities'
func (s *ActivitiesService) GetFromWPHrefWithContext(ctx context.Context, href string) (*Activities, *Response, error) {
if strings.HasPrefix(href, "/") && len(href) > 1 {
href = href[1:]
}
if href == "" {
return nil, nil, fmt.Errorf("href is empty")
}
apiEndPoint := href
obj, resp, err := GetListWithContext(ctx, s, apiEndPoint, nil, 0, 0)
if err != nil {
return nil, resp, err
}
return obj.(*Activities), resp, err
}
// GetFromWPHref wraps GetFromWPHrefWithContext using the background context.
func (s *ActivitiesService) GetFromWPHref(href string) (*Activities, *Response, error) {
return s.GetFromWPHrefWithContext(context.Background(), href)
}