rpaas-core-service/pkg/services/branchservice.go

51 lines
1.1 KiB
Go
Raw Normal View History

2024-05-03 11:57:03 +00:00
package services
2024-05-03 11:57:06 +00:00
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"pkg.jojmatic.com/root/rpaas-core-service/pkg/models"
)
type BranchService interface {
RegisterBranch(endpoint string, body *models.BranchRegisterBody) (map[string]interface{}, error)
}
type BranchServiceImpl struct {
ctx context.Context
}
func NewBranchService(ctx context.Context) *BranchServiceImpl {
return &BranchServiceImpl{
ctx: ctx,
}
}
func (bc *BranchServiceImpl) RegisterBranch(endpoint string, body *models.BranchRegisterBody) (map[string]interface{}, error) {
url := endpoint + "v1/branch/register"
branchInfo, err := json.Marshal(body)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(branchInfo))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("register company failed, status code: %d", resp.StatusCode)
}
var datajson map[string]interface{}
data, _ := io.ReadAll(resp.Body)
if err := json.Unmarshal(data, &datajson); err != nil {
return nil, err
}
return datajson, err
}