opendal/services/pcloud/
lister.rs1use std::sync::Arc;
19
20use bytes::Buf;
21use http::StatusCode;
22
23use super::core::*;
24use super::error::parse_error;
25use crate::raw::oio::Entry;
26use crate::raw::*;
27use crate::*;
28
29pub struct PcloudLister {
30 core: Arc<PcloudCore>,
31
32 path: String,
33}
34
35impl PcloudLister {
36 pub(super) fn new(core: Arc<PcloudCore>, path: &str) -> Self {
37 PcloudLister {
38 core,
39 path: path.to_string(),
40 }
41 }
42}
43
44impl oio::PageList for PcloudLister {
45 async fn next_page(&self, ctx: &mut oio::PageContext) -> Result<()> {
46 let resp = self.core.list_folder(&self.path).await?;
47
48 let status = resp.status();
49
50 match status {
51 StatusCode::OK => {
52 let bs = resp.into_body();
53
54 let resp: ListFolderResponse = serde_json::from_reader(bs.clone().reader())
55 .map_err(new_json_deserialize_error)?;
56 let result = resp.result;
57
58 if result == 2005 {
59 ctx.done = true;
60 return Ok(());
61 }
62
63 if result != 0 {
64 return Err(Error::new(ErrorKind::Unexpected, format!("{resp:?}")));
65 }
66
67 if let Some(metadata) = resp.metadata {
68 if let Some(contents) = metadata.contents {
69 for content in contents {
70 let path = if content.isfolder {
71 format!("{}/", content.path.clone())
72 } else {
73 content.path.clone()
74 };
75
76 let md = parse_list_metadata(content)?;
77 let path = build_rel_path(&self.core.root, &path);
78
79 ctx.entries.push_back(Entry::new(&path, md))
80 }
81 }
82
83 ctx.done = true;
84 return Ok(());
85 }
86
87 Err(Error::new(
88 ErrorKind::Unexpected,
89 String::from_utf8_lossy(&bs.to_bytes()),
90 ))
91 }
92 _ => Err(parse_error(resp)),
93 }
94 }
95}