Skip to content

Commit

Permalink
add: support timeout per request
Browse files Browse the repository at this point in the history
  • Loading branch information
chyroc committed Jan 3, 2024
1 parent cfe1bbd commit 0fe88e9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions api_auth_app_access_token_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (r *AuthService) GetAppAccessToken(ctx context.Context) (*TokenExpire, *Res
AppSecret: r.cli.appSecret,
AppTicket: appTicket,
},
MethodOption: newMethodOption(nil),
}
resp := new(getTenantAccessTokenResp)

Expand Down
11 changes: 6 additions & 5 deletions api_auth_tenant_access_token_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ func (r *AuthService) GetTenantAccessToken(ctx context.Context) (*TokenExpire, *
}
}
req := &RawRequestReq{
Scope: "Auth",
API: "GetTenantAccessToken",
Method: "POST",
URL: uri,
Body: body,
Scope: "Auth",
API: "GetTenantAccessToken",
Method: "POST",
URL: uri,
Body: body,
MethodOption: newMethodOption(nil),
}
resp := new(getTenantAccessTokenResp)

Expand Down
14 changes: 13 additions & 1 deletion impl_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"reflect"
"strconv"
"strings"
"time"

"github.com/chyroc/lark/internal"
)
Expand Down Expand Up @@ -118,6 +119,10 @@ func (r *Lark) parseRawHttpRequest(ctx context.Context, req *RawRequestReq) (*ra
Method: strings.ToUpper(req.Method),
Headers: map[string]string{},
URL: req.URL,
Timeout: r.timeout,
}
if req.MethodOption.timeout > 0 {
rawHttpReq.Timeout = req.MethodOption.timeout
}

// 1 headers
Expand Down Expand Up @@ -145,7 +150,13 @@ func (r *Lark) doRequest(ctx context.Context, rawHttpReq *rawHttpRequest, realRe
rawHttpReq.Method, rawHttpReq.URL, jsonHeader(rawHttpReq.Headers), string(rawHttpReq.RawBody))
}

req, err := http.NewRequest(rawHttpReq.Method, rawHttpReq.URL, rawHttpReq.Body)
if rawHttpReq.Timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, rawHttpReq.Timeout)
defer cancel()
}

req, err := http.NewRequestWithContext(ctx, rawHttpReq.Method, rawHttpReq.URL, rawHttpReq.Body)
if err != nil {
return response, err
}
Expand Down Expand Up @@ -337,6 +348,7 @@ type rawHttpRequest struct {
Body io.Reader
RawBody []byte
Headers map[string]string
Timeout time.Duration
}

func newFileUploadRequest(params map[string]string, filekey string, reader io.Reader) (string, io.Reader, error) {
Expand Down
8 changes: 8 additions & 0 deletions lark.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,17 @@ func WithUserAccessToken(token string) MethodOptionFunc {
}
}

// WithRequestTimeout set request timeout
func WithRequestTimeout(timeout time.Duration) MethodOptionFunc {
return func(option *MethodOption) {
option.timeout = timeout
}
}

// MethodOption method option
type MethodOption struct {
userAccessToken string
timeout time.Duration
}

func newMethodOption(options []MethodOptionFunc) *MethodOption {
Expand Down

0 comments on commit 0fe88e9

Please sign in to comment.