mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-12 23:15:14 +00:00
feat: Add API key authentication support to charging engine client
- Add APIKey field to ChargingEngineConfig struct - Add apiKey field to ChargingEngineClient struct - Add setAPIKeyHeader method to set X-API-Key header when API key is configured - Call setAPIKeyHeader in CheckCredit, DeductCredit, AddCredit, GetBalance, and HealthCheck methods
This commit is contained in:
@@ -19,6 +19,7 @@ type Config struct {
|
|||||||
type ChargingEngineConfig struct {
|
type ChargingEngineConfig struct {
|
||||||
BaseURL string `json:"base_url"`
|
BaseURL string `json:"base_url"`
|
||||||
Timeout time.Duration `json:"timeout"`
|
Timeout time.Duration `json:"timeout"`
|
||||||
|
APIKey string `json:"api_key"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PaymentConfig struct {
|
type PaymentConfig struct {
|
||||||
|
|||||||
@@ -14,18 +14,27 @@ import (
|
|||||||
type ChargingEngineClient struct {
|
type ChargingEngineClient struct {
|
||||||
baseURL string
|
baseURL string
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
|
apiKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewChargingEngineClient creates a new client for the Rust charging engine
|
// NewChargingEngineClient creates a new client for the Rust charging engine
|
||||||
func NewChargingEngineClient(cfg *config.ChargingEngineConfig) *ChargingEngineClient {
|
func NewChargingEngineClient(cfg *config.ChargingEngineConfig) *ChargingEngineClient {
|
||||||
return &ChargingEngineClient{
|
return &ChargingEngineClient{
|
||||||
baseURL: cfg.BaseURL,
|
baseURL: cfg.BaseURL,
|
||||||
|
apiKey: cfg.APIKey,
|
||||||
httpClient: &http.Client{
|
httpClient: &http.Client{
|
||||||
Timeout: cfg.Timeout,
|
Timeout: cfg.Timeout,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// setAPIKeyHeader adds the X-API-Key header to a request if an API key is configured
|
||||||
|
func (c *ChargingEngineClient) setAPIKeyHeader(req *http.Request) {
|
||||||
|
if c.apiKey != "" {
|
||||||
|
req.Header.Set("X-API-Key", c.apiKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type CreditCheckRequest struct {
|
type CreditCheckRequest struct {
|
||||||
BytesRequested uint64 `json:"bytes_requested"`
|
BytesRequested uint64 `json:"bytes_requested"`
|
||||||
}
|
}
|
||||||
@@ -66,6 +75,7 @@ func (c *ChargingEngineClient) CheckCredit(ctx context.Context, ip string, bytes
|
|||||||
return nil, fmt.Errorf("create request: %w", err)
|
return nil, fmt.Errorf("create request: %w", err)
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
c.setAPIKeyHeader(req)
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -97,6 +107,7 @@ func (c *ChargingEngineClient) DeductCredit(ctx context.Context, ip string, byte
|
|||||||
return fmt.Errorf("create request: %w", err)
|
return fmt.Errorf("create request: %w", err)
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
c.setAPIKeyHeader(req)
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -123,6 +134,7 @@ func (c *ChargingEngineClient) AddCredit(ctx context.Context, ip string, bytesTo
|
|||||||
return fmt.Errorf("create request: %w", err)
|
return fmt.Errorf("create request: %w", err)
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
c.setAPIKeyHeader(req)
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -143,6 +155,7 @@ func (c *ChargingEngineClient) GetBalance(ctx context.Context, ip string) (*Bala
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("create request: %w", err)
|
return nil, fmt.Errorf("create request: %w", err)
|
||||||
}
|
}
|
||||||
|
c.setAPIKeyHeader(req)
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -168,6 +181,7 @@ func (c *ChargingEngineClient) HealthCheck(ctx context.Context) (*EngineHealthRe
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("create request: %w", err)
|
return nil, fmt.Errorf("create request: %w", err)
|
||||||
}
|
}
|
||||||
|
c.setAPIKeyHeader(req)
|
||||||
|
|
||||||
resp, err := c.httpClient.Do(req)
|
resp, err := c.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user