-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommunity.go
77 lines (61 loc) · 1.45 KB
/
community.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
package lingotek
import (
"encoding/json"
"net/url"
"strconv"
)
func (l *Lingotek) GetCommunity(communityId string) (*Community, error) {
var community Community
err := l.getEntity("community/"+communityId, nil, &community)
return &community, err
}
func (l *Lingotek) GetCommunitiesPage(offset, limit int) ([]Community, error) {
var communities []Community
v := url.Values{}
v.Set("offset", strconv.Itoa(offset))
v.Set("limit", strconv.Itoa(limit))
err := l.getEntityCollectionPage("community", &v, &communities)
return communities, err
}
func (l *Lingotek) ListCommunities(doneChan <-chan bool) (<-chan Community, <-chan error) {
resultChan := make(chan Community)
errChan := make(chan error, 1)
go func() {
defer close(resultChan)
defer close(errChan)
response := l.createDummyResponse("community", nil)
var communities []Community
var totalRead = int32(0)
for {
resp, err := l.getNextPage(response)
if err != nil {
if err != EndOfList {
errChan <- err
}
return
}
response = resp
if response.Properties.Size == 0 {
return
}
err = json.Unmarshal(response.Entities, &communities)
if err != nil {
errChan <- err
return
}
for i := 0; i < len(communities); i++ {
totalRead += 1
select {
case <-doneChan:
return
default:
resultChan <- communities[i]
}
}
if totalRead == response.Properties.Total {
return
}
}
}()
return resultChan, errChan
}