parent
e3fd6326dc
commit
1b3d551f0c
@ -1,70 +0,0 @@
|
||||
package sdk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Configuration for SDK
|
||||
type HttpConfig struct {
|
||||
AppID string
|
||||
PlatformID string // Renamed for consistency
|
||||
ChannelID string
|
||||
AppSecret string
|
||||
BaseURL string // Either Test or Production
|
||||
}
|
||||
|
||||
// RequestParams for common parameters
|
||||
type RequestParams map[string]interface{} // Renamed to be more descriptive
|
||||
|
||||
// Response for API responses
|
||||
type Response struct {
|
||||
ErrorCode int `json:"code"` // Renamed to ErrorCode for clarity
|
||||
ErrorMessage string `json:"msg"` // Renamed to ErrorMessage for clarity
|
||||
}
|
||||
|
||||
var (
|
||||
clientInstance *Client
|
||||
once sync.Once
|
||||
)
|
||||
|
||||
// NewClient initializes the SDK client with reusable resty client (Singleton)
|
||||
func NewClient(config Config) *Client {
|
||||
once.Do(func() {
|
||||
clientInstance = &Client{
|
||||
config: config,
|
||||
client: resty.New().SetRetryCount(3).SetRetryWaitTime(1 * time.Second),
|
||||
}
|
||||
})
|
||||
return clientInstance
|
||||
}
|
||||
|
||||
// Client struct to manage requests
|
||||
type Client struct {
|
||||
config Config
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
// HTTP POST helper function with retry logic
|
||||
func (c *Client) post(url string, params interface{}) (*Response, error) {
|
||||
// Retry up to 3 times with a 1-second delay between retries
|
||||
var resp *Response
|
||||
response, err := c.client.R().
|
||||
SetBody(params).
|
||||
SetResult(&resp). // Set result to unmarshal JSON into Response struct
|
||||
Post(url)
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request failed: %v", err)
|
||||
}
|
||||
|
||||
// Check the response status code
|
||||
if response.StatusCode() != 200 {
|
||||
return nil, fmt.Errorf("non-200 response received: %v", response.StatusCode())
|
||||
}
|
||||
|
||||
// Return the response body directly after it has been unmarshalled
|
||||
return resp, nil
|
||||
}
|
Loading…
Reference in New Issue