-
Notifications
You must be signed in to change notification settings - Fork 18
/
api.go
146 lines (126 loc) · 5.38 KB
/
api.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package lokalise
import (
"errors"
"time"
)
type Api struct {
httpClient *restClient
pageOptions PageOptions
Branches func() *BranchService
Comments func() *CommentService
Contributors func() *ContributorService
Files func() *FileService
Keys func() *KeyService
Languages func() *LanguageService
Orders func() *OrderService
PaymentCards func() *PaymentCardService
Projects func() *ProjectService
QueuedProcesses func() *QueuedProcessService
Screenshots func() *ScreenshotService
Segments func() *SegmentationService
Snapshots func() *SnapshotService
Tasks func() *TaskService
Teams func() *TeamService
TeamUserGroups func() *TeamUserGroupService
TeamUsers func() *TeamUserService
PermissionTemplates func() *PermissionTemplateService
TranslationProviders func() *TranslationProviderService
Translations func() *TranslationService
TranslationStatuses func() *TranslationStatusService
Webhooks func() *WebhookService
}
type ClientOption func(*Api) error
func New(apiToken string, options ...ClientOption) (*Api, error) {
c := Api{}
c.httpClient = newClient(apiToken)
for _, o := range options {
err := o(&c)
if err != nil {
return nil, err
}
}
bs := BaseService{c.httpClient, c.pageOptions, nil}
// predefined list options if any
prjOpts := ProjectListOptions{Page: c.pageOptions.Page, Limit: c.pageOptions.Limit}
keyOpts := KeyListOptions{Pagination: c.pageOptions.Pagination, Page: c.pageOptions.Page, Limit: c.pageOptions.Limit, Cursor: c.pageOptions.Cursor}
taskOpts := TaskListOptions{Page: c.pageOptions.Page, Limit: c.pageOptions.Limit}
scOpts := ScreenshotListOptions{Page: c.pageOptions.Page, Limit: c.pageOptions.Limit}
trOpts := TranslationListOptions{Pagination: c.pageOptions.Pagination, Page: c.pageOptions.Page, Limit: c.pageOptions.Limit, Cursor: c.pageOptions.Cursor}
fOpts := FileListOptions{Page: c.pageOptions.Page, Limit: c.pageOptions.Limit}
c.Projects = func() *ProjectService { return &ProjectService{BaseService: bs, opts: prjOpts} }
c.Branches = func() *BranchService { return &BranchService{bs} }
c.Teams = func() *TeamService { return &TeamService{bs} }
c.PermissionTemplates = func() *PermissionTemplateService { return &PermissionTemplateService{bs} }
c.TeamUsers = func() *TeamUserService { return &TeamUserService{bs} }
c.TeamUserGroups = func() *TeamUserGroupService { return &TeamUserGroupService{bs} }
c.Contributors = func() *ContributorService { return &ContributorService{bs} }
c.Comments = func() *CommentService { return &CommentService{bs} }
c.Keys = func() *KeyService { return &KeyService{BaseService: bs, listOpts: keyOpts} }
c.Tasks = func() *TaskService { return &TaskService{BaseService: bs, listOpts: taskOpts} }
c.Screenshots = func() *ScreenshotService { return &ScreenshotService{BaseService: bs, listOpts: scOpts} }
c.Segments = func() *SegmentationService { return &SegmentationService{BaseService: bs} }
c.Snapshots = func() *SnapshotService { return &SnapshotService{bs} }
c.Languages = func() *LanguageService { return &LanguageService{bs} }
c.Translations = func() *TranslationService { return &TranslationService{BaseService: bs, opts: trOpts} }
c.TranslationProviders = func() *TranslationProviderService { return &TranslationProviderService{bs} }
c.TranslationStatuses = func() *TranslationStatusService { return &TranslationStatusService{bs} }
c.Orders = func() *OrderService { return &OrderService{bs} }
c.PaymentCards = func() *PaymentCardService { return &PaymentCardService{bs} }
c.Webhooks = func() *WebhookService { return &WebhookService{bs} }
c.Files = func() *FileService { return &FileService{BaseService: bs, opts: fOpts} }
c.QueuedProcesses = func() *QueuedProcessService { return &QueuedProcessService{bs} }
return &c, nil
}
// WithBaseURL returns a ClientOption setting the base URL of the client.
// This should only be used for testing different API versions or for using a mocked
// backend in tests.
// noinspection GoUnusedExportedFunction
func WithBaseURL(url string) ClientOption {
return func(c *Api) error {
c.httpClient.Client.SetHostURL(url)
return nil
}
}
// WithRetryCount returns a client ClientOption setting the retry count of outgoing requests.
// if count is zero retries are disabled.
func WithRetryCount(count int) ClientOption {
return func(c *Api) error {
if count < 0 {
return errors.New("lokalise: retry count must be positive")
}
c.httpClient.Client.SetRetryCount(count)
return nil
}
}
// Sets default wait time to sleep before retrying request.
// Default is 100 milliseconds.
// noinspection GoUnusedExportedFunction
func WithRetryTimeout(t time.Duration) ClientOption {
return func(c *Api) error {
c.httpClient.Client.SetRetryWaitTime(t)
return nil
}
}
func WithConnectionTimeout(t time.Duration) ClientOption {
return func(c *Api) error {
c.httpClient.Client.SetTimeout(t)
return nil
}
}
func WithDebug(dbg bool) ClientOption {
return func(c *Api) error {
c.httpClient.Client.SetDebug(dbg)
return nil
}
}
func WithPageLimit(limit uint) ClientOption {
return func(c *Api) error {
c.pageOptions.Limit = limit
return nil
}
}
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool {
return &v
}