// Code generated by lark_sdk_gen. DO NOT EDIT.
/**
 * Copyright 2022 chyroc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package lark

import (
	"context"
	"io"
)

// DownloadDriveFile 下载云空间中的文件, 如 PDF 文件。不包含飞书文档、电子表格以及多维表格等在线文档。该接口支持通过在请求头添加 `Range` 参数分片下载部分文件。
//
// 前提条件:
// 调用此接口之前, 你需确保应用已拥有文件的下载权限。否则接口将返回 403 的 HTTP 状态码。参考[云空间常见问题](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/faq)第五点了解如何分享文件的下载权限给应用。更多云文档接口权限问题, 参考[云文档常见问题](https://open.feishu.cn/document/ukTMukTMukTM/uczNzUjL3czM14yN3MTN)。
// 注意事项:
// 本接口仅支持下载云空间中的资源文件。要下载云文档中的素材（如图片、附件等）, 需调用[下载素材](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/download)接口。
// 使用限制:
// 该接口调用频率上限为 5 QPS, 10000 次/天。否则会返回 1061045 错误码, 可通过稍后重试解决。
//
// doc: https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/file/download
// new doc: https://open.feishu.cn/document/server-docs/docs/drive-v1/download/download
func (r *DriveService) DownloadDriveFile(ctx context.Context, request *DownloadDriveFileReq, options ...MethodOptionFunc) (*DownloadDriveFileResp, *Response, error) {
	if r.cli.mock.mockDriveDownloadDriveFile != nil {
		r.cli.Log(ctx, LogLevelDebug, "[lark] Drive#DownloadDriveFile mock enable")
		return r.cli.mock.mockDriveDownloadDriveFile(ctx, request, options...)
	}

	req := &RawRequestReq{
		Scope:                 "Drive",
		API:                   "DownloadDriveFile",
		Method:                "GET",
		URL:                   r.cli.openBaseURL + "/open-apis/drive/v1/files/:file_token/download",
		Body:                  request,
		MethodOption:          newMethodOption(options),
		NeedTenantAccessToken: true,
		NeedUserAccessToken:   true,
	}
	resp := new(downloadDriveFileResp)

	response, err := r.cli.RawRequest(ctx, req, resp)
	return resp.Data, response, err
}

// MockDriveDownloadDriveFile mock DriveDownloadDriveFile method
func (r *Mock) MockDriveDownloadDriveFile(f func(ctx context.Context, request *DownloadDriveFileReq, options ...MethodOptionFunc) (*DownloadDriveFileResp, *Response, error)) {
	r.mockDriveDownloadDriveFile = f
}

// UnMockDriveDownloadDriveFile un-mock DriveDownloadDriveFile method
func (r *Mock) UnMockDriveDownloadDriveFile() {
	r.mockDriveDownloadDriveFile = nil
}

// DownloadDriveFileReq ...
type DownloadDriveFileReq struct {
	FileToken string `path:"file_token" json:"-"` // 文件的 token, 获取方式见[文件概述](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/file/file-overview), 示例值: "boxcnabCdefgabcef"
}

// downloadDriveFileResp ...
type downloadDriveFileResp struct {
	Code int64                  `json:"code,omitempty"`
	Msg  string                 `json:"msg,omitempty"`
	Data *DownloadDriveFileResp `json:"data,omitempty"`
}

func (r *downloadDriveFileResp) SetReader(file io.Reader) {
	if r.Data == nil {
		r.Data = &DownloadDriveFileResp{}
	}
	r.Data.File = file
}

func (r *downloadDriveFileResp) SetFilename(filename string) {
	if r.Data == nil {
		r.Data = &DownloadDriveFileResp{}
	}
	r.Data.Filename = filename
}

// DownloadDriveFileResp ...
type DownloadDriveFileResp struct {
	File     io.Reader `json:"file,omitempty"`
	Filename string    `json:"filename,omitempty"`
}
