66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package veli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/SamuelTariku/FortuneBet-Backend/internal/config"
|
|
"github.com/go-resty/resty/v2"
|
|
)
|
|
|
|
type VeliClient struct {
|
|
client *resty.Client
|
|
config *config.Config
|
|
}
|
|
|
|
func NewVeliClient(cfg *config.Config) *VeliClient {
|
|
client := resty.New().
|
|
SetBaseURL(cfg.VeliGames.APIKey).
|
|
SetHeader("Accept", "application/json").
|
|
SetHeader("X-API-Key", cfg.VeliGames.APIKey).
|
|
SetTimeout(30 * time.Second)
|
|
|
|
return &VeliClient{
|
|
client: client,
|
|
config: cfg,
|
|
}
|
|
}
|
|
|
|
func (vc *VeliClient) Get(ctx context.Context, endpoint string, result interface{}) error {
|
|
resp, err := vc.client.R().
|
|
SetContext(ctx).
|
|
SetResult(result).
|
|
Get(endpoint)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("request failed: %w", err)
|
|
}
|
|
|
|
if resp.IsError() {
|
|
return fmt.Errorf("API error: %s", resp.Status())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (vc *VeliClient) Post(ctx context.Context, endpoint string, body interface{}, result interface{}) error {
|
|
resp, err := vc.client.R().
|
|
SetContext(ctx).
|
|
SetBody(body).
|
|
SetResult(result).
|
|
Post(endpoint)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("request failed: %w", err)
|
|
}
|
|
|
|
if resp.IsError() {
|
|
return fmt.Errorf("API error: %s", resp.Status())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Add other HTTP methods as needed (Put, Delete, etc.)
|