use super::backend::*;
use super::core::parse_error;
use http::Response;
use http::StatusCode;
use opendal_core::raw::*;
use opendal_core::*;
pub struct GithubReader {
backend: GithubBackend,
ctx: OperationContext,
path: String,
}
impl GithubReader {
pub(super) fn new(
backend: GithubBackend,
ctx: OperationContext,
path: &str,
_: OpRead,
) -> Self {
Self {
backend,
ctx,
path: path.to_string(),
}
}
}
impl oio::StreamRead for GithubReader {
async fn open(&self, range: BytesRange) -> Result<(RpRead, Box<dyn oio::ReadStreamDyn>)> {
let backend = &self.backend;
let path = self.path.as_str();
let resp = backend.core.get(&self.ctx, path, range).await?;
let status = resp.status();
let (rp, stream) = match status {
StatusCode::OK | StatusCode::PARTIAL_CONTENT => (
RpRead::new(parse_into_metadata(path, resp.headers())?),
resp.into_body(),
),
_ => {
let (part, mut body) = resp.into_parts();
let buf = body.to_buffer().await?;
return Err(parse_error(Response::from_parts(part, buf)));
}
};
Ok((rp, Box::new(stream) as Box<dyn oio::ReadStreamDyn>))
}
}