// Create application configuration to prevent leakage. It is recommended to put application information in environment variables.
// Environment variable or parameter name:
// APP_ID: App ID in the application certificate
// APP_SECRET: App Secret in the application certificate
// VERIFICATION_TOKEN: Verification Token in the event subscription
// ENCRYPT_KEY: Encrypt Key in the event subscription, yes Empty, indicating that the event content is not encrypted
// The configuration of the enterprise self-built application
appSettings := config.NewInternalAppSettings("[APP_ID]", "[APP_SECRET]", "[VERIFICATION_TOKEN]", "[ENCRYPT_KEY]")
// Enterprise The configuration of self-built applications, the application configuration is obtained through environment variables
appSettings := config.GetInternalAppSettingsByEnv()
// App Store application configuration
appSettings := config.NewISVAppSettings("[APP_ID]", "[APP_SECRET]", "[VERIFICATION_TOKEN]", "[ENCRYPT_KEY]")
// App store application configuration, obtain application configuration through environment variables
appSettings := config.GetISVAppSettingsByEnv()
// Create Config
// domain: domain name http address: constants.DomainFeiShu/constants.DomainLarkSuite
// appSettings: application configuration
// logger: [log interface](core/log/log.go)
// loggerLevel: output log level log.LevelDebug/LevelInfo/LevelWarn/LevelError
// store: [Storage interface](core/store/store.go), used to store app_ticket/app_access_token/tenant_access_token
// used in online config
conf := config.NewConfig(domain, appSettings, logger, loggerLevel, store)
// Config for development and testing
// logger: use the default implementation(core/log/log.go defaultLogger)
// loggerLevel: Debug level
// store: use the default implementation(core/store/store.go DefaultStore)
conf := config.NewTestConfig(domain, appSettings)
// Create CoreContext(*core.Context) for API requests, Event callbacks, Card callbacks, etc., as function parameters
// core.Context implements the context.Context interface
coreCtx := core.WarpContext(context.Background())
// Get the RequestID(string) of API requests, Event callbacks, and Card callbacks, used for problem feedback, open platform query related logs, you can quickly locate the problem
requestID := coreCtx.GetRequestID()
// Get response to API request Status code(int)
statusCode := coreCtx.GetHTTPStatusCode()
Module api
Processing flow
The acquisition and life cycle maintenance of app_access_token and tenant_access_token life cycle, developers can directly access the business interface
import(
"github.com/larksuite/oapi-sdk-go/core/config"
"github.com/larksuite/oapi-sdk-go/core/constants"
"github.com/larksuite/oapi-sdk-go/core/log"
"github.com/larksuite/oapi-sdk-go/api/core/request"
"github.com/larksuite/oapi-sdk-go/api/core/response"
)
// Create request
// httpPath:(path after `open-apis/`) API path, for example: https://{domain}/open-apis/authen/v1/user_info, the httpPath: "authen/v1/user_info"
// httpMethod: GET/POST/PUT/BATCH/DELETE
// accessTokenType: which token access is used by the API, value range: request.AccessTokenTypeApp/request.AccessTokenTypeTenant/request.AccessTokenTypeUser, for example: request.AccessTokenTypeTenant
// input : Request body(may be request.NewFormData()(for example: file upload)), if the request body is not needed(for example, some GET requests), then pass: nil
// output: response body(output := response["data" ])
// optFns: extended function, some uncommon parameter packages, as follows:
// request.SetPathParams(map[string]interface{}{"user_id": 4}): set the URL Path parameter(with: prefix) value, When httpPath="users/:user_id", the requested URL="https://{domain}/open-apis/users/4"
// request.SetQueryParams(map[string]interface{}{"age":4,"types":[1,2]}): Set the URL qeury, will append to the url?age=4&types=1&types=2
// request.setResponseStream(), set whether the response is a stream, such as downloading a file, at this time: output value is Buffer type
// request.SetNotDataField(), set whether the response does not have a `data` field, business interfaces all have `data `Field, so you don’t need to set
// request.SetTenantKey("TenantKey"), as an `app store application`, it means using `tenant_access_token` to access the API, you need to set
// request.SetUserAccessToken("UserAccessToken"), which means using` user_access_token` To access the API, you need to set
req := request.NewRequest2(httpPath:string, httpMethod:string, accessTokenType:AccessTokenType ,input:interface, output:interface ,... optFns : OptFn [)))
coreCtx := core.WarpContext(context.Background())
err := api.Send(coreCtx, conf, req)
fmt.Println(coreCtx.GetRequestID())
fmt.Println(coreCtx.GetHTTPStatusCode())
if err != nil {
fmt.Println(tools.Prettify(err))
e := err.(*response.Error)
fmt.Println(e.Code)
fmt.Println(e.Msg)
return
}
fmt.Println(tools.Prettify(ret))