循环遍历 API 响应以获取多个页面。

huangapple go评论95阅读模式
英文:

Cycling through API responses to get multiple pages

问题

这是我的代码:

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
}

这段代码的作用是:

它向 https://groups.roblox.com/v1/groups/2/users?sortOrder=Asc&limit=100&cursor= 发送一个请求。

然后它返回响应中的 nextpagecursor

然后我尝试使用名为 cycle 的最后一个函数循环遍历页面。

但是我不知道如何使它工作,以便给我每个页面的 nextpagecursor,这样我就可以从每个页面获取数据。

英文:

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.

答案1

得分: 2

这是一个典型的JSON API工作流程:

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)
}

示例:

package main
import "fmt"

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

结果:

{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}}]}
英文:

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}}]}

huangapple
  • 本文由 发表于 2021年7月14日 21:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/68378727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定