Skip to content

Commit

Permalink
feat: support auth request headers (#3376)
Browse files Browse the repository at this point in the history
Signed-off-by: Song Gao <[email protected]>
  • Loading branch information
Yisaer authored and ngjaying committed Nov 20, 2024
1 parent d5413a3 commit 3098a05
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 36 deletions.
3 changes: 3 additions & 0 deletions docs/en_US/guide/sources/builtin/http_pull.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ default:
# # Expire time of the token in string, time unit is second, allow template
# expire: '3600'
# # Refresh token fetch method
# # Request header
# headers:
# Accept: application/json
# refresh:
# # Url to refresh the token, always use POST method
# url: https://127.0.0.1/api/refresh
Expand Down
3 changes: 3 additions & 0 deletions docs/zh_CN/guide/sources/builtin/http_pull.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ default:
# body: '{"username": "admin","password": "123456"}'
# # 令牌的过期时间,以字符串表示,时间单位为秒,允许使用模板
# expire: '3600'
# # 请求头
# headers:
# Accept: application/json
# # 如何刷新令牌
# refresh:
# # 刷新令牌的 URL,总是使用 POST 方法发送请求
Expand Down
3 changes: 3 additions & 0 deletions etc/sources/httppull.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ default:
# body: '{"username": "admin","password": "123456"}'
# # Expire time of the token in string, time unit is second, allow template
# expire: '3600'
# # Request header
# headers:
# Accept: application/json
# # Refresh token fetch method
# refresh:
# # Url to refresh the token, always use POST method
Expand Down
9 changes: 5 additions & 4 deletions internal/io/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ type ClientConf struct {
}

type AccessTokenConf struct {
Url string `json:"url"`
Body string `json:"body"`
Expire string `json:"expire"`
Url string `json:"url"`
Body string `json:"body"`
Expire string `json:"expire"`
Headers map[string]string `json:"headers"`
ExpireInSecond int
}

Expand Down Expand Up @@ -217,7 +218,7 @@ func (cc *ClientConf) InitConf(device string, props map[string]interface{}) erro

// initialize the oAuth access token
func (cc *ClientConf) auth(ctx api.StreamContext) error {
resp, err := httpx.Send(conf.Log, cc.client, "json", http.MethodPost, cc.accessConf.Url, nil, cc.accessConf.Body)
resp, err := httpx.Send(conf.Log, cc.client, "json", http.MethodPost, cc.accessConf.Url, cc.accessConf.Headers, cc.accessConf.Body)
if err != nil {
return err
}
Expand Down
32 changes: 0 additions & 32 deletions internal/pkg/httpx/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package httpx
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -83,37 +82,6 @@ func Send(logger api.Logger, client *http.Client, bodyType string, method string
return client.Do(req)
}

func convertToMap(v interface{}, sendSingle bool) (map[string]interface{}, error) {
switch t := v.(type) {
case []byte:
r := make(map[string]interface{})
if err := json.Unmarshal(t, &r); err != nil {
if sendSingle {
return nil, fmt.Errorf("fail to decode content: %v", err)
} else {
r["result"] = string(t)
}
}
return r, nil
case map[string]interface{}:
return t, nil
case []map[string]interface{}:
r := make(map[string]interface{})
if sendSingle {
return nil, fmt.Errorf("invalid content: %v", t)
} else {
j, err := json.Marshal(t)
if err != nil {
return nil, err
}
r["result"] = string(j)
}
return r, nil
default:
return nil, fmt.Errorf("invalid content: %v", v)
}
}

func IsValidUrl(uri string) bool {
pu, err := url.ParseRequestURI(uri)
if err != nil {
Expand Down

0 comments on commit 3098a05

Please sign in to comment.