Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support auth request headers #3376

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading