Skip to content
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

add upload approval files method #99

Open
wants to merge 2 commits into
base: v3_main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

旨在让开发者便捷的调用飞书开放API、处理订阅的消息事件、处理服务端推送的卡片行为。


## 目录


Expand Down
62 changes: 62 additions & 0 deletions sample/apiall/approvalv4/uploadFile_approval.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Package approval code generated by oapi sdk gen
/*
* MIT License
*
* Copyright (c) 2022 Lark Technologies Pte. Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package main

import (
"context"
"fmt"
"github.com/larksuite/oapi-sdk-go/v3"
"github.com/larksuite/oapi-sdk-go/v3/core"
"github.com/larksuite/oapi-sdk-go/v3/service/approval/v4"
"os"
)

// POST /approval/openapi/v2/file/upload
func main() {
// 创建 Client
client := lark.NewClient("appid", "appsecret", func(config *larkcore.Config) {
config.BaseUrl = "https://open.feishu-boe.cn"
config.LogLevel = larkcore.LogLevelDebug
config.LogReqAtDebug = true
})
pdf, err := os.Open("/Users/bytedance/Downloads/test.pdf")
if err != nil {
return
}
// 创建请求对象
req := larkapproval.NewApprovalCreateFileReqBuilder().
Body(larkapproval.NewApprovalCreateFileReqBodyBuilder().
Name("test.pdf").
Type("attachment").
Content(pdf).
Build()).
Build()
// 发起请求
resp, err := client.Approval.Approval.UploadFile(context.Background(), req)

// 处理错误
if err != nil {
fmt.Println(err)
return
}

// 服务端错误处理
if !resp.Success() {
fmt.Println(resp.Code, resp.Msg, resp.RequestId())
return
}

// 业务处理
fmt.Println(larkcore.Prettify(resp))
}
23 changes: 23 additions & 0 deletions service/approval/v4/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,29 @@ func (a *approval) Unsubscribe(ctx context.Context, req *UnsubscribeApprovalReq,
return resp, err
}

//上传审批文件资源
//
//- 官网API文档链接:https://open.feishu.cn/document/ukTMukTMukTM/uUDOyUjL1gjM14SN4ITN
func (a *approval) UploadFile(ctx context.Context, req *ApprovalCreateFileReq, options ...larkcore.RequestOptionFunc) (*ApprovalCreateFileResp, error) {
options = append(options, larkcore.WithFileUpload())
// 发起请求
apiReq := req.apiReq
apiReq.ApiPath = "/approval/openapi/v2/file/upload"
apiReq.HttpMethod = http.MethodPost
apiReq.SupportedAccessTokenTypes = []larkcore.AccessTokenType{larkcore.AccessTokenTypeTenant}
apiResp, err := larkcore.Request(ctx, apiReq, a.service.config, options...)
if err != nil {
return nil, err
}
// 反序列响应结果
resp := &ApprovalCreateFileResp{ApiResp: apiResp}
err = apiResp.JSONUnmarshalBody(resp, a.service.config)
if err != nil {
return nil, err
}
return resp, err
}

// 三方审批定义创建
//
// - 审批定义是审批的描述,包括审批名称、图标、描述等基础信息。创建好审批定义,用户就可以在审批应用的发起页中看到审批,如果用户点击发起,则会跳转到配置的发起三方系统地址去发起审批。;;另外,审批定义还配置了审批操作时的回调地址:审批人在待审批列表中进行【同意】【拒绝】操作时,审批中心会调用回调地址通知三方系统。
Expand Down
87 changes: 87 additions & 0 deletions service/approval/v4/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package larkapproval

import (
"fmt"
"io"

"context"
"errors"
Expand Down Expand Up @@ -9821,6 +9822,92 @@ func (resp *UnsubscribeApprovalResp) Success() bool {
return resp.Code == 0
}

func NewApprovalCreateFileReqBuilder() *ApprovalCreateFileReqBuilder {
builder := &ApprovalCreateFileReqBuilder{}
builder.apiReq = &larkcore.ApiReq{
PathParams: larkcore.PathParams{},
QueryParams: larkcore.QueryParams{},
}
return builder
}

type ApprovalCreateFileReqBuilder struct {
apiReq *larkcore.ApiReq
body *ApprovalCreateFileReqBody
}

// 上传文件,可以上传视频,音频和常见的文件类型。
func (builder *ApprovalCreateFileReqBuilder) Body(body *ApprovalCreateFileReqBody) *ApprovalCreateFileReqBuilder {
builder.body = body
return builder
}

func (builder *ApprovalCreateFileReqBuilder) Build() *ApprovalCreateFileReq {
req := &ApprovalCreateFileReq{}
req.apiReq = &larkcore.ApiReq{}
req.apiReq.Body = builder.body
return req
}

func NewApprovalCreateFileReqBodyBuilder() *ApprovalCreateFileReqBodyBuilder {
builder := &ApprovalCreateFileReqBodyBuilder{}
return builder
}

type ApprovalCreateFileReqBodyBuilder struct {
name string //文件名(需包含文件扩展名,如“文件.doc”
type_ string // 文件类型(image 或 attachment)
content io.Reader // 文件内容
}

func (builder *ApprovalCreateFileReqBodyBuilder) Name(name string) *ApprovalCreateFileReqBodyBuilder {
builder.name = name
return builder
}

func (builder *ApprovalCreateFileReqBodyBuilder) Type(type_ string) *ApprovalCreateFileReqBodyBuilder {
builder.type_ = type_
return builder
}

func (builder *ApprovalCreateFileReqBodyBuilder) Content(content io.Reader) *ApprovalCreateFileReqBodyBuilder {
builder.content = content
return builder
}

func (builder *ApprovalCreateFileReqBodyBuilder) Build() *ApprovalCreateFileReqBody {
req := &ApprovalCreateFileReqBody{}
req.Name = builder.name
req.Type = builder.type_
req.Content = builder.content
return req
}

type ApprovalCreateFileReqBody struct {
Name string `json:"name"` //文件名(需包含文件扩展名,如“文件.doc”
Type string `json:"type"` // 文件类型(image 或 attachment)
Content io.Reader `json:"content"` // 文件内容
}

type ApprovalCreateFileReq struct {
apiReq *larkcore.ApiReq
Body *ApprovalCreateFileReqBody `body:""`
}

type ApprovalCreateFileRespData struct {
Code string `json:"code"` // 文件的key
Url string `json:"url"` // 文件的url路由
}
type ApprovalCreateFileResp struct {
*larkcore.ApiResp `json:"-"`
larkcore.CodeError
Data *ApprovalCreateFileRespData `json:"data"` // 业务数据
}

func (t *ApprovalCreateFileResp) Success() bool {
return t.Code == 0
}

type CreateExternalApprovalReqBuilder struct {
apiReq *larkcore.ApiReq
externalApproval *ExternalApproval
Expand Down