Skip to content

Instantly share code, notes, and snippets.

@jmw511
Created January 6, 2015 19:06
Show Gist options
  • Save jmw511/4827aac455e183ead09c to your computer and use it in GitHub Desktop.
Save jmw511/4827aac455e183ead09c to your computer and use it in GitHub Desktop.
GetContentInBatches function
func (api GraphV2) GetContentInBatches(path string, since, until int64, fields ...string) ([]BatchResponse, error) {
url := fmt.Sprintf("%v/%v?fields=id&since=%v&until=%v&limit=%v&access_token=%v", HostV2, path, since, until, BatchLimit, api.AccessToken)
ids := []string{}
for {
resp, err := GetFacebookGraphRequest(url)
if err != nil {
return nil, err
}
if resp.ErrorCode != 0 {
return nil, fmt.Errorf("graph error: message(%v) code(%v)", resp.ErrorMessage, resp.ErrorCode)
}
if len(resp.Data) == 0 {
break
}
for _, data := range resp.Data {
ids = append(ids, data.ID)
}
if resp.Paging.Next == "" {
break
}
url = resp.Paging.Next
}
batchedIDs := api.arrayInBatches(ids, MaxBatchRequests)
batchURLs := []string{}
for _, batch := range batchedIDs {
requests := []string{}
for _, id := range batch {
request := fmt.Sprintf("{'method':'GET','relative_url':'%v?fields=%v'}", id, strings.Join(fields, ","))
requests = append(requests, request)
}
batchURL := fmt.Sprintf("%v?batch=[%v]&access_token=%v", HostV2, strings.Join(requests, ","), api.AccessToken)
batchURLs = append(batchURLs, batchURL)
}
return api.PostGraphRequests(batchURLs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment