inital commit

master
unknown 2024-05-03 14:57:06 +03:00
parent 8a69c4596e
commit 5ceca6af39
1 changed files with 49 additions and 0 deletions

View File

@ -1 +1,50 @@
package services
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
}