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

// GetAccessToken 根据[登录预授权码](https://open.feishu.cn/document/common-capabilities/sso/api/obtain-oauth-code) 返回 code 获取 `user_access_token`。
//
// 为了让流程更加规范, 本接口不再返回用户信息, 只返回token相关的字段。如需用户信息, 请通过 [获取用户信息](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/user_info/get)接口获取数据
//
// doc: https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/authen-v1/oidc-access_token/create
func (r *AuthService) GetAccessToken(ctx context.Context, request *GetAccessTokenReq, options ...MethodOptionFunc) (*GetAccessTokenResp, *Response, error) {
	if r.cli.mock.mockAuthGetAccessToken != nil {
		r.cli.Log(ctx, LogLevelDebug, "[lark] Auth#GetAccessToken mock enable")
		return r.cli.mock.mockAuthGetAccessToken(ctx, request, options...)
	}

	req := &RawRequestReq{
		Scope:              "Auth",
		API:                "GetAccessToken",
		Method:             "POST",
		URL:                r.cli.openBaseURL + "/open-apis/authen/v1/oidc/access_token",
		Body:               request,
		MethodOption:       newMethodOption(options),
		NeedAppAccessToken: true,
	}
	resp := new(getAccessTokenResp)

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

// MockAuthGetAccessToken mock AuthGetAccessToken method
func (r *Mock) MockAuthGetAccessToken(f func(ctx context.Context, request *GetAccessTokenReq, options ...MethodOptionFunc) (*GetAccessTokenResp, *Response, error)) {
	r.mockAuthGetAccessToken = f
}

// UnMockAuthGetAccessToken un-mock AuthGetAccessToken method
func (r *Mock) UnMockAuthGetAccessToken() {
	r.mockAuthGetAccessToken = nil
}

// GetAccessTokenReq ...
type GetAccessTokenReq struct {
	GrantType string `json:"grant_type,omitempty"` // 授权类型, 固定值, 示例值: "authorization_code"
	Code      string `json:"code,omitempty"`       // 登录预授权码, 调用[登录预授权码](https://open.feishu.cn/document/common-capabilities/sso/api/obtain-oauth-code) 获取code, 示例值: "xMSldislSkdK"
}

// GetAccessTokenResp ...
type GetAccessTokenResp struct {
	AccessToken      string `json:"access_token,omitempty"`       // 字段`access_token`即user_access_token, 用于获取用户资源和访问某些open api
	RefreshToken     string `json:"refresh_token,omitempty"`      // 刷新user_access_token时使用的 refresh_token
	TokenType        string `json:"token_type,omitempty"`         // token 类型, 固定值
	ExpiresIn        int64  `json:"expires_in,omitempty"`         // user_access_token有效期, 单位: 秒, 有效时间两个小时左右, 需要以返回结果为准
	RefreshExpiresIn int64  `json:"refresh_expires_in,omitempty"` // refresh_token有效期, 单位: 秒, 一般是30天左右, 需要以返回结果为准
	Scope            string `json:"scope,omitempty"`              // 用户授予app的权限全集
}

// getAccessTokenResp ...
type getAccessTokenResp struct {
	Code  int64               `json:"code,omitempty"` // 错误码, 非 0 表示失败
	Msg   string              `json:"msg,omitempty"`  // 错误描述
	Data  *GetAccessTokenResp `json:"data,omitempty"`
	Error *ErrorDetail        `json:"error,omitempty"`
}
