47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
package client
|
|
|
|
import (
|
|
"fmt"
|
|
"gitea.pena/PenaDevops/smtpbiz-exporter/internal/models"
|
|
"github.com/gofiber/fiber/v2"
|
|
urlPkg "net/url"
|
|
)
|
|
|
|
type SMTPClient struct {
|
|
apiURL string
|
|
client *fiber.Client
|
|
apiKey string
|
|
}
|
|
|
|
func NewSMTPClient(apiURL, apiKey string) *SMTPClient {
|
|
return &SMTPClient{
|
|
apiURL: apiURL,
|
|
client: fiber.AcquireClient(),
|
|
apiKey: apiKey,
|
|
}
|
|
}
|
|
|
|
func (c *SMTPClient) UseGetMethod(endpoint models.EndpointsSMTP, params map[string]interface{}) ([]byte, error) {
|
|
url := fmt.Sprintf("%s/%s", c.apiURL, endpoint)
|
|
if len(params) > 0 {
|
|
query := urlPkg.Values{}
|
|
for key, value := range params {
|
|
query.Add(key, fmt.Sprintf("%v", value))
|
|
}
|
|
url = fmt.Sprintf("%s?%s", url, query.Encode())
|
|
}
|
|
req := c.client.Get(url)
|
|
req.Set("Authorization", c.apiKey)
|
|
|
|
statusCode, respBody, errs := req.Bytes()
|
|
if errs != nil {
|
|
return nil, fmt.Errorf("request errors: %v", errs)
|
|
}
|
|
|
|
if statusCode != fiber.StatusOK {
|
|
return nil, fmt.Errorf("failed with status code %d: %s", statusCode, string(respBody))
|
|
}
|
|
|
|
return respBody, nil
|
|
}
|