generated from PenaSide/GolangTemplate
63 lines
1.4 KiB
Go
63 lines
1.4 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"go.uber.org/zap"
|
|
"log"
|
|
"net/url"
|
|
"path"
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/errors"
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"
|
|
"penahub.gitlab.yandexcloud.net/pena-services/customer/pkg/client"
|
|
)
|
|
|
|
type TemplateClientDeps struct {
|
|
Logger *zap.Logger
|
|
URLs *models.TemplategenMicroserviceURL
|
|
}
|
|
|
|
type TemplateClient struct {
|
|
logger *zap.Logger
|
|
urls *models.TemplategenMicroserviceURL
|
|
}
|
|
|
|
func NewTemplateClient(deps TemplateClientDeps) *TemplateClient {
|
|
|
|
if deps.Logger == nil {
|
|
log.Panicln("logger is nil on <NewTemplateClient>")
|
|
}
|
|
|
|
if deps.URLs == nil {
|
|
log.Panicln("urls is nil on <NewTemplateClient>")
|
|
}
|
|
|
|
return &TemplateClient{
|
|
logger: deps.Logger,
|
|
urls: deps.URLs,
|
|
}
|
|
}
|
|
|
|
func (receiver *TemplateClient) PostToTemplategen(ctx context.Context, data any) errors.Error {
|
|
tmplURL, err := url.Parse(receiver.urls.Templategen)
|
|
if err != nil {
|
|
return errors.New(
|
|
fmt.Errorf("failed to parse URL on <PostToTemplategen>: %w", err),
|
|
errors.ErrInternalError,
|
|
)
|
|
}
|
|
|
|
tmplURL.Path = path.Join(tmplURL.Path)
|
|
|
|
_, err = client.Post[interface{}, models.FastifyError](ctx, &client.RequestSettings{
|
|
URL: tmplURL.String(),
|
|
Headers: map[string]string{"Content-Type": "multipart/form-data"},
|
|
Body: data,
|
|
})
|
|
if err != nil {
|
|
return errors.New(err, errors.ErrInternalError)
|
|
}
|
|
|
|
return nil
|
|
}
|