chore: 初始化 Slurm Go SDK 项目骨架和核心客户端
添加 Go 模块定义、.gitignore、包文档、HTTP Client 核心、Token 认证和错误处理。 Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
39
internal/slurm/auth.go
Normal file
39
internal/slurm/auth.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package slurm
|
||||
|
||||
import "net/http"
|
||||
|
||||
// TokenAuthTransport implements http.RoundTripper and injects
|
||||
// X-SLURM-USER-NAME and X-SLURM-USER-TOKEN headers into every request.
|
||||
type TokenAuthTransport struct {
|
||||
UserName string
|
||||
Token string
|
||||
|
||||
// Base is the underlying RoundTripper. If nil, http.DefaultTransport is used.
|
||||
Base http.RoundTripper
|
||||
}
|
||||
|
||||
// RoundTrip executes a single HTTP request, adding Slurm authentication headers.
|
||||
func (t *TokenAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req2 := cloneRequest(req)
|
||||
req2.Header.Set("X-SLURM-USER-NAME", t.UserName)
|
||||
req2.Header.Set("X-SLURM-USER-TOKEN", t.Token)
|
||||
return t.transport().RoundTrip(req2)
|
||||
}
|
||||
|
||||
// Client returns a new http.Client that uses this TokenAuthTransport.
|
||||
func (t *TokenAuthTransport) Client() *http.Client {
|
||||
return &http.Client{Transport: t}
|
||||
}
|
||||
|
||||
func (t *TokenAuthTransport) transport() http.RoundTripper {
|
||||
if t.Base != nil {
|
||||
return t.Base
|
||||
}
|
||||
return http.DefaultTransport
|
||||
}
|
||||
|
||||
// cloneRequest creates a shallow copy of the request with a deep copy of the headers.
|
||||
func cloneRequest(req *http.Request) *http.Request {
|
||||
r := req.Clone(req.Context())
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user