gitee.com/larksuite/oapi-sdk-go/v3@v3.0.3/service/ext/api.go (about)

     1  /*
     2   * MIT License
     3   *
     4   * Copyright (c) 2022 Lark Technologies Pte. Ltd.
     5   *
     6   * 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:
     7   *
     8   * The above copyright notice and this permission notice, shall be included in all copies or substantial portions of the Software.
     9   *
    10   * 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.
    11   */
    12  
    13  package larkext
    14  
    15  import (
    16  	"context"
    17  	"net/http"
    18  
    19  	larkcore "gitee.com/larksuite/oapi-sdk-go/v3/core"
    20  )
    21  
    22  func NewService(config *larkcore.Config) *ExtService {
    23  	s := &ExtService{config: config}
    24  	s.DriveExplorer = &driveExplorer{service: s}
    25  	s.Authen = &authen{service: s}
    26  	return s
    27  }
    28  
    29  // 业务域服务定义
    30  type ExtService struct {
    31  	config        *larkcore.Config
    32  	DriveExplorer *driveExplorer
    33  	Authen        *authen
    34  }
    35  
    36  // 资源服务定义
    37  type driveExplorer struct {
    38  	service *ExtService
    39  }
    40  
    41  // 资源服务定义
    42  type authen struct {
    43  	service *ExtService
    44  }
    45  
    46  func (d *authen) AuthenAccessToken(ctx context.Context, req *AuthenAccessTokenReq, options ...larkcore.RequestOptionFunc) (*AuthenAccessTokenResp, error) {
    47  	// 发起请求
    48  	apiReq := req.apiReq
    49  	apiReq.ApiPath = "/open-apis/authen/v1/access_token"
    50  	apiReq.HttpMethod = http.MethodPost
    51  	apiReq.SupportedAccessTokenTypes = []larkcore.AccessTokenType{larkcore.AccessTokenTypeApp}
    52  	apiResp, err := larkcore.Request(ctx, apiReq, d.service.config, options...)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	// 反序列响应结果
    57  	resp := &AuthenAccessTokenResp{ApiResp: apiResp}
    58  	err = apiResp.JSONUnmarshalBody(resp)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  	return resp, err
    63  }
    64  
    65  func (d *authen) RefreshAuthenAccessToken(ctx context.Context, req *RefreshAuthenAccessTokenReq, options ...larkcore.RequestOptionFunc) (*RefreshAuthenAccessTokenResp, error) {
    66  	// 发起请求
    67  	apiReq := req.apiReq
    68  	apiReq.ApiPath = "/open-apis/authen/v1/refresh_access_token"
    69  	apiReq.HttpMethod = http.MethodPost
    70  	apiReq.SupportedAccessTokenTypes = []larkcore.AccessTokenType{larkcore.AccessTokenTypeApp}
    71  	apiResp, err := larkcore.Request(ctx, apiReq, d.service.config, options...)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	// 反序列响应结果
    76  	resp := &RefreshAuthenAccessTokenResp{ApiResp: apiResp}
    77  	err = apiResp.JSONUnmarshalBody(resp)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return resp, err
    82  }
    83  
    84  func (d *authen) AuthenUserInfo(ctx context.Context, options ...larkcore.RequestOptionFunc) (*AuthenUserInfoResp, error) {
    85  	// 发起请求
    86  	apiReq := &larkcore.ApiReq{}
    87  	apiReq.ApiPath = "/open-apis/authen/v1/user_info"
    88  	apiReq.HttpMethod = http.MethodGet
    89  	apiReq.SupportedAccessTokenTypes = []larkcore.AccessTokenType{larkcore.AccessTokenTypeUser}
    90  	apiResp, err := larkcore.Request(ctx, apiReq, d.service.config, options...)
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  	// 反序列响应结果
    95  	resp := &AuthenUserInfoResp{ApiResp: apiResp}
    96  	err = apiResp.JSONUnmarshalBody(resp)
    97  	if err != nil {
    98  		return nil, err
    99  	}
   100  	return resp, err
   101  }
   102  
   103  func (d *driveExplorer) CreateFile(ctx context.Context, req *CreateFileReq, options ...larkcore.RequestOptionFunc) (*CreateFileResp, error) {
   104  	// 发起请求
   105  	apiReq := req.apiReq
   106  	apiReq.ApiPath = "/open-apis/drive/explorer/v2/file/:folderToken"
   107  	apiReq.HttpMethod = http.MethodPost
   108  	apiReq.SupportedAccessTokenTypes = []larkcore.AccessTokenType{larkcore.AccessTokenTypeTenant, larkcore.AccessTokenTypeUser}
   109  	apiResp, err := larkcore.Request(ctx, apiReq, d.service.config, options...)
   110  	if err != nil {
   111  		return nil, err
   112  	}
   113  	// 反序列响应结果
   114  	resp := &CreateFileResp{ApiResp: apiResp}
   115  	err = apiResp.JSONUnmarshalBody(resp)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  	return resp, err
   120  }