Skip to content

Commit

Permalink
feat(openapi): timeout with context
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrix-X committed Sep 25, 2024
1 parent 9e46e3e commit de8536c
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions internal/provider/brainx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"github.com/ArtisanCloud/PowerLibs/v3/cache"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/contextx"
"io"
"net/http"
"time"
Expand Down Expand Up @@ -167,18 +166,14 @@ func (sp *BrainXProviderClient) HTTPGet(ctx context.Context, uri string, params
}

func (sp *BrainXProviderClient) HTTPPost(ctx context.Context, uri string, jsonData interface{}, useAuth bool, headers map[string]string) ([]byte, error) {
newCtx := contextx.ValueOnlyFrom(ctx)
timeoutCtx, cancel := context.WithTimeout(newCtx, 60*time.Second)
defer cancel()

// 将 jsonData 转换为 JSON 格式的字节流
body, err := json.Marshal(jsonData)
if err != nil {
return nil, err
}

url := sp.BaseURL + uri
req, err := http.NewRequestWithContext(timeoutCtx, "POST", url, bytes.NewBuffer(body))
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
Expand All @@ -189,5 +184,5 @@ func (sp *BrainXProviderClient) HTTPPost(ctx context.Context, uri string, jsonDa
req.Header.Set(key, value)
}

return sp.doRequest(timeoutCtx, req, useAuth)
return sp.doRequest(ctx, req, useAuth)
}

0 comments on commit de8536c

Please sign in to comment.