// 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"
)

// DownloadFile 下载文件接口, 只能下载应用自己上传的文件。
//
// 注意事项:
// - 需要开启[机器人能力](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-enable-bot-ability)
// - 只能下载机器人自己上传的文件
// - 下载用户发送的资源, 请使用[获取消息中的资源文件](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message-resource/get)接口
// - 下载的资源大小不能超过100M
// - 如果需要Content-Disposition header, 发起请求的时候需要在header中设置Content-Type为application/json
//
// doc: https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/file/get
// new doc: https://open.feishu.cn/document/server-docs/im-v1/file/get
func (r *FileService) DownloadFile(ctx context.Context, request *DownloadFileReq, options ...MethodOptionFunc) (*DownloadFileResp, *Response, error) {
	if r.cli.mock.mockFileDownloadFile != nil {
		r.cli.Log(ctx, LogLevelDebug, "[lark] File#DownloadFile mock enable")
		return r.cli.mock.mockFileDownloadFile(ctx, request, options...)
	}

	req := &RawRequestReq{
		Scope:                 "File",
		API:                   "DownloadFile",
		Method:                "GET",
		URL:                   r.cli.openBaseURL + "/open-apis/im/v1/files/:file_key",
		Body:                  request,
		MethodOption:          newMethodOption(options),
		NeedTenantAccessToken: true,
	}
	resp := new(downloadFileResp)

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

// MockFileDownloadFile mock FileDownloadFile method
func (r *Mock) MockFileDownloadFile(f func(ctx context.Context, request *DownloadFileReq, options ...MethodOptionFunc) (*DownloadFileResp, *Response, error)) {
	r.mockFileDownloadFile = f
}

// UnMockFileDownloadFile un-mock FileDownloadFile method
func (r *Mock) UnMockFileDownloadFile() {
	r.mockFileDownloadFile = nil
}

// DownloadFileReq ...
type DownloadFileReq struct {
	FileKey string `path:"file_key" json:"-"` // 文件的key, 通过[上传文件](	/ssl:ttdoc/uAjLw4CM/ukTMukTMukTM/reference/im-v1/file/create)接口上传图片后获得, 示例值: "file_456a92d6-c6ea-4de4-ac3f-7afcf44ac78g"
}

// downloadFileResp ...
type downloadFileResp struct {
	Code  int64             `json:"code,omitempty"`
	Msg   string            `json:"msg,omitempty"`
	Data  *DownloadFileResp `json:"data,omitempty"`
	Error *ErrorDetail      `json:"error,omitempty"`
}

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

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

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