Skip to content

refactor(services/yandex_disk): Move raw request to core #6090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 27, 2025
11 changes: 1 addition & 10 deletions core/src/services/yandex_disk/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use std::fmt::Formatter;
use std::sync::Arc;

use bytes::Buf;
use http::header;
use http::Request;
use http::Response;
use http::StatusCode;
use log::debug;
Expand Down Expand Up @@ -229,14 +227,7 @@ impl Access for YandexDiskBackend {
}

async fn read(&self, path: &str, args: OpRead) -> Result<(RpRead, Self::Reader)> {
// TODO: move this out of reader.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay to remove this TODO? Is this addressed now? I see the get_download_url and download(download_url) pattern in few backend.rs and it think it is the recommended approach, right? Or should we move both get_download_url and download(download_url) into a new read function inside core.rs so the backend is abstracted from this two-step flow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see that b2 service encapsulates both download_url and download in core

let resp = self.get_upload_url().await?;

Should we do the same here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per #6089 (comment), I will refactor this also

let download_url = self.core.get_download_url(path).await?;

let req = Request::get(download_url)
.header(header::RANGE, args.range().to_header())
.body(Buffer::new())
.map_err(new_request_build_error)?;
let resp = self.core.info.http_client().fetch(req).await?;
let resp = self.core.download(path, args.range()).await?;

let status = resp.status();
match status {
Expand Down
23 changes: 21 additions & 2 deletions core/src/services/yandex_disk/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl YandexDiskCore {

impl YandexDiskCore {
/// Get upload url.
pub async fn get_upload_url(&self, path: &str) -> Result<String> {
async fn get_upload_url(&self, path: &str) -> Result<String> {
let path = build_rooted_abs_path(&self.root, path);

let url = format!(
Expand Down Expand Up @@ -96,7 +96,16 @@ impl YandexDiskCore {
}
}

pub async fn get_download_url(&self, path: &str) -> Result<String> {
pub async fn upload(&self, path: &str, body: Buffer) -> Result<Response<Buffer>> {
let upload_url = self.get_upload_url(path).await?;
let req = Request::put(upload_url)
.body(body)
.map_err(new_request_build_error)?;

self.send(req).await
}

async fn get_download_url(&self, path: &str) -> Result<String> {
let path = build_rooted_abs_path(&self.root, path);

let url = format!(
Expand Down Expand Up @@ -128,6 +137,16 @@ impl YandexDiskCore {
}
}

pub async fn download(&self, path: &str, range: BytesRange) -> Result<Response<HttpBody>> {
let download_url = self.get_download_url(path).await?;
let req = Request::get(download_url)
.header(header::RANGE, range.to_header())
.body(Buffer::new())
.map_err(new_request_build_error)?;

self.info.http_client().fetch(req).await
}

pub async fn ensure_dir_exists(&self, path: &str) -> Result<()> {
let path = build_abs_path(&self.root, path);

Expand Down
10 changes: 1 addition & 9 deletions core/src/services/yandex_disk/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

use std::sync::Arc;

use http::Request;
use http::StatusCode;

use super::core::YandexDiskCore;
Expand All @@ -42,16 +41,9 @@ impl oio::OneShotWrite for YandexDiskWriter {
async fn write_once(&self, bs: Buffer) -> Result<Metadata> {
self.core.ensure_dir_exists(&self.path).await?;

let upload_url = self.core.get_upload_url(&self.path).await?;

let req = Request::put(upload_url)
.body(bs)
.map_err(new_request_build_error)?;

let resp = self.core.send(req).await?;
let resp = self.core.upload(&self.path, bs).await?;

let status = resp.status();

match status {
StatusCode::CREATED => Ok(Metadata::default()),
_ => Err(parse_error(resp)),
Expand Down
Loading