added support for passing different bearer tokens
This commit is contained in:
parent
e11b297ae7
commit
a865a3ba2d
@ -10,7 +10,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
||||||
@ -23,7 +22,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
|
||||||
logg = slogging.Get().With("component", "sarafu-api.devapi")
|
logg = slogging.Get().With("component", "sarafu-api.devapi")
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -32,6 +30,10 @@ type APIError struct {
|
|||||||
Description string
|
Description string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ctxKey string
|
||||||
|
|
||||||
|
const ctxKeyAuthToken ctxKey = "authToken"
|
||||||
|
|
||||||
func (e *APIError) Error() string {
|
func (e *APIError) Error() string {
|
||||||
if e.Code != "" {
|
if e.Code != "" {
|
||||||
return fmt.Sprintf("[%s] %s", e.Code, e.Description)
|
return fmt.Sprintf("[%s] %s", e.Code, e.Description)
|
||||||
@ -787,7 +789,13 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
|
|||||||
var okResponse api.OKResponse
|
var okResponse api.OKResponse
|
||||||
var errResponse api.ErrResponse
|
var errResponse api.ErrResponse
|
||||||
|
|
||||||
|
// Check if a custom Authorization token was provided
|
||||||
|
if token, ok := ctx.Value(ctxKeyAuthToken).(string); ok && token != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+token)
|
||||||
|
} else {
|
||||||
req.Header.Set("Authorization", "Bearer "+config.BearerToken)
|
req.Header.Set("Authorization", "Bearer "+config.BearerToken)
|
||||||
|
}
|
||||||
|
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
// Log request
|
// Log request
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user