| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- package plane_v1
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "fwh-toolbox-server/config"
- "io"
- "net/http"
- "strings"
- "time"
- "go.uber.org/zap"
- )
- // 官方API手册
- // https://developers.plane.so/api-reference/introduction
- const (
- timeout time.Duration = 60 * time.Second // Plane请求超时60s
- )
- var (
- planeHost string
- planeApiKey string
- planeWorkspaceSlug string
- planeProjectId string
- planeLabelBugId string
- planeLabelFeatureId string
- )
- // PlaneConfig Plane配置结构
- type PlaneConfig struct {
- Host string
- ApiKey string
- WorkspaceSlug string
- ProjectId string
- LabelBugId string
- LabelFeatureId string
- }
- // PlaneHttpInit 初始化Plane HTTP客户端配置
- func PlaneHttpInit(cfg *config.PlaneApiConfig) {
- planeHost = strings.TrimRight(cfg.Host, "/")
- planeApiKey = cfg.ApiKey
- planeWorkspaceSlug = cfg.WorkspaceSlug
- planeProjectId = cfg.ProjectId
- planeLabelBugId = cfg.LabelBugId
- planeLabelFeatureId = cfg.LabelFeatureId
- }
- // GetLabelBugId 获取Bug标签ID
- func GetLabelBugId() string {
- return planeLabelBugId
- }
- // GetLabelFeatureId 获取Feature标签ID
- func GetLabelFeatureId() string {
- return planeLabelFeatureId
- }
- // planeResponse 封装Plane HTTP响应
- type planeResponse struct {
- StatusCode int
- Status string
- Body []byte
- }
- // planeHttpGet 发送GET请求到Plane API
- func planeHttpGet(relativeURL string) (*planeResponse, error) {
- url := planeHost + relativeURL
- zap.L().Debug("[Plane] HttpGet", zap.String("url", url))
- req, err := http.NewRequest(http.MethodGet, url, nil)
- if err != nil {
- return nil, fmt.Errorf("创建请求失败: %w", err)
- }
- req.Header.Set("X-API-Key", planeApiKey)
- req.Header.Set("Accept", "application/json")
- client := &http.Client{Timeout: timeout}
- resp, err := client.Do(req)
- if err != nil {
- return nil, fmt.Errorf("请求失败: %w", err)
- }
- defer resp.Body.Close()
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("读取响应失败: %w", err)
- }
- return &planeResponse{
- StatusCode: resp.StatusCode,
- Status: resp.Status,
- Body: body,
- }, nil
- }
- // planeHttpPost 发送POST请求到Plane API
- func planeHttpPost(relativeURL string, reqBody any) (*planeResponse, error) {
- url := planeHost + relativeURL
- zap.L().Debug("[Plane] HttpPost", zap.String("url", url))
- jsonData, err := json.Marshal(reqBody)
- if err != nil {
- return nil, fmt.Errorf("序列化请求体失败: %w", err)
- }
- req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(jsonData))
- if err != nil {
- return nil, fmt.Errorf("创建请求失败: %w", err)
- }
- req.Header.Set("X-API-Key", planeApiKey)
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Accept", "application/json")
- client := &http.Client{Timeout: timeout}
- resp, err := client.Do(req)
- if err != nil {
- return nil, fmt.Errorf("请求失败: %w", err)
- }
- defer resp.Body.Close()
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("读取响应失败: %w", err)
- }
- return &planeResponse{
- StatusCode: resp.StatusCode,
- Status: resp.Status,
- Body: body,
- }, nil
- }
- // planeHttpPatch 发送PATCH请求到Plane API
- func planeHttpPatch(relativeURL string, reqBody any) (*planeResponse, error) {
- url := planeHost + relativeURL
- zap.L().Debug("[Plane] HttpPatch", zap.String("url", url))
- jsonData, err := json.Marshal(reqBody)
- if err != nil {
- return nil, fmt.Errorf("序列化请求体失败: %w", err)
- }
- req, err := http.NewRequest(http.MethodPatch, url, bytes.NewReader(jsonData))
- if err != nil {
- return nil, fmt.Errorf("创建请求失败: %w", err)
- }
- req.Header.Set("X-API-Key", planeApiKey)
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Accept", "application/json")
- client := &http.Client{Timeout: timeout}
- resp, err := client.Do(req)
- if err != nil {
- return nil, fmt.Errorf("请求失败: %w", err)
- }
- defer resp.Body.Close()
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("读取响应失败: %w", err)
- }
- return &planeResponse{
- StatusCode: resp.StatusCode,
- Status: resp.Status,
- Body: body,
- }, nil
- }
- // planeHttpPut 发送PUT请求(用于上传文件到presigned URL,不带API Key)
- func planeHttpPut(url string, data []byte, contentType string) (*planeResponse, error) {
- zap.L().Debug("[Plane] HttpPut", zap.String("url", url))
- req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(data))
- if err != nil {
- return nil, fmt.Errorf("创建请求失败: %w", err)
- }
- req.Header.Set("Content-Type", contentType)
- client := &http.Client{Timeout: timeout}
- resp, err := client.Do(req)
- if err != nil {
- return nil, fmt.Errorf("请求失败: %w", err)
- }
- defer resp.Body.Close()
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("读取响应失败: %w", err)
- }
- return &planeResponse{
- StatusCode: resp.StatusCode,
- Status: resp.Status,
- Body: body,
- }, nil
- }
- // isSuccess 判断HTTP状态码是否表示成功(2xx)
- func isSuccess(statusCode int) bool {
- return statusCode >= 200 && statusCode < 300
- }
- // planeHttpDelete 发送DELETE请求到Plane API
- func planeHttpDelete(relativeURL string) (*planeResponse, error) {
- url := planeHost + relativeURL
- zap.L().Debug("[Plane] HttpDelete", zap.String("url", url))
- req, err := http.NewRequest(http.MethodDelete, url, nil)
- if err != nil {
- return nil, fmt.Errorf("创建请求失败: %w", err)
- }
- req.Header.Set("X-API-Key", planeApiKey)
- req.Header.Set("Accept", "application/json")
- client := &http.Client{Timeout: timeout}
- resp, err := client.Do(req)
- if err != nil {
- return nil, fmt.Errorf("请求失败: %w", err)
- }
- defer resp.Body.Close()
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, fmt.Errorf("读取响应失败: %w", err)
- }
- return &planeResponse{
- StatusCode: resp.StatusCode,
- Status: resp.Status,
- Body: body,
- }, nil
- }
|