Scypno
Scypno

Reputation: 39

Cycling through API responses to get multiple pages

this is my code:

func nextpage(id int, currentpage string) string {
    response, err := http.Get(fmt.Sprintf("https://groups.roblox.com/v1/groups/%d/users?sortOrder=Asc&limit=100&cursor=%s", id, currentpage))
    if err != nil {
        panic(err)
    }
    defer response.Body.Close()
    var getcursor *gus
    error := json.NewDecoder(response.Body).Decode(&getcursor)
    if error != nil {
        fmt.Println(error)
    }
    return getcursor.Nextpagecursor
}

func cycle(id int) string {
    secondpage := nextpage(id, "")
    thirdpage := nextpage(id, secondpage)
    fourthpage := nextpage(id, thirdpage)

    return secondpage
}

what this does:

it send a request to https://groups.roblox.com/v1/groups/2/users?sortOrder=Asc&limit=100&cursor=

then it returns the nextpagecursor of the response that gives.

then i try to cycle through the pages with the last function named cycle

but i don't know how to make it work to give me every nextpagecursor so i can get data from every page.

Upvotes: 2

Views: 591

Answers (1)

Zombo
Zombo

Reputation: 1

It's a typical JSON API workflow:

package main

import (
   "encoding/json"
   "net/http"
)

type users struct {
   NextPageCursor string
   Data []struct {
      User struct { Username string }
   }
}

func (u *users) get() error {
   req, err := http.NewRequest("GET", "https://groups.roblox.com/v1/groups/2/users", nil)
   if err != nil {
      return err
   }
   if u.NextPageCursor != "" {
      q := req.URL.Query()
      q.Set("cursor", u.NextPageCursor)
      req.URL.RawQuery = q.Encode()
   }
   res, err := new(http.Client).Do(req)
   if err != nil {
      return err
   }
   defer res.Body.Close()
   return json.NewDecoder(res.Body).Decode(u)
}

Example:

package main
import "fmt"

func main() {
   var u users
   for range [2]struct{}{} {
      u.get()
      fmt.Printf("%+v\n", u)
   }
}

Result:

{NextPageCursor:155884_1_1c6bfd900d25d25d1949dcfead5765e5 Data:[{User:{Username:jkid243}} {User:{Username:CpMod}} {User:{Username:Zeluka}} {User:{Username:tfts}} {User:{Username:saveaseal13}} {User:{Username:Beast440}} {User:{Username:leolr9}} {User:{Username:eggnog22}} {User:{Username:mathgeek007}} {User:{Username:WhatsForDinner}}]}
{NextPageCursor:367165_1_903387d5f21f53fccf4a693d918d880a Data:[{User:{Username:banjoist26}} {User:{Username:itsmariosbuddy}} {User:{Username:toabytooby2211}} {User:{Username:ShadowLuigi}} {User:{Username:calim}} {User:{Username:Sonicthehedgehog9000}} {User:{Username:benjy8}} {User:{Username:bregfhafrh}} {User:{Username:crashman15}} {User:{Username:hiedi00}}]}

Upvotes: 2

Related Questions