headers = c(
`Authorization` = access_key,
`wix-site-id` = our_site_id
)
fetch_members <- function() {
# set constants
# endpoint <- "https://www.wixapis.com/members/v1/members"
offset <- 0
limit <- 1000
is_paging <- TRUE
output_list <- c()
params <- list(`paging.limit` = limit, fieldSet = 'FULL', sort = c('createdDate'))
# page through the api, fetching data
while(is_paging) {
print(paste0('offset: ', offset))
# set params, make fetch
params$`paging.offset` <- offset
res <- httr::GET(url = endpoint, httr::add_headers(.headers=headers), query = params)
res
# grab members, metadata from fetch
content <- httr::content(res)
metadata <- content$metadata
this_list <- content$members
length(this_list)
# add to list, increment offset, and assess paging
output_list <- c(output_list, this_list)
offset <- offset + limit
is_paging <- metadata$count == limit
}
# flatten the data
members_df <- output_list %>%
purrr::map(unlist) %>%
purrr::map(t) %>%
purrr::map(as_tibble, .name_repair = ~make.names(., unique = TRUE)) %>%
dplyr::bind_rows() %>%
readr::type_convert() %>%
janitor::clean_names(case = 'snake')
# and return
return(members_df)
}
字符串
使用wix API /members/v1/members端点处理分页和排序的正确方法是什么?
从Wix API获取网站成员列表时遇到了一个令人沮丧的问题。我们在上面的例子中使用了R,但在Python中遇到了同样的问题。我们调用上面的这个函数,它每次都成功返回相同数量的成员9744
。然而,每次都有不同的成员集合,从来没有正确的9744个唯一成员,而是,我们总是得到一些重复和一些遗漏。显然,排序或分页(或两者)都搞砸了。我们得到如下输出:
members <- fetch_members()
which(members$login_email == '[email protected]')
[1] 8165
members <- fetch_members()
which(members$login_email == '[email protected]')
[1] 8165 9698
members <- fetch_members()
which(members$login_email == '[email protected]')
integer(0)
型
https://dev.wix.com/docs/rest/api-reference/members/members/list-members是我们正在使用的文档,https://dev.wix.com/docs/rest/articles/getting-started/sorting-and-paging#sort-list-endpoints是排序和分页文档。
坦率地说,我认为分页是好的,排序是问题。在元数据中,它正确地显示了偏移量,限制。我们如何在这里正确排序?
**编辑:**还有一个https://www.wixapis.com/members/v1/members/query端点,但是使用这个端点我们也不能让分页或排序正常工作。即使有较低的分页限制(100而不是1000),如果分页和排序可以工作,我们也很乐意使用这个端点。
1条答案
按热度按时间l7wslrjt1#
根据documentation判断,我猜您的问题出在
params
上。字符串